Skip to content

Commit

Permalink
Add ShowCurrency option
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Jun 9, 2024
1 parent 36b7c66 commit eafc295
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
65 changes: 47 additions & 18 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@ import { truncate } from './truncate';

export type FormatTypes = 'money' | 'percentage' | 'date' | 'decimal' | 'number' | 'days' | 'months';

const defaultLocate = 'en-US';
const defaultCurrency = 'USD';

const defaultOptions = {
keepFormat: false,
decimals: 2,
nullValue: 'N/A',
ignoreUndefined: false,
locale: 'en-US',
currency: 'USD',
locale: defaultLocate,
currency: defaultCurrency,
showCurrency: false,
} as const;

const formatMoney = (stringData: string, nullValue: string, locale: string, currency: string) => {
const formatMoney = (
stringData: string,
nullValue: string,
locale: string,
currency: string,
showCurrency: boolean,
) => {
const parsedMoney = parseFloat(stringData);
return !parsedMoney
const amount = !parsedMoney
? nullValue
: new Intl.NumberFormat(locale, { style: 'currency', currency }).format(truncate(parsedMoney, 100000));

return showCurrency ? `${amount} ${currency}` : amount;
};

const formatDays = (stringData: string) => (Number(stringData) === 1 ? '1 day' : `${stringData} days`);
Expand All @@ -38,12 +50,20 @@ const internalFotmatter = (
nullValue,
locale,
currency,
}: { keepFormat: boolean; decimals: number; nullValue: string; locale: string; currency: string },
showCurrency,
}: {
keepFormat: boolean;
decimals: number;
nullValue: string;
locale: string;
currency: string;
showCurrency: boolean;
},
format?: FormatTypes,
): string => {
switch (format) {
case 'money':
return formatMoney(stringData, nullValue, locale, currency);
return formatMoney(stringData, nullValue, locale, currency, showCurrency);
case 'percentage':
return formatPercentage(stringData, decimals);
case 'number':
Expand Down Expand Up @@ -74,26 +94,35 @@ export const formatter = (
ignoreUndefined?: boolean;
locale?: string;
currency?: string;
showCurrency?: boolean;
},
): string | undefined => {
if (isMoneyObject(data)) return formatter(data.Amount, 'money', { ...options, currency: data.Currency });

const { keepFormat, decimals, nullValue, ignoreUndefined, locale, currency } = { ...defaultOptions, ...options };
if (isMoneyObject(data))
return formatter(data.Amount, 'money', { ...defaultOptions, ...options, currency: data.Currency });

const { keepFormat, decimals, nullValue, ignoreUndefined, locale, currency, showCurrency } = {
...defaultOptions,
...options,
};
if (data === undefined && !ignoreUndefined) return undefined;

return data == null
? nullValue
: internalFotmatter(String(data), { keepFormat, decimals, nullValue, locale, currency }, format);
: internalFotmatter(String(data), { keepFormat, decimals, nullValue, locale, currency, showCurrency }, format);
};

export const toMoney = (data: unknown, options?: { locale?: string; currency?: string }) =>
{
const defaultOptions = { ...options, nullValue: '$0.00' };

if (isMoneyObject(data))
return formatter(data.Amount, 'money', {...defaultOptions, currency: data.Currency});

return formatter(data, 'money', defaultOptions);
export const toMoney = (data: unknown, options?: { locale?: string; currency?: string; showCurrency?: boolean }) => {
const defaultOptions = {
...options,
nullValue: new Intl.NumberFormat(options?.locale ?? defaultLocate, {
style: 'currency',
currency: options?.currency ?? defaultCurrency,
}).format(0),
};

if (isMoneyObject(data)) return formatter(data.Amount, 'money', { ...defaultOptions, currency: data.Currency });

return formatter(data, 'money', defaultOptions);
};

export const toPercentage = (data: unknown, options?: { decimals?: number }) => formatter(data, 'percentage', options);
28 changes: 24 additions & 4 deletions test/formatter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { formatter, toMoney, toPercentage } from '../src/formatter';

describe('formatter', () => {
const number = 90;
describe('toMoney', () => {
const zero = 0;
const million = 1000000;
const stringValue = 'Money';

it('should return formatted million money', () => {
const id = toMoney(million);
Expand All @@ -19,9 +17,31 @@ describe('formatter', () => {
expect(id).toBe('$0.00');
});
it('should return $0.00 on money', () => {
const id = toMoney(stringValue);
const id = toMoney('Money');
expect(id).toBe('$0.00');
});
it('should return currency with options and currency', () => {
const id = toMoney(million, { currency: 'USD', showCurrency: true });
expect(id).toBe('$1,000,000.00 USD');
});
it('should return currency with options and default currency', () => {
const id = toMoney(million, { showCurrency: true });
expect(id).toBe('$1,000,000.00 USD');
});
it('should return euro', () => {
const id = toMoney(million, { currency: 'EUR' });
expect(id).toBe('€1,000,000.00');
});
it('should return zero euro with null', () => {
const id = toMoney(null, { currency: 'EUR' });
expect(id).toBe('€0.00');
});
});

describe('formatter', () => {
const number = 90;
const stringValue = 'Money';

it('should return 90%', () => {
const id = toPercentage(number, { decimals: 0 });
expect(id).toBe('90%');
Expand Down

0 comments on commit eafc295

Please sign in to comment.