From e4c782f9d9e103a825b09c0c6bcd53f690d2bf7b Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Mon, 14 Aug 2023 14:24:00 +0200 Subject: [PATCH] refactor: use class in typing-speed-unit (ferhmer) (#4518) --- frontend/src/ts/types/types.d.ts | 1 + frontend/src/ts/utils/typing-speed-units.ts | 100 ++++++++------------ 2 files changed, 40 insertions(+), 61 deletions(-) diff --git a/frontend/src/ts/types/types.d.ts b/frontend/src/ts/types/types.d.ts index 891826227..b8ead1897 100644 --- a/frontend/src/ts/types/types.d.ts +++ b/frontend/src/ts/types/types.d.ts @@ -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; diff --git a/frontend/src/ts/utils/typing-speed-units.ts b/frontend/src/ts/utils/typing-speed-units.ts index bbda7995f..843d4f09a 100644 --- a/frontend/src/ts/utils/typing-speed-units.ts +++ b/frontend/src/ts/utils/typing-speed-units.ts @@ -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 = { + 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; -}