refactor: replace millify with Intl compact formatter

This commit is contained in:
hazre committed 2026-03-25 15:26:32 +01:00
1 parent 514535ca35
commit b7f9b851ff
9 files changed
+109 -116

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
default: patch
---
Use the browser's native compact number formatting for room and member counts.
+2 -3
View File
@@ -34,9 +34,9 @@
"@atlaskit/pragmatic-drag-and-drop-auto-scroll": "^1.4.0",
"@atlaskit/pragmatic-drag-and-drop-hitbox": "^1.1.0",
"@fontsource-variable/nunito": "5.2.7",
"@sentry/react": "^10.43.0",
"@fontsource/space-mono": "5.2.9",
"@phosphor-icons/react": "^2.1.10",
"@sentry/react": "^10.43.0",
"@tanstack/react-query": "^5.90.21",
"@tanstack/react-query-devtools": "^5.91.3",
"@tanstack/react-virtual": "^3.13.19",
@@ -72,7 +72,6 @@
"linkifyjs": "^4.3.2",
"matrix-js-sdk": "^38.4.0",
"matrix-widget-api": "^1.16.1",
"millify": "^6.1.0",
"pdfjs-dist": "^5.4.624",
"prismjs": "^1.30.0",
"react": "^18.3.1",
@@ -96,12 +95,12 @@
},
"devDependencies": {
"@cloudflare/vite-plugin": "^1.26.0",
"@sableclient/sable-call-embedded": "v1.1.4",
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@eslint/compat": "2.0.2",
"@eslint/js": "9.39.3",
"@rollup/plugin-inject": "^5.0.5",
"@rollup/plugin-wasm": "^6.2.2",
"@sableclient/sable-call-embedded": "v1.1.4",
"@sentry/vite-plugin": "^5.1.1",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.2",
-98
View File
@@ -142,9 +142,6 @@ importers:
matrix-widget-api:
specifier: ^1.16.1
version: 1.17.0
millify:
specifier: ^6.1.0
version: 6.1.0
pdfjs-dist:
specifier: ^5.4.624
version: 5.5.207
@@ -3320,10 +3317,6 @@ packages:
[email protected]:
resolution: {integrity: sha512-saHYOzhIQs6wy2sVxTM6bUDsQO4F50V9RQ22qBpEdCW+I+/Wmke2HOl6lS6dTpdxVhb88/I6+Hs+438c3lfUow==}
[email protected]:
resolution: {integrity: sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==}
engines: {node: '>=12'}
[email protected]:
resolution: {integrity: sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==}
engines: {node: '>=6'}
@@ -3530,9 +3523,6 @@ packages:
[email protected]:
resolution: {integrity: sha512-5z3uFKBWjiNR44nFcYdkcXjKMbg5KXNdciu7mhTPo9tB7NbqSNP2sSnGR+fqknZSCwKkBN+oxiiajWs4dT6ORg==}
[email protected]:
resolution: {integrity: sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==}
[email protected]:
resolution: {integrity: sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==}
@@ -3960,10 +3950,6 @@ packages:
resolution: {integrity: sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==}
engines: {node: '>=6.9.0'}
[email protected]:
resolution: {integrity: sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==}
engines: {node: 6.* || 8.* || >= 10.*}
[email protected]:
resolution: {integrity: sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==}
engines: {node: '>= 0.4'}
@@ -4199,10 +4185,6 @@ packages:
resolution: {integrity: sha512-1pC6N8qWJbWoPtEjgcL2xyhQOP491EQjeUo3qTKcmV8YSDDJrOepfG8pcC7h/QgnQHYSv0mJ3Z/ZWxmatVrysg==}
engines: {node: '>= 0.4'}
[email protected]:
resolution: {integrity: sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==}
engines: {node: '>=8'}
[email protected]:
resolution: {integrity: sha512-upqt1SkGkODW9tsGNG5mtXTXtECizwtS2kA161M+gJPc1xdb/Ax629af6YrTwcOeQHbewrPNlE5Dx7kzvXTizA==}
engines: {node: '>= 0.4'}
@@ -4542,10 +4524,6 @@ packages:
resolution: {integrity: sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==}
engines: {node: '>=8.6'}
[email protected]:
resolution: {integrity: sha512-H/E3J6t+DQs/F2YgfDhxUVZz/dF8JXPPKTLHL/yHCcLZLtCXJDUaqvhJXQwqOVBvbyNn4T0WjLpIHd7PAw7fBA==}
hasBin: true
[email protected]:
resolution: {integrity: sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==}
engines: {node: '>=4'}
@@ -4931,10 +4909,6 @@ packages:
resolution: {integrity: sha512-NZQZdC5wOE/H3UT28fVGL+ikOZcEzfMGk/c3iN9UGxzWHMa1op7274oyiUVrAG4B2EuFhus8SvkaYnhvW92p9Q==}
hasBin: true
[email protected]:
resolution: {integrity: sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==}
engines: {node: '>=0.10.0'}
[email protected]:
resolution: {integrity: sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==}
engines: {node: '>=0.10.0'}
@@ -5130,10 +5104,6 @@ packages:
resolution: {integrity: sha512-eLoXW/DHyl62zxY4SCaIgnRhuMr6ri4juEYARS8E6sCEqzKpOiE521Ucofdx+KnDZl5xmvGYaaKCk5FEOxJCoQ==}
engines: {node: '>= 0.4'}
[email protected]:
resolution: {integrity: sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==}
engines: {node: '>=8'}
[email protected]:
resolution: {integrity: sha512-o7+c9bW6zpAdJHTtujeePODAhkuicdAryFsfVKwA+wGw89wJ4GTY484WTucM9hLtDEOpOvI+aHnzqnC5lHp4Rg==}
engines: {node: '>= 0.4'}
@@ -5161,10 +5131,6 @@ packages:
resolution: {integrity: sha512-rHqiFh1elqCQ9WPLIC8I0Q/g/wj5J1eMkyoiD6eoQApWHP0FtlK7rqnhmabL5VUY9JQCcqwwvlOaSuutekgyrw==}
engines: {node: '>=4'}
[email protected]:
resolution: {integrity: sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==}
engines: {node: '>=8'}
[email protected]:
resolution: {integrity: sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==}
engines: {node: '>=4'}
@@ -5678,10 +5644,6 @@ packages:
'@cloudflare/workers-types':
optional: true
[email protected]:
resolution: {integrity: sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==}
engines: {node: '>=10'}
[email protected]:
resolution: {integrity: sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==}
engines: {node: '>=10.0.0'}
@@ -5701,10 +5663,6 @@ packages:
[email protected]:
resolution: {integrity: sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==}
[email protected]:
resolution: {integrity: sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==}
engines: {node: '>=10'}
[email protected]:
resolution: {integrity: sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==}
@@ -5713,14 +5671,6 @@ packages:
engines: {node: '>= 14.6'}
hasBin: true
[email protected]:
resolution: {integrity: sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==}
engines: {node: '>=12'}
[email protected]:
resolution: {integrity: sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==}
engines: {node: '>=12'}
[email protected]:
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
engines: {node: '>=10'}
@@ -9132,12 +9082,6 @@ snapshots:
[email protected]: {}
[email protected]:
dependencies:
string-width: 4.2.3
strip-ansi: 6.0.1
wrap-ansi: 7.0.0
[email protected]: {}
[email protected]:
@@ -9314,8 +9258,6 @@ snapshots:
[email protected]: {}
[email protected]: {}
[email protected]: {}
[email protected]([email protected]):
@@ -9909,8 +9851,6 @@ snapshots:
[email protected]: {}
[email protected]: {}
[email protected]:
dependencies:
call-bind-apply-helpers: 1.0.2
@@ -10177,8 +10117,6 @@ snapshots:
dependencies:
call-bound: 1.0.4
[email protected]: {}
[email protected]:
dependencies:
call-bound: 1.0.4
@@ -10511,10 +10449,6 @@ snapshots:
braces: 3.0.3
picomatch: 2.3.1
[email protected]:
dependencies:
yargs: 17.7.2
[email protected]: {}
[email protected]:
@@ -10952,8 +10886,6 @@ snapshots:
dependencies:
jsesc: 3.1.0
[email protected]: {}
[email protected]: {}
[email protected]: {}
@@ -11225,12 +11157,6 @@ snapshots:
es-errors: 1.3.0
internal-slot: 1.1.0
[email protected]:
dependencies:
emoji-regex: 8.0.0
is-fullwidth-code-point: 3.0.0
strip-ansi: 6.0.1
[email protected]:
dependencies:
call-bind: 1.0.8
@@ -11287,10 +11213,6 @@ snapshots:
is-obj: 1.0.1
is-regexp: 1.0.0
[email protected]:
dependencies:
ansi-regex: 5.0.1
[email protected]: {}
[email protected]: {}
@@ -11884,37 +11806,17 @@ snapshots:
- bufferutil
- utf-8-validate
[email protected]:
dependencies:
ansi-styles: 4.3.0
string-width: 4.2.3
strip-ansi: 6.0.1
[email protected]: {}
[email protected]: {}
[email protected]: {}
[email protected]: {}
[email protected]: {}
[email protected]:
optional: true
[email protected]: {}
[email protected]:
dependencies:
cliui: 8.0.1
escalade: 3.2.0
get-caller-file: 2.0.5
require-directory: 2.1.1
string-width: 4.2.3
y18n: 5.0.8
yargs-parser: 21.1.1
[email protected]: {}
[email protected]:
+2 -2
View File
@@ -21,7 +21,6 @@ import classNames from 'classnames';
import FocusTrap from 'focus-trap-react';
import { getMxIdLocalPart, mxcUrlToHttp } from '$utils/matrix';
import { nameInitials } from '$utils/common';
import { millify } from '$plugins/millify';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
import { onEnterOrSpace, stopPropagation } from '$utils/keyboard';
@@ -32,6 +31,7 @@ import { getRoomAvatarUrl, getStateEvent } from '$utils/room';
import { useStateEventCallback } from '$hooks/useStateEventCallback';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { RoomAvatar } from '$components/room-avatar';
import { formatCompactNumber } from '$utils/formatCompactNumber';
import * as css from './style.css';
type GridColumnCount = '1' | '2' | '3';
@@ -256,7 +256,7 @@ export const RoomCard = as<'div', RoomCardProps>(
{typeof joinedMemberCount === 'number' && (
<Box gap="100">
<Icon size="50" src={Icons.User} />
<Text size="T200">{`${millify(joinedMemberCount)} Members`}</Text>
<Text size="T200">{`${formatCompactNumber(joinedMemberCount)} Members`}</Text>
</Box>
)}
{typeof joinedRoomId === 'string' && (
+5 -2
View File
@@ -24,7 +24,6 @@ import { RoomAvatar, RoomIcon } from '$components/room-avatar';
import { SequenceCard } from '$components/sequence-card';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { HierarchyItem } from '$hooks/useSpaceHierarchy';
import { millify } from '$plugins/millify';
import { LocalRoomSummaryLoader } from '$components/RoomSummaryLoader';
import { UseStateProvider } from '$components/UseStateProvider';
import { RoomTopicViewer } from '$components/room-topic-viewer';
@@ -34,6 +33,7 @@ import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
import { getDirectRoomAvatarUrl, getRoomAvatarUrl } from '$utils/room';
import { mxcUrlToHttp } from '$utils/matrix';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { formatCompactNumber } from '$utils/formatCompactNumber';
import { ItemDraggableTarget, useDraggableItem } from './DnD';
import * as styleCss from './style.css';
import * as css from './RoomItem.css';
@@ -220,7 +220,10 @@ function RoomProfile({
<Box gap="200" alignItems="Center">
{memberCount && (
<Box shrink="No" gap="200">
<Text size="T200" priority="300">{`${millify(memberCount)} Members`}</Text>
<Text
size="T200"
priority="300"
>{`${formatCompactNumber(memberCount)} Members`}</Text>
</Box>
)}
{memberCount && topic && (
+2 -2
View File
@@ -44,7 +44,6 @@ import { useSetSetting, useSetting } from '$state/hooks/settings';
import { settingsAtom } from '$state/settings';
import { useAtomValue } from 'jotai';
import { nicknamesAtom } from '$state/nicknames';
import { millify } from '$plugins/millify';
import { ScrollTopContainer } from '$components/scroll-top-container';
import { UserAvatar } from '$components/user-avatar';
import { useRoomTypingMember } from '$hooks/useRoomTypingMembers';
@@ -60,6 +59,7 @@ import { ContainerColor } from '$styles/ContainerColor.css';
import { useFlattenPowerTagMembers, useGetMemberPowerTag } from '$hooks/useMemberPowerTag';
import { useRoomCreators } from '$hooks/useRoomCreators';
import { useSableCosmetics } from '$hooks/useSableCosmetics';
import { formatCompactNumber } from '$utils/formatCompactNumber';
import * as css from './MembersDrawer.css';
type MemberDrawerHeaderProps = {
@@ -73,7 +73,7 @@ function MemberDrawerHeader({ room }: MemberDrawerHeaderProps) {
<Box grow="Yes" alignItems="Center" gap="200">
<Box grow="Yes" alignItems="Center" gap="200">
<Text title={`${room.getJoinedMemberCount()} Members`} size="H5" truncate>
{`${millify(room.getJoinedMemberCount())} Members`}
{`${formatCompactNumber(room.getJoinedMemberCount())} Members`}
</Text>
</Box>
<Box shrink="No" alignItems="Center">
-9
View File
@@ -1,9 +0,0 @@
import millifyPlugin from 'millify';
import { MillifyOptions } from 'millify/dist/options';
export const millify = (count: number, options?: Partial<MillifyOptions>): string =>
millifyPlugin(count, {
precision: 1,
locales: [],
...options,
});
+71
View File
@@ -0,0 +1,71 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { formatCompactNumber } from './formatCompactNumber';
afterEach(() => {
vi.restoreAllMocks();
});
describe('formatCompactNumber', () => {
it('uses navigator.languages by default', () => {
const originalLanguages = navigator.languages;
const format = vi.fn().mockReturnValue('formatted');
const numberFormat = vi
.spyOn(Intl, 'NumberFormat')
.mockImplementation(function MockNumberFormat() {
return {
format,
} as unknown as Intl.NumberFormat;
} as typeof Intl.NumberFormat);
Object.defineProperty(navigator, 'languages', {
configurable: true,
value: ['fr-FR'],
});
try {
expect(formatCompactNumber(1_500)).toBe('formatted');
expect(numberFormat).toHaveBeenCalledWith(['fr-FR'], {
notation: 'compact',
maximumFractionDigits: 1,
});
expect(format).toHaveBeenCalledWith(1_500);
} finally {
Object.defineProperty(navigator, 'languages', {
configurable: true,
value: originalLanguages,
});
}
});
it('passes explicit locales and options to Intl.NumberFormat', () => {
const format = vi.fn().mockReturnValue('formatted');
const numberFormat = vi
.spyOn(Intl, 'NumberFormat')
.mockImplementation(function MockNumberFormat() {
return {
format,
} as unknown as Intl.NumberFormat;
} as typeof Intl.NumberFormat);
expect(
formatCompactNumber(1_250_000, {
locales: ['fr-FR'],
maximumFractionDigits: 2,
})
).toBe('formatted');
expect(numberFormat).toHaveBeenCalledWith(['fr-FR'], {
notation: 'compact',
maximumFractionDigits: 2,
});
expect(format).toHaveBeenCalledWith(1_250_000);
});
it('uses Intl compact notation for formatted output', () => {
const expected = new Intl.NumberFormat(['fr-FR'], {
notation: 'compact',
maximumFractionDigits: 1,
}).format(1_250_000);
expect(formatCompactNumber(1_250_000, { locales: ['fr-FR'] })).toBe(expected);
});
});
+22
View File
@@ -0,0 +1,22 @@
type FormatCompactNumberOptions = {
locales?: Intl.LocalesArgument;
maximumFractionDigits?: number;
};
const DEFAULT_MAXIMUM_FRACTION_DIGITS = 1;
const getDefaultLocales = (): Intl.LocalesArgument | undefined =>
typeof navigator === 'undefined' ? undefined : navigator.languages;
export function formatCompactNumber(
value: number,
{
locales = getDefaultLocales(),
maximumFractionDigits = DEFAULT_MAXIMUM_FRACTION_DIGITS,
}: FormatCompactNumberOptions = {}
): string {
return new Intl.NumberFormat(locales, {
notation: 'compact',
maximumFractionDigits,
}).format(value);
}