From 15edee4bf939c114a93377a2e2b255ecc32ef0e7 Mon Sep 17 00:00:00 2001 From: Brett Gilio Date: Mon, 19 Dec 2022 00:08:24 +0000 Subject: [PATCH 1/4] Use "m" shorthand for messages >= one million. Implements [Unread messages count] Abandon "k" (= thousand) when it reaches million+ #4615 --- client/js/helpers/roundBadgeNumber.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/client/js/helpers/roundBadgeNumber.ts b/client/js/helpers/roundBadgeNumber.ts index dd7b6fdc..6318ac29 100644 --- a/client/js/helpers/roundBadgeNumber.ts +++ b/client/js/helpers/roundBadgeNumber.ts @@ -3,5 +3,11 @@ export default (count: number) => { return count.toString(); } - return (count / 1000).toFixed(2).slice(0, -1) + "k"; + if (count >= 1000 && count < 1000000) { + return (count / 1000).toFixed(2).slice(0, -1) + "k"; + } + + if (count >= 1000000) { + return (count / 1000000).toFixed(2).slice(0, -1) + "m"; + } }; From 4cfba05c56c88576f53263673a0e095a9a0c25f1 Mon Sep 17 00:00:00 2001 From: Brett Gilio Date: Thu, 22 Dec 2022 20:51:13 +0000 Subject: [PATCH 2/4] Use "m" shorthand for messages >= one million Implements [Unread messages count] Abandon "k" (= thousand) when it reaches million+ #4615 --- client/js/helpers/roundBadgeNumber.ts | 17 ++++++++--------- test/client/js/helpers/roundBadgeNumberTest.ts | 7 ++++++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/client/js/helpers/roundBadgeNumber.ts b/client/js/helpers/roundBadgeNumber.ts index 6318ac29..25c275b8 100644 --- a/client/js/helpers/roundBadgeNumber.ts +++ b/client/js/helpers/roundBadgeNumber.ts @@ -1,13 +1,12 @@ export default (count: number) => { - if (count < 1000) { - return count.toString(); - } + const suffixes = [ + {divisor: 1, suffix: ""}, + {divisor: 1000, suffix: "k"}, + {divisor: 1000000, suffix: "m"}, + ]; - if (count >= 1000 && count < 1000000) { - return (count / 1000).toFixed(2).slice(0, -1) + "k"; - } + const {divisor, suffix} = + suffixes[Math.min(suffixes.length - 1, Math.floor(Math.log10(count) / 3))]; - if (count >= 1000000) { - return (count / 1000000).toFixed(2).slice(0, -1) + "m"; - } + return (count / divisor).toFixed(2).slice(0, -1) + suffix; }; diff --git a/test/client/js/helpers/roundBadgeNumberTest.ts b/test/client/js/helpers/roundBadgeNumberTest.ts index 1b26ee91..25925fa4 100644 --- a/test/client/js/helpers/roundBadgeNumberTest.ts +++ b/test/client/js/helpers/roundBadgeNumberTest.ts @@ -6,10 +6,15 @@ describe("roundBadgeNumber helper", function () { expect(roundBadgeNumber(123)).to.equal("123"); }); - it("should return numbers above 999 in thousands", function () { + it("should return numbers between 1000 and 999999 with a 'k' suffix", 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 round and not floor", function () { expect(roundBadgeNumber(9999)).to.be.equal("10.0k"); }); From 523834c403836b9f13d5dae70ed0c1d07baf0f4e Mon Sep 17 00:00:00 2001 From: Brett Gilio Date: Thu, 22 Dec 2022 21:06:52 +0000 Subject: [PATCH 3/4] Do not floor the resultant string value. --- client/js/helpers/roundBadgeNumber.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/js/helpers/roundBadgeNumber.ts b/client/js/helpers/roundBadgeNumber.ts index 25c275b8..537746bc 100644 --- a/client/js/helpers/roundBadgeNumber.ts +++ b/client/js/helpers/roundBadgeNumber.ts @@ -8,5 +8,5 @@ export default (count: number) => { const {divisor, suffix} = suffixes[Math.min(suffixes.length - 1, Math.floor(Math.log10(count) / 3))]; - return (count / divisor).toFixed(2).slice(0, -1) + suffix; + return String(Math.ceil((count / divisor) * 10) / 10).concat(suffix); }; From bb11548b598c36a66498556c50009b28de7e5cf7 Mon Sep 17 00:00:00 2001 From: Brett Gilio Date: Thu, 22 Dec 2022 23:12:24 +0000 Subject: [PATCH 4/4] 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 () {