Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fehmer committed Feb 16, 2024
1 parent 80b4fd7 commit 3a44525
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
32 changes: 11 additions & 21 deletions frontend/src/ts/popups/pb-tables-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedTypes.Config.Mode>;
Expand Down Expand Up @@ -47,6 +48,15 @@ function update(mode: SharedTypes.Config.Mode): void {
});

let mode2memory: SharedTypes.Config.Mode2<SharedTypes.Config.Mode>;
const formatTypingSpeed = Format.formatTypingSpeed(
getTypingSpeedUnit(Config.typingSpeedUnit),
Config.alwaysShowDecimalPlaces,
"-"
);
const formatPercentage = Format.formatPercentage(
Config.alwaysShowDecimalPlaces,
"-"
);

list.forEach((pb) => {
let dateText = `-<br><span class="sub">-</span>`;
Expand Down Expand Up @@ -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() + "%";
}
30 changes: 30 additions & 0 deletions frontend/src/ts/utils/format.ts
Original file line number Diff line number Diff line change
@@ -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() + "%";
};
}

0 comments on commit 3a44525

Please sign in to comment.