mirror of
https://github.com/thelounge/thelounge.git
synced 2024-06-15 20:15:11 +02:00
Use "m" shorthand for messages >= one million
Implements [Unread messages count] Abandon "k" (= thousand) when it reaches million+ #4615
This commit is contained in:
parent
15edee4bf9
commit
4cfba05c56
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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