Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "M" shorthand for messages >= one million #4659

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion client/js/helpers/roundBadgeNumber.ts
Expand Up @@ -3,5 +3,9 @@ export default (count: number) => {
return count.toString();
}

return (count / 1000).toFixed(2).slice(0, -1) + "k";
const suffixes = ["", "k", "M"];
const magnitudeIndex = Math.min(Math.floor(Math.log10(count) / 3), suffixes.length - 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I understand the use case here, I think it's simpler to just have two if cases to handle M/k.

if(count >= 1000000) M
if(count >= 1000) k
count

const magnitude = 1000 ** magnitudeIndex;
const roundedCount = (count / magnitude).toFixed(1);
return roundedCount + suffixes[magnitudeIndex];
};
7 changes: 6 additions & 1 deletion test/client/js/helpers/roundBadgeNumberTest.ts
Expand Up @@ -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 () {
Copy link
Member

@xPaw xPaw Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a few more expects, e.g. 999999 is in fact 999k

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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for much higher magnitude number (with current code it would validate that it magnitudeIndex works` as expected.

});

it("should round and not floor", function () {
expect(roundBadgeNumber(9999)).to.be.equal("10.0k");
});
Expand Down