From f658d8efb4285408a5d0e3f861b184d9ce0d6f5f Mon Sep 17 00:00:00 2001 From: Pierre-Nicolas Watin-Augouard Date: Thu, 11 Sep 2025 13:23:17 +0200 Subject: [PATCH] chore: allow string or number amount in format price --- src/currency/index.spec.ts | 28 +++++++++++++++++++++------- src/currency/index.ts | 4 ++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/currency/index.spec.ts b/src/currency/index.spec.ts index 76f61be..306cd79 100644 --- a/src/currency/index.spec.ts +++ b/src/currency/index.spec.ts @@ -3,50 +3,64 @@ import { formatPrice } from './index'; import type { Currency } from './index'; describe('formatPrice', () => { + test('should format price in the same way with a numeric or string amount', () => { + const moneyString = { + amount: '1234.56', + currencyCode: 'EUR', + } satisfies Currency; + const moneyNumber = { + amount: 1234.56, + currencyCode: 'EUR', + } satisfies Currency; + const resultString = formatPrice(moneyString); + const resultNumber = formatPrice(moneyNumber); + + expect(resultString).toBe(resultNumber); + }); test('should format price with default locale and cents enabled', () => { - const money = { amount: '1234.56', currencyCode: 'EUR' } satisfies Currency; + const money = { amount: 1234.56, currencyCode: 'EUR' } satisfies Currency; const result = formatPrice(money); expect(result).toBe('1 234,56 €'); }); test('should format price with custom locale and cents enabled', () => { - const money = { amount: '1234.56', currencyCode: 'USD' } satisfies Currency; + const money = { amount: 1234.56, currencyCode: 'USD' } satisfies Currency; const result = formatPrice(money, 'en-US'); expect(result).toBe('$1,234.56'); }); test('should format price with quantity multiplier', () => { - const money = { amount: '1234.56', currencyCode: 'USD' } satisfies Currency; + const money = { amount: 1234.56, currencyCode: 'USD' } satisfies Currency; const result = formatPrice(money, 'en-US', 2); expect(result).toBe('$2,469.12'); }); test('should format price with cents disabled when applicable', () => { - const money = { amount: '1234.00', currencyCode: 'USD' } satisfies Currency; + const money = { amount: 1234, currencyCode: 'USD' } satisfies Currency; const result = formatPrice(money, 'en-US', 1, true); expect(result).toBe('$1,234'); }); test('should still include cents if the amount has fractional part even if disableCents is true', () => { - const money = { amount: '1234.56', currencyCode: 'USD' } satisfies Currency; + const money = { amount: 1234.56, currencyCode: 'USD' } satisfies Currency; const result = formatPrice(money, 'en-US', 1, true); expect(result).toBe('$1,234.56'); }); test('should format price correctly with a different currency code', () => { - const money = { amount: '1234.56', currencyCode: 'GBP' } satisfies Currency; + const money = { amount: 1234.56, currencyCode: 'GBP' } satisfies Currency; const result = formatPrice(money, 'en-GB'); expect(result).toBe('£1,234.56'); }); test('should handle large quantities correctly', () => { - const money = { amount: '1000.00', currencyCode: 'EUR' } satisfies Currency; + const money = { amount: 1000, currencyCode: 'EUR' } satisfies Currency; const result = formatPrice(money, 'fr', 100); expect(result).toBe('100 000,00 €'); diff --git a/src/currency/index.ts b/src/currency/index.ts index 9b8994f..5438e62 100644 --- a/src/currency/index.ts +++ b/src/currency/index.ts @@ -1,7 +1,7 @@ import type { CurrencyCode } from './types'; export type Currency = { - amount: string; + amount: string | number; currencyCode: CurrencyCode; }; @@ -11,7 +11,7 @@ export const formatPrice = ( quantity = 1, disableCents = false, ): string => { - const price = Number.parseFloat(amount); + const price = typeof amount === 'number' ? amount : Number.parseFloat(amount); return new Intl.NumberFormat(locale, { style: 'currency',