From bb11548b598c36a66498556c50009b28de7e5cf7 Mon Sep 17 00:00:00 2001 From: Brett Gilio Date: Thu, 22 Dec 2022 23:12:24 +0000 Subject: [PATCH] Revise suffixes and make them SI compliant --- client/js/helpers/roundBadgeNumber.ts | 17 ++++++++--------- test/client/js/helpers/roundBadgeNumberTest.ts | 6 +++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/client/js/helpers/roundBadgeNumber.ts b/client/js/helpers/roundBadgeNumber.ts index 537746bc..55e1b7d0 100644 --- a/client/js/helpers/roundBadgeNumber.ts +++ b/client/js/helpers/roundBadgeNumber.ts @@ -1,12 +1,11 @@ export default (count: number) => { - const suffixes = [ - {divisor: 1, suffix: ""}, - {divisor: 1000, suffix: "k"}, - {divisor: 1000000, suffix: "m"}, - ]; + if (count < 1000) { + return count.toString(); + } - const {divisor, suffix} = - suffixes[Math.min(suffixes.length - 1, Math.floor(Math.log10(count) / 3))]; - - return String(Math.ceil((count / divisor) * 10) / 10).concat(suffix); + const suffixes = ["", "k", "M"]; + const magnitudeIndex = Math.min(Math.floor(Math.log10(count) / 3), suffixes.length - 1); + const magnitude = 1000 ** magnitudeIndex; + const roundedCount = (count / magnitude).toFixed(1); + return roundedCount + suffixes[magnitudeIndex]; }; diff --git a/test/client/js/helpers/roundBadgeNumberTest.ts b/test/client/js/helpers/roundBadgeNumberTest.ts index 25925fa4..f6fe0c33 100644 --- a/test/client/js/helpers/roundBadgeNumberTest.ts +++ b/test/client/js/helpers/roundBadgeNumberTest.ts @@ -10,9 +10,9 @@ describe("roundBadgeNumber helper", function () { expect(roundBadgeNumber(1000)).to.be.equal("1.0k"); }); - it("should return numbers above 999999 with a 'm' suffix", function () { - expect(roundBadgeNumber(1000000)).to.be.equal("1.0m"); - expect(roundBadgeNumber(1234567)).to.be.equal("1.2m"); + it("should return numbers above 999999 with a 'M' suffix", function () { + expect(roundBadgeNumber(1000000)).to.be.equal("1.0M"); + expect(roundBadgeNumber(1234567)).to.be.equal("1.2M"); }); it("should round and not floor", function () {