From a0700cdfe3fb01808d0fe22155708ec124df4662 Mon Sep 17 00:00:00 2001 From: Anders Date: Tue, 29 Oct 2024 21:08:42 +0100 Subject: [PATCH] fix(NumberFormat): should not throw exception when providing an invalid currency (#4192) --- .../dnb-eufemia/src/components/number-format/NumberUtils.js | 2 +- .../components/number-format/__tests__/NumberUtils.test.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/dnb-eufemia/src/components/number-format/NumberUtils.js b/packages/dnb-eufemia/src/components/number-format/NumberUtils.js index 2bab75c1259..9dab14611db 100644 --- a/packages/dnb-eufemia/src/components/number-format/NumberUtils.js +++ b/packages/dnb-eufemia/src/components/number-format/NumberUtils.js @@ -486,7 +486,7 @@ const prepareMinus = (display, locale) => { * @returns {string} The aligned output string. */ function alignCurrencySymbol(output, currencyDisplay) { - if (currencyDisplay === 'name') { + if (typeof output === 'string' && currencyDisplay === 'name') { output = output.replace(/(nor[^\s]+?)\s(\w+)/i, '$2') } return output diff --git a/packages/dnb-eufemia/src/components/number-format/__tests__/NumberUtils.test.ts b/packages/dnb-eufemia/src/components/number-format/__tests__/NumberUtils.test.ts index 59768562039..2f7d2e3b008 100644 --- a/packages/dnb-eufemia/src/components/number-format/__tests__/NumberUtils.test.ts +++ b/packages/dnb-eufemia/src/components/number-format/__tests__/NumberUtils.test.ts @@ -109,6 +109,10 @@ describe('Decimals format', () => { decimals: undefined, }) ).toBe('-1,1234567891234568') + expect(format(null, { currency: 'non-valid value' })).toBe('null') + expect(format(undefined, { currency: 'non-valid value' })).toBe( + 'undefined' + ) }) describe('rounding', () => {