Change formatCount impl to use Intl.NumberFormat (#11379)

* Change formatCount impl to use Intl.NumberFormat

* Update formatCount JSDoc description
This commit is contained in:
Germain 2023-08-08 12:45:20 +01:00 committed by GitHub
parent 033c600fa2
commit 84d196776e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 10 deletions

View file

@ -17,28 +17,33 @@ limitations under the License.
import { ReactElement, ReactNode } from "react"; import { ReactElement, ReactNode } from "react";
import { _t } from "../languageHandler"; import { _t, getCurrentLanguage } from "../languageHandler";
import { jsxJoin } from "./ReactUtils"; import { jsxJoin } from "./ReactUtils";
const locale = getCurrentLanguage();
// It's quite costly to instanciate `Intl.NumberFormat`, hence why we do not do
// it in every function call
const compactFormatter = new Intl.NumberFormat(locale, {
notation: "compact",
});
/** /**
* formats numbers to fit into ~3 characters, suitable for badge counts * formats and rounds numbers to fit into ~3 characters, suitable for badge counts
* e.g: 999, 9.9K, 99K, 0.9M, 9.9M, 99M, 0.9B, 9.9B * e.g: 999, 10K, 99K, 1M, 10M, 99M, 1B, 10B, ...
*/ */
export function formatCount(count: number): string { export function formatCount(count: number): string {
if (count < 1000) return count.toString(); return compactFormatter.format(count);
if (count < 10000) return (count / 1000).toFixed(1) + "K";
if (count < 100000) return (count / 1000).toFixed(0) + "K";
if (count < 10000000) return (count / 1000000).toFixed(1) + "M";
if (count < 100000000) return (count / 1000000).toFixed(0) + "M";
return (count / 1000000000).toFixed(1) + "B"; // 10B is enough for anyone, right? :S
} }
// It's quite costly to instanciate `Intl.NumberFormat`, hence why we do not do
// it in every function call
const formatter = new Intl.NumberFormat(locale);
/** /**
* Format a count showing the whole number but making it a bit more readable. * Format a count showing the whole number but making it a bit more readable.
* e.g: 1000 => 1,000 * e.g: 1000 => 1,000
*/ */
export function formatCountLong(count: number): string { export function formatCountLong(count: number): string {
const formatter = new Intl.NumberFormat();
return formatter.format(count); return formatter.format(count);
} }

View file

@ -0,0 +1,42 @@
/*
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { formatCount, formatCountLong } from "../../src/utils/FormattingUtils";
jest.mock("../../src/dispatcher/dispatcher");
describe("FormattingUtils", () => {
describe("formatCount", () => {
it.each([
{ count: 999, expectedCount: "999" },
{ count: 9999, expectedCount: "10K" },
{ count: 99999, expectedCount: "100K" },
{ count: 999999, expectedCount: "1M" },
{ count: 9999999, expectedCount: "10M" },
{ count: 99999999, expectedCount: "100M" },
{ count: 999999999, expectedCount: "1B" },
{ count: 9999999999, expectedCount: "10B" },
])("formats $count as $expectedCount", ({ count, expectedCount }) => {
expect(formatCount(count)).toBe(expectedCount);
});
});
describe("formatCountLong", () => {
it("formats numbers according to the locale", () => {
expect(formatCountLong(1000)).toBe("1,000");
});
});
});