mirror of
https://github.com/thelounge/thelounge.git
synced 2024-06-15 20:15:11 +02:00
Merge bb11548b59
into 549c445853
This commit is contained in:
commit
0cf19ea886
|
@ -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);
|
||||
const magnitude = 1000 ** magnitudeIndex;
|
||||
const roundedCount = (count / magnitude).toFixed(1);
|
||||
return roundedCount + suffixes[magnitudeIndex];
|
||||
};
|
||||
|
|
|
@ -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");
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue