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 8d9bddb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
23 changes: 1 addition & 22 deletions frontend/src/ts/popups/pb-tables-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedTypes.Config.Mode>;
Expand Down Expand Up @@ -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() + "%";
}
21 changes: 21 additions & 0 deletions frontend/src/ts/utils/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as Misc from "./misc";
import Config from "../config";
import { get as getTypingSpeedUnit } from "../utils/typing-speed-units";

export function formatTypingSpeed(wpm?: number, fallback?: "-"): string {
if (wpm === undefined || wpm === null) return fallback ?? "";
const result = getTypingSpeedUnit(Config.typingSpeedUnit).fromWpm(wpm);
if (Config.alwaysShowDecimalPlaces) {
return Misc.roundTo2(result).toFixed(2);
}
return Math.round(result).toString();
}

export function formatPercentage(percentage?: number, fallback?: "-"): string {
if (percentage === undefined || percentage === null) return fallback ?? "";

if (Config.alwaysShowDecimalPlaces) {
return Misc.roundTo2(percentage).toFixed(2) + "%";
}
return Math.round(percentage).toString() + "%";
}

0 comments on commit 8d9bddb

Please sign in to comment.