mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-03-01 01:05:06 +08:00
refactor: use class in typing-speed-unit (ferhmer) (#4518)
This commit is contained in:
parent
e06213be3c
commit
e4c782f9d9
2 changed files with 40 additions and 61 deletions
1
frontend/src/ts/types/types.d.ts
vendored
1
frontend/src/ts/types/types.d.ts
vendored
|
@ -889,6 +889,7 @@ declare namespace MonkeyTypes {
|
|||
type AllRewards = XpReward | BadgeReward;
|
||||
|
||||
type TypingSpeedUnit = "wpm" | "cpm" | "wps" | "cps" | "wph";
|
||||
|
||||
interface TypingSpeedUnitSettings {
|
||||
fromWpm: (number) => number;
|
||||
toWpm: (number) => number;
|
||||
|
|
|
@ -1,60 +1,45 @@
|
|||
import { roundTo2 } from "../utils/misc";
|
||||
const typingSpeedUnits: Record<
|
||||
MonkeyTypes.TypingSpeedUnit,
|
||||
MonkeyTypes.TypingSpeedUnitSettings
|
||||
> = {
|
||||
wpm: {
|
||||
fromWpm: (wpm: number) => wpm,
|
||||
toWpm: (wpm: number) => wpm,
|
||||
convertWithUnitSuffix: (wpm: number) => {
|
||||
return convertTypingSpeedWithUnitSuffix("wpm", wpm);
|
||||
},
|
||||
|
||||
fullUnitString: "Words per Minute",
|
||||
histogramDataBucketSize: 10,
|
||||
historyStepSize: 10,
|
||||
},
|
||||
cpm: {
|
||||
fromWpm: (wpm: number) => wpm * 5,
|
||||
toWpm: (cpm: number) => cpm / 5,
|
||||
convertWithUnitSuffix: (wpm: number) => {
|
||||
return convertTypingSpeedWithUnitSuffix("cpm", wpm);
|
||||
},
|
||||
fullUnitString: "Characters per Minute",
|
||||
histogramDataBucketSize: 50,
|
||||
historyStepSize: 100,
|
||||
},
|
||||
wps: {
|
||||
fromWpm: (wpm: number) => wpm / 60,
|
||||
toWpm: (wps: number) => wps * 60,
|
||||
convertWithUnitSuffix: (wpm: number) => {
|
||||
return convertTypingSpeedWithUnitSuffix("wps", wpm);
|
||||
},
|
||||
fullUnitString: "Words per Second",
|
||||
histogramDataBucketSize: 0.5,
|
||||
historyStepSize: 2,
|
||||
},
|
||||
cps: {
|
||||
fromWpm: (wpm: number) => (wpm * 5) / 60,
|
||||
toWpm: (cps: number) => (cps * 60) / 5,
|
||||
convertWithUnitSuffix: (wpm: number) => {
|
||||
return convertTypingSpeedWithUnitSuffix("cps", wpm);
|
||||
},
|
||||
fullUnitString: "Characters per Second",
|
||||
histogramDataBucketSize: 5,
|
||||
historyStepSize: 5,
|
||||
},
|
||||
wph: {
|
||||
fromWpm: (wpm: number) => wpm * 60,
|
||||
toWpm: (wph: number) => wph / 60,
|
||||
convertWithUnitSuffix: (wpm: number) => {
|
||||
return convertTypingSpeedWithUnitSuffix("wph", wpm);
|
||||
},
|
||||
class Unit implements MonkeyTypes.TypingSpeedUnitSettings {
|
||||
unit: MonkeyTypes.TypingSpeedUnit;
|
||||
convertFactor: number;
|
||||
fullUnitString: string;
|
||||
histogramDataBucketSize: number;
|
||||
historyStepSize: number;
|
||||
|
||||
fullUnitString: "Words per Hour",
|
||||
histogramDataBucketSize: 250,
|
||||
historyStepSize: 1000,
|
||||
},
|
||||
constructor(
|
||||
unit: MonkeyTypes.TypingSpeedUnit,
|
||||
convertFactor: number,
|
||||
fullUnitString: string,
|
||||
histogramDataBucketSize: number,
|
||||
historyStepSize: number
|
||||
) {
|
||||
this.unit = unit;
|
||||
this.convertFactor = convertFactor;
|
||||
this.fullUnitString = fullUnitString;
|
||||
this.histogramDataBucketSize = histogramDataBucketSize;
|
||||
this.historyStepSize = historyStepSize;
|
||||
}
|
||||
|
||||
fromWpm(wpm: number): number {
|
||||
return wpm * this.convertFactor;
|
||||
}
|
||||
|
||||
toWpm(val: number): number {
|
||||
return val / this.convertFactor;
|
||||
}
|
||||
|
||||
convertWithUnitSuffix(wpm: number): string {
|
||||
return roundTo2(this.fromWpm(wpm)).toFixed(2) + " " + this.unit;
|
||||
}
|
||||
}
|
||||
|
||||
const typingSpeedUnits: Record<MonkeyTypes.TypingSpeedUnit, Unit> = {
|
||||
wpm: new Unit("wpm", 1, "Words per Minute", 10, 10),
|
||||
cpm: new Unit("cpm", 5, "Characters per Minute", 50, 100),
|
||||
wps: new Unit("wps", 1 / 60, "Words per Second", 0.5, 2),
|
||||
cps: new Unit("cps", 5 / 60, "Characters per Second", 5, 5),
|
||||
wph: new Unit("wph", 60, "Words per Hour", 250, 1000),
|
||||
};
|
||||
|
||||
export function get(
|
||||
|
@ -62,10 +47,3 @@ export function get(
|
|||
): MonkeyTypes.TypingSpeedUnitSettings {
|
||||
return typingSpeedUnits[unit];
|
||||
}
|
||||
|
||||
function convertTypingSpeedWithUnitSuffix(
|
||||
unit: MonkeyTypes.TypingSpeedUnit,
|
||||
wpm: number
|
||||
): string {
|
||||
return roundTo2(get(unit).fromWpm(wpm)).toFixed(2) + " " + unit;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue