diff --git a/frontend/src/ts/popups/pb-tables-popup.ts b/frontend/src/ts/popups/pb-tables-popup.ts index aa3c23514f0ed..13327f0a98520 100644 --- a/frontend/src/ts/popups/pb-tables-popup.ts +++ b/frontend/src/ts/popups/pb-tables-popup.ts @@ -4,7 +4,8 @@ import * as Skeleton from "./skeleton"; import { getLanguageDisplayString, isPopupVisible } from "../utils/misc"; import Config from "../config"; import { get as getTypingSpeedUnit } from "../utils/typing-speed-units"; -import * as Misc from "../utils/misc"; + +import * as Format from "../utils/format"; type PersonalBest = { mode2: SharedTypes.Config.Mode2; @@ -47,6 +48,15 @@ function update(mode: SharedTypes.Config.Mode): void { }); let mode2memory: SharedTypes.Config.Mode2; + const formatTypingSpeed = Format.formatTypingSpeed( + getTypingSpeedUnit(Config.typingSpeedUnit), + Config.alwaysShowDecimalPlaces, + "-" + ); + const formatPercentage = Format.formatPercentage( + Config.alwaysShowDecimalPlaces, + "-" + ); list.forEach((pb) => { let dateText = `-
-`; @@ -138,23 +148,3 @@ $(document).on("keydown", (event) => { }); Skeleton.save(wrapperId); - -function formatTypingSpeed(wpm?: number): string { - if (wpm === undefined || wpm === null) return "-"; - - const typingSpeedUnit = getTypingSpeedUnit(Config.typingSpeedUnit); - const result = typingSpeedUnit.fromWpm(wpm); - if (Config.alwaysShowDecimalPlaces) { - return Misc.roundTo2(result).toFixed(2); - } - return Math.round(result).toString(); -} - -function formatPercentage(percentage?: number): string { - if (percentage === undefined || percentage === null) return "-"; - - if (Config.alwaysShowDecimalPlaces) { - return Misc.roundTo2(percentage).toFixed(2) + "%"; - } - return Math.round(percentage).toString() + "%"; -} diff --git a/frontend/src/ts/utils/format.ts b/frontend/src/ts/utils/format.ts new file mode 100644 index 0000000000000..1190054ccc594 --- /dev/null +++ b/frontend/src/ts/utils/format.ts @@ -0,0 +1,30 @@ +import * as Misc from "./misc"; + +export function formatTypingSpeed( + unit: MonkeyTypes.TypingSpeedUnitSettings, + alwaysShowDecimalPlaces: boolean, + fallback?: string +): (wpm: number) => string { + return (wpm) => { + if (wpm === undefined || wpm === null) return fallback ?? ""; + const result = unit.fromWpm(wpm); + if (alwaysShowDecimalPlaces) { + return Misc.roundTo2(result).toFixed(2); + } + return Math.round(result).toString(); + }; +} + +export function formatPercentage( + alwaysShowDecimalPlaces: boolean, + fallback?: string +): (percentage: number) => string { + return (percentage) => { + if (percentage === undefined || percentage === null) return fallback ?? ""; + + if (alwaysShowDecimalPlaces) { + return Misc.roundTo2(percentage).toFixed(2) + "%"; + } + return Math.round(percentage).toString() + "%"; + }; +}