diff --git a/.changeset/slimy-rules-thank.md b/.changeset/slimy-rules-thank.md new file mode 100644 index 00000000..6d6277c3 --- /dev/null +++ b/.changeset/slimy-rules-thank.md @@ -0,0 +1,5 @@ +--- +"@razorpay/i18nify-js": patch +--- + +currency symbol placement evaluation in formatNumberToParts diff --git a/packages/i18nify-js/src/modules/currency/__tests__/formatNumberByParts.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/formatNumberByParts.test.ts index 54cf12f2..a188e5be 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/formatNumberByParts.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/formatNumberByParts.test.ts @@ -237,4 +237,30 @@ describe('formatNumberByParts', () => { ), ); }); + + it('should return correct value for isPrefixSymbol for negative amounts', () => { + const inrParts = formatNumberByParts(-1234567, { + currency: 'INR', + locale: 'en-IN', + intlOptions: { style: 'currency' }, + } as any); + + const eurParts = formatNumberByParts(-1234567, { + currency: 'EUR', + locale: 'de-DE', + intlOptions: { style: 'currency' }, + } as any); + + expect(inrParts.isPrefixSymbol).toEqual(true); + expect(eurParts.isPrefixSymbol).toEqual(false); + + expect(inrParts.minusSign).toEqual('-'); + expect(eurParts.minusSign).toEqual('-'); + }); + + it('should return true as default value of isPrefixSymbol even when currency code is not passed', () => { + expect(formatNumberByParts(-1234567, {} as any).isPrefixSymbol).toEqual( + true, + ); + }); }); diff --git a/packages/i18nify-js/src/modules/currency/formatNumberByParts.ts b/packages/i18nify-js/src/modules/currency/formatNumberByParts.ts index 5e30aabf..50b67b02 100644 --- a/packages/i18nify-js/src/modules/currency/formatNumberByParts.ts +++ b/packages/i18nify-js/src/modules/currency/formatNumberByParts.ts @@ -43,7 +43,9 @@ const formatNumberByParts = ( return { ...formattedObj, - isPrefixSymbol: parts[0].type === 'currency', + isPrefixSymbol: + parts.findIndex((item) => item.type === 'currency') < + parts.findIndex((item) => item.type === 'integer'), rawParts: parts, }; } catch (err) {