Skip to content

Commit

Permalink
add method to format accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
fehmer committed Feb 26, 2024
1 parent b69d20c commit 310e7ed
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 33 deletions.
20 changes: 20 additions & 0 deletions frontend/__tests__/utils/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe("format.ts", () => {
expect(format.typingSpeed(80.75, { rounding: Math.floor })).toEqual("80");
});
});

describe("percentage", () => {
it("should format with decimalPlaces from configuration", () => {
//no decimals
Expand Down Expand Up @@ -153,6 +154,25 @@ describe("format.ts", () => {
expect(format.percentage(80.75, { rounding: Math.floor })).toEqual("80%");
});
});

describe("accuracy", () => {
it("should floor decimals by default", () => {
//no decimals
const noDecimals = getInstance({ alwaysShowDecimalPlaces: false });
expect(noDecimals.accuracy(12.75)).toEqual("12%");
//with decimals
const withDecimals = getInstance({ alwaysShowDecimalPlaces: true });
expect(withDecimals.accuracy(12.75)).toEqual("12.75%");
});

it("should format with rounding", () => {
const format = getInstance({ alwaysShowDecimalPlaces: false });
expect(format.accuracy(80.5)).toEqual("80%");
expect(format.accuracy(80.25, { rounding: Math.ceil })).toEqual("81%");
expect(format.accuracy(80.75, { rounding: Math.floor })).toEqual("80%");
});
});

describe("decimals", () => {
it("should format with decimalPlaces from configuration", () => {
//no decimals
Expand Down
12 changes: 3 additions & 9 deletions frontend/src/ts/account/pb-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ function buildPbHtml(
<div class="wpm">${Format.typingSpeed(pbData.wpm, {
showDecimalPlaces: false,
})}</div>
<div class="acc">${Format.percentage(pbData.acc, {
<div class="acc">${Format.accuracy(pbData.acc, {
showDecimalPlaces: false,
rounding: Math.floor,
})}</div>
</div>
<div class="fullTest">
Expand All @@ -165,13 +164,8 @@ function buildPbHtml(
suffix: ` ${speedUnit}`,
})}</div>
<div>${Format.typingSpeed(pbData.raw, { suffix: " raw" })}</div>
<div>${Format.percentage(pbData.acc, {
suffix: " acc",
rounding: Math.floor,
})}</div>
<div>${Format.percentage(pbData.consistency, {
suffix: " con",
})}</div>
<div>${Format.accuracy(pbData.acc, { suffix: " acc" })}</div>
<div>${Format.percentage(pbData.consistency, { suffix: " con" })}</div>
<div>${dateText}</div>
</div>`;
} catch (e) {
Expand Down
11 changes: 3 additions & 8 deletions frontend/src/ts/elements/modes-notice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,16 @@ export async function update(): Promise<void> {
}

if (Config.showAverage !== "off") {
let avgWPM = Last10Average.getWPM();
let avgAcc = Last10Average.getAcc();

if (!Config.alwaysShowDecimalPlaces) {
avgWPM = Math.round(avgWPM);
avgAcc = Math.round(avgAcc);
}
const avgWPM = Last10Average.getWPM();
const avgAcc = Last10Average.getAcc();

if (isAuthenticated() && avgWPM > 0) {
const avgWPMText = ["speed", "both"].includes(Config.showAverage)
? Format.typingSpeed(avgWPM, { suffix: ` ${Config.typingSpeedUnit}` })
: "";

const avgAccText = ["acc", "both"].includes(Config.showAverage)
? Format.percentage(avgAcc, { suffix: " acc", rounding: Math.floor })
? Format.accuracy(avgAcc, { suffix: " acc" })
: "";

const text = `${avgWPMText} ${avgAccText}`.trim();
Expand Down
12 changes: 3 additions & 9 deletions frontend/src/ts/pages/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,15 +881,9 @@ async function fillContent(): Promise<void> {
$(".pageAccount .highestWpm .mode").html(topMode);
$(".pageAccount .testsTaken .val").text(testCount);

$(".pageAccount .highestAcc .val").text(
Format.percentage(topAcc, { rounding: Math.floor })
);
$(".pageAccount .avgAcc .val").text(
Format.percentage(totalAcc / testCount, { rounding: Math.floor })
);
$(".pageAccount .avgAcc10 .val").text(
Format.percentage(totalAcc10 / last10, { rounding: Math.floor })
);
$(".pageAccount .highestAcc .val").text(Format.accuracy(topAcc));
$(".pageAccount .avgAcc .val").text(Format.accuracy(totalAcc / testCount));
$(".pageAccount .avgAcc10 .val").text(Format.accuracy(totalAcc10 / last10));

if (totalCons === 0 || totalCons === undefined) {
$(".pageAccount .avgCons .val").text("-");
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/ts/popups/pb-tables-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ function update(mode: SharedTypes.Config.Mode): void {
<td>
${Format.typingSpeed(pb.wpm)}
<br />
<span class="sub">${Format.percentage(pb.acc, {
rounding: Math.floor,
})}</span>
<span class="sub">${Format.accuracy(pb.acc)}</span>
</td>
<td>
${Format.typingSpeed(pb.raw)}
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/ts/test/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ function updateWpmAndAcc(): void {
}
$("#result .stats .raw .bottom").text(Format.typingSpeed(result.rawWpm));
$("#result .stats .acc .bottom").text(
result.acc === 100
? "100%"
: Format.percentage(result.acc, { rounding: Math.floor })
result.acc === 100 ? "100%" : Format.accuracy(result.acc)
);

if (Config.alwaysShowDecimalPlaces) {
Expand Down
14 changes: 13 additions & 1 deletion frontend/src/ts/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Formatting {

typingSpeed(
wpm: number | null | undefined,
formatOptions: FormatOptions = FORMAT_DEFAULT_OPTIONS
formatOptions: FormatOptions = {}
): string {
const options = { ...FORMAT_DEFAULT_OPTIONS, ...formatOptions };
if (wpm === undefined || wpm === null) return options.fallback ?? "";
Expand All @@ -30,6 +30,7 @@ export class Formatting {

return this.number(result, options);
}

percentage(
percentage: number | null | undefined,
formatOptions: FormatOptions = {}
Expand All @@ -40,13 +41,24 @@ export class Formatting {
return this.number(percentage, options);
}

accuracy(
accuracy: number | null | undefined,
formatOptions: FormatOptions = {}
): string {
return this.percentage(accuracy, {
rounding: Math.floor,
...formatOptions,
});
}

decimals(
value: number | null | undefined,
formatOptions: FormatOptions = {}
): string {
const options = { ...FORMAT_DEFAULT_OPTIONS, ...formatOptions };
return this.number(value, options);
}

private number(
value: number | null | undefined,
formatOptions: FormatOptions
Expand Down

0 comments on commit 310e7ed

Please sign in to comment.