From 9b5339543fd890dc0b44d9f4b13b81dbd724cf89 Mon Sep 17 00:00:00 2001 From: Drew Goodwin Date: Fri, 24 Mar 2023 09:48:56 +0900 Subject: [PATCH] feat: BigInt support for useFormatter.format() (#222) --- packages/use-intl/src/core/createFormatter.tsx | 2 +- packages/use-intl/test/core/createFormatter.test.tsx | 12 ++++++++++++ packages/use-intl/test/react/useFormatter.test.tsx | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/use-intl/src/core/createFormatter.tsx b/packages/use-intl/src/core/createFormatter.tsx index 2bf0e54c9..890e6db0b 100644 --- a/packages/use-intl/src/core/createFormatter.tsx +++ b/packages/use-intl/src/core/createFormatter.tsx @@ -130,7 +130,7 @@ export default function createFormatter({ } function number( - value: number, + value: number | bigint, formatOrOptions?: string | NumberFormatOptions ) { return getFormattedValue( diff --git a/packages/use-intl/test/core/createFormatter.test.tsx b/packages/use-intl/test/core/createFormatter.test.tsx index ad9585749..101eefb7f 100644 --- a/packages/use-intl/test/core/createFormatter.test.tsx +++ b/packages/use-intl/test/core/createFormatter.test.tsx @@ -12,6 +12,18 @@ it('formats a date and time', () => { }); it('formats a number', () => { + expect(intl.number(123456)).toBe( + '123,456' + ); +}); + +it('formats a bigint', () => { + expect(intl.number(123456789123456n)).toBe( + '123,456,789,123,456' + ); +}); + +it('formats a number as currency', () => { expect(intl.number(123456.789, {style: 'currency', currency: 'USD'})).toBe( '$123,456.79' ); diff --git a/packages/use-intl/test/react/useFormatter.test.tsx b/packages/use-intl/test/react/useFormatter.test.tsx index 8dc4dab2f..bce99eff9 100644 --- a/packages/use-intl/test/react/useFormatter.test.tsx +++ b/packages/use-intl/test/react/useFormatter.test.tsx @@ -209,6 +209,11 @@ describe('number', () => { screen.getByText('2,948,192,329.123'); }); + it('formats a bigint', () => { + renderNumber(294819232912312); + screen.getByText('294,819,232,912,312'); + }); + it('accepts options', () => { renderNumber(299.99, {currency: 'EUR', style: 'currency'}); screen.getByText('€299.99');