Skip to content

Commit b77125a

Browse files
authored
feat: add generic number format (#6664)
## Explanation Add a generic `formatNumber` option to allow clients flexibility while using the formatting cache. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent d8da4c2 commit b77125a

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/assets-controllers/src/utils/formatters.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@ const invalidValues = [
88
Number.NEGATIVE_INFINITY,
99
];
1010

11+
describe('formatNumber', () => {
12+
const { formatNumber } = createFormatters({ locale });
13+
14+
it('formats a basic integer', () => {
15+
expect(formatNumber(1234)).toBe('1,234');
16+
});
17+
18+
it('respects fraction digit options', () => {
19+
expect(
20+
formatNumber(1.2345, {
21+
minimumFractionDigits: 2,
22+
maximumFractionDigits: 2,
23+
}),
24+
).toBe('1.23');
25+
});
26+
27+
it('returns empty string for invalid number', () => {
28+
expect(formatNumber(NaN)).toBe('');
29+
});
30+
});
31+
1132
describe('formatCurrency', () => {
1233
const { formatCurrency } = createFormatters({ locale });
1334

packages/assets-controllers/src/utils/formatters.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ function getCachedNumberFormat(
5151
return format;
5252
}
5353

54+
/**
55+
* Format a number with optional Intl overrides.
56+
*
57+
* @param config - Configuration object with locale.
58+
* @param config.locale - Locale string.
59+
* @param value - Numeric value to format.
60+
* @param options - Optional Intl.NumberFormat overrides.
61+
* @returns Formatted number string.
62+
*/
63+
function formatNumber(
64+
config: { locale: string },
65+
value: number | bigint | `${number}`,
66+
options: Intl.NumberFormatOptions = {},
67+
) {
68+
if (!Number.isFinite(Number(value))) {
69+
return '';
70+
}
71+
72+
const numberFormat = getCachedNumberFormat(config.locale, options);
73+
74+
// @ts-expect-error Remove this comment once TypeScript is updated to 5.5+
75+
return numberFormat.format(value);
76+
}
77+
5478
/**
5579
* Format a value as a currency string.
5680
*
@@ -260,6 +284,13 @@ function formatTokenQuantity(
260284
*/
261285
export function createFormatters({ locale = FALLBACK_LOCALE }) {
262286
return {
287+
/**
288+
* Format a number with optional Intl overrides.
289+
*
290+
* @param value - Numeric value to format.
291+
* @param options - Optional Intl.NumberFormat overrides.
292+
*/
293+
formatNumber: formatNumber.bind(null, { locale }),
263294
/**
264295
* Format a value as a currency string.
265296
*

0 commit comments

Comments
 (0)