mirror of
https://github.com/thelounge/thelounge.git
synced 2024-05-29 03:42:20 +02:00
Revise suffixes and make them SI compliant
This commit is contained in:
parent
523834c403
commit
bb11548b59
|
@ -1,12 +1,11 @@
|
||||||
export default (count: number) => {
|
export default (count: number) => {
|
||||||
const suffixes = [
|
if (count < 1000) {
|
||||||
{divisor: 1, suffix: ""},
|
return count.toString();
|
||||||
{divisor: 1000, suffix: "k"},
|
}
|
||||||
{divisor: 1000000, suffix: "m"},
|
|
||||||
];
|
|
||||||
|
|
||||||
const {divisor, suffix} =
|
const suffixes = ["", "k", "M"];
|
||||||
suffixes[Math.min(suffixes.length - 1, Math.floor(Math.log10(count) / 3))];
|
const magnitudeIndex = Math.min(Math.floor(Math.log10(count) / 3), suffixes.length - 1);
|
||||||
|
const magnitude = 1000 ** magnitudeIndex;
|
||||||
return String(Math.ceil((count / divisor) * 10) / 10).concat(suffix);
|
const roundedCount = (count / magnitude).toFixed(1);
|
||||||
|
return roundedCount + suffixes[magnitudeIndex];
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,9 +10,9 @@ describe("roundBadgeNumber helper", function () {
|
||||||
expect(roundBadgeNumber(1000)).to.be.equal("1.0k");
|
expect(roundBadgeNumber(1000)).to.be.equal("1.0k");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return numbers above 999999 with a 'm' suffix", function () {
|
it("should return numbers above 999999 with a 'M' suffix", function () {
|
||||||
expect(roundBadgeNumber(1000000)).to.be.equal("1.0m");
|
expect(roundBadgeNumber(1000000)).to.be.equal("1.0M");
|
||||||
expect(roundBadgeNumber(1234567)).to.be.equal("1.2m");
|
expect(roundBadgeNumber(1234567)).to.be.equal("1.2M");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should round and not floor", function () {
|
it("should round and not floor", function () {
|
||||||
|
|
Loading…
Reference in a new issue