Skip to content

Commit

Permalink
impr: floor accuracy intead of rounding when not showing decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
fehmer committed Feb 24, 2024
1 parent 91c30cd commit 17e12be
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
21 changes: 21 additions & 0 deletions frontend/__tests__/utils/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ describe("format.ts", () => {
expect(format.typingSpeed(null, { suffix: " raw" })).toEqual("-");
expect(format.typingSpeed(undefined, { suffix: " raw" })).toEqual("-");
});

it("should format with rounding", () => {
const format = getInstance({ alwaysShowDecimalPlaces: false });
expect(format.typingSpeed(80.25)).toEqual("80");
expect(format.typingSpeed(80.25, { rounding: Math.ceil })).toEqual("81");
expect(format.typingSpeed(80.75, { rounding: Math.floor })).toEqual("80");
});
});
describe("percentage", () => {
it("should format with decimalPlaces from configuration", () => {
Expand Down Expand Up @@ -142,6 +149,13 @@ describe("format.ts", () => {
expect(format.percentage(null, { suffix: " raw" })).toEqual("-");
expect(format.percentage(undefined, { suffix: " raw" })).toEqual("-");
});

it("should format with rounding", () => {
const format = getInstance({ alwaysShowDecimalPlaces: false });
expect(format.percentage(80.25)).toEqual("80%");
expect(format.percentage(80.25, { rounding: Math.ceil })).toEqual("81%");
expect(format.percentage(80.75, { rounding: Math.floor })).toEqual("80%");
});
});
describe("decimals", () => {
it("should format with decimalPlaces from configuration", () => {
Expand Down Expand Up @@ -192,6 +206,13 @@ describe("format.ts", () => {
expect(format.decimals(null, { suffix: " raw" })).toEqual("-");
expect(format.decimals(undefined, { suffix: " raw" })).toEqual("-");
});

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

Expand Down
6 changes: 5 additions & 1 deletion frontend/src/ts/account/pb-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ function buildPbHtml(
})}</div>
<div class="acc">${Format.percentage(pbData.acc, {
showDecimalPlaces: false,
rounding: Math.floor,
})}</div>
</div>
<div class="fullTest">
Expand All @@ -164,7 +165,10 @@ function buildPbHtml(
suffix: ` ${speedUnit}`,
})}</div>
<div>${Format.typingSpeed(pbData.raw, { suffix: " raw" })}</div>
<div>${Format.percentage(pbData.acc, { suffix: " acc" })}</div>
<div>${Format.percentage(pbData.acc, {
suffix: " acc",
rounding: Math.floor,
})}</div>
<div>${Format.percentage(pbData.consistency, {
suffix: " con",
})}</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ts/elements/modes-notice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export async function update(): Promise<void> {
: "";

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

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

$(".pageAccount .highestAcc .val").text(Format.percentage(topAcc));
$(".pageAccount .avgAcc .val").text(Format.percentage(totalAcc / testCount));
$(".pageAccount .avgAcc10 .val").text(Format.percentage(totalAcc10 / last10));
$(".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 })
);

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

if (Config.alwaysShowDecimalPlaces) {
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/ts/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export type FormatOptions = {
showDecimalPlaces?: boolean;
suffix?: string;
fallback?: string;
rounding?: (val: number) => number;
};

const FORMAT_DEFAULT_OPTIONS: FormatOptions = {
suffix: "",
fallback: "-",
showDecimalPlaces: undefined,
rounding: Math.round,
};

export class Formatting {
Expand Down Expand Up @@ -60,7 +62,7 @@ export class Formatting {
) {
return Misc.roundTo2(value).toFixed(2) + suffix;
}
return Math.round(value).toString() + suffix;
return (formatOptions.rounding ?? Math.round)(value).toString() + suffix;
}
}

Expand Down

0 comments on commit 17e12be

Please sign in to comment.