From ee05aa7cada13045fbacc3724d65e7d5487a954c Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Thu, 1 Aug 2024 10:59:45 -0400 Subject: [PATCH 1/4] Ensure the currency is specified. --- src/lib/intlFormat.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/intlFormat.ts b/src/lib/intlFormat.ts index 79632cfad..c012638c6 100644 --- a/src/lib/intlFormat.ts +++ b/src/lib/intlFormat.ts @@ -19,9 +19,12 @@ export const currencyFormat = ( ): string => { const amount = Number.isNaN(value) ? 0 : value; const decimal = amount % 1 !== 0; + if (!currency) { + currency = 'USD'; + } return new Intl.NumberFormat(locale, { style: 'currency', - currency: currency ?? 'USD', + currency: currency, minimumFractionDigits: decimal ? 2 : 0, maximumFractionDigits: decimal ? 2 : 0, }).format(Number.isFinite(amount) ? amount : 0); From 3c39a9cdc21315b5d58049e6f89f7c2229b7254d Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Thu, 1 Aug 2024 11:00:15 -0400 Subject: [PATCH 2/4] Return something rather than error and show white screen of death --- src/lib/intlFormat.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib/intlFormat.ts b/src/lib/intlFormat.ts index c012638c6..4ef35315d 100644 --- a/src/lib/intlFormat.ts +++ b/src/lib/intlFormat.ts @@ -22,12 +22,18 @@ export const currencyFormat = ( if (!currency) { currency = 'USD'; } - return new Intl.NumberFormat(locale, { - style: 'currency', + try { + return new Intl.NumberFormat(locale, { + style: 'currency', currency: currency, - minimumFractionDigits: decimal ? 2 : 0, - maximumFractionDigits: decimal ? 2 : 0, - }).format(Number.isFinite(amount) ? amount : 0); + minimumFractionDigits: decimal ? 2 : 0, + maximumFractionDigits: decimal ? 2 : 0, + }).format(Number.isFinite(amount) ? amount : 0); + } catch (error) { + // eslint-disable-next-line no-console + console.error(`Error formatting currency: ${error}`); + return `${amount} ${currency}`; + } }; export const dayMonthFormat = ( From 6ceaf0ef3acd995239a6838cbabb5694d31f9553 Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Thu, 1 Aug 2024 12:05:20 -0400 Subject: [PATCH 3/4] Adding tests --- src/lib/intlFormat.test.ts | 7 +++++++ src/lib/intlFormat.ts | 1 + 2 files changed, 8 insertions(+) diff --git a/src/lib/intlFormat.test.ts b/src/lib/intlFormat.test.ts index f38bd8668..f98a47bc1 100644 --- a/src/lib/intlFormat.test.ts +++ b/src/lib/intlFormat.test.ts @@ -82,6 +82,13 @@ describe('intlFormat', () => { it('handles undefined case', () => { expect(currencyFormat(1000, undefined, 'en-US')).toEqual('$1,000'); }); + it('handles empty string case', () => { + expect(currencyFormat(1234.56, '', 'en-GB')).toEqual('US$1,234.56'); + }); + + it('handles an error', () => { + expect(currencyFormat(1234.56, ' ', 'en-GB')).toEqual('1234.56 '); + }); }); describe('different language', () => { diff --git a/src/lib/intlFormat.ts b/src/lib/intlFormat.ts index 4ef35315d..1e6529214 100644 --- a/src/lib/intlFormat.ts +++ b/src/lib/intlFormat.ts @@ -22,6 +22,7 @@ export const currencyFormat = ( if (!currency) { currency = 'USD'; } + window.Intl; try { return new Intl.NumberFormat(locale, { style: 'currency', From 7c2846c0ff52062326ee9b40dc6ef756f2016372 Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Fri, 2 Aug 2024 10:57:47 -0400 Subject: [PATCH 4/4] remove unwanted code --- src/lib/intlFormat.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/intlFormat.ts b/src/lib/intlFormat.ts index 1e6529214..4ef35315d 100644 --- a/src/lib/intlFormat.ts +++ b/src/lib/intlFormat.ts @@ -22,7 +22,6 @@ export const currencyFormat = ( if (!currency) { currency = 'USD'; } - window.Intl; try { return new Intl.NumberFormat(locale, { style: 'currency',