diff --git a/frontend/src/ts/popups/pb-tables-popup.ts b/frontend/src/ts/popups/pb-tables-popup.ts index aa3c23514f0e..e7606f361018 100644 --- a/frontend/src/ts/popups/pb-tables-popup.ts +++ b/frontend/src/ts/popups/pb-tables-popup.ts @@ -3,8 +3,7 @@ import format from "date-fns/format"; 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 { formatTypingSpeed, formatPercentage } from "../utils/format"; type PersonalBest = { mode2: SharedTypes.Config.Mode2; @@ -138,23 +137,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 000000000000..d09e66bb8f5f --- /dev/null +++ b/frontend/src/ts/utils/format.ts @@ -0,0 +1,33 @@ +import * as Misc from "./misc"; +import Config from "../config"; +import { get as getTypingSpeedUnit } from "../utils/typing-speed-units"; + +export function formatTypingSpeed( + wpm: number | null | undefined, + showDecimals?: boolean, + fallback?: "-" +): string { + if (wpm === undefined || wpm === null) return fallback ?? ""; + const result = getTypingSpeedUnit(Config.typingSpeedUnit).fromWpm(wpm); + if ( + showDecimals !== undefined ? showDecimals : Config.alwaysShowDecimalPlaces + ) { + return Misc.roundTo2(result).toFixed(2); + } + return Math.round(result).toString(); +} + +export function formatPercentage( + percentage: number | null | undefined, + showDecimals?: boolean, + fallback?: "-" +): string { + if (percentage === undefined || percentage === null) return fallback ?? ""; + + if ( + showDecimals !== undefined ? showDecimals : Config.alwaysShowDecimalPlaces + ) { + return Misc.roundTo2(percentage).toFixed(2) + "%"; + } + return Math.round(percentage).toString() + "%"; +}