Skip to content

Commit

Permalink
feat: replace part helpers with getFormat and getCurrencyFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed May 12, 2020
1 parent e8e4731 commit c2a5946
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 187 deletions.
200 changes: 200 additions & 0 deletions src/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Format to parts currency should format 123456.789 for 'PT' 1`] = `
Array [
Object {
"type": "currency",
"value": "",
},
Object {
"type": "literal",
"value": " ",
},
Object {
"type": "integer",
"value": "123",
},
Object {
"type": "group",
"value": ".",
},
Object {
"type": "integer",
"value": "456",
},
Object {
"type": "decimal",
"value": ",",
},
Object {
"type": "fraction",
"value": "79",
},
]
`;

exports[`Format to parts currency should format 123456.789 for [ 'CN', 'PT', [length]: 2 ] 1`] = `
Array [
Object {
"type": "currency",
"value": "",
},
Object {
"type": "literal",
"value": " ",
},
Object {
"type": "integer",
"value": "123",
},
Object {
"type": "group",
"value": ".",
},
Object {
"type": "integer",
"value": "456",
},
Object {
"type": "decimal",
"value": ",",
},
Object {
"type": "fraction",
"value": "79",
},
]
`;

exports[`Format to parts number should format 123456.789 for 'PT' 1`] = `
Array [
Object {
"type": "integer",
"value": "123",
},
Object {
"type": "group",
"value": ".",
},
Object {
"type": "integer",
"value": "456",
},
Object {
"type": "decimal",
"value": ",",
},
Object {
"type": "fraction",
"value": "789",
},
]
`;

exports[`Format to parts number should format 123456.789 for [ 'CN', 'PT', [length]: 2 ] 1`] = `
Array [
Object {
"type": "integer",
"value": "123",
},
Object {
"type": "group",
"value": ".",
},
Object {
"type": "integer",
"value": "456",
},
Object {
"type": "decimal",
"value": ",",
},
Object {
"type": "fraction",
"value": "789",
},
]
`;

exports[`Get format currency should get format for 'PT' 1`] = `
Object {
"currency": "EUR",
"currencyDisplay": "symbol",
"currencyIndex": 0,
"currencySign": "standard",
"locale": "pt",
"maximumFractionDigits": 2,
"minimumFractionDigits": 2,
"minimumIntegerDigits": 1,
"notation": "standard",
"numberingSystem": "latn",
"parts": Object {
"currency": "",
"decimal": ",",
"group": ".",
"literal": " ",
},
"signDisplay": "auto",
"style": "currency",
"useGrouping": true,
}
`;

exports[`Get format currency should get format for [ 'en-US', 'pt-PT', [length]: 2 ] 1`] = `
Object {
"currency": "USD",
"currencyDisplay": "symbol",
"currencyIndex": 0,
"currencySign": "standard",
"locale": "en-US",
"maximumFractionDigits": 2,
"minimumFractionDigits": 2,
"minimumIntegerDigits": 1,
"notation": "standard",
"numberingSystem": "latn",
"parts": Object {
"currency": "$",
"decimal": ".",
"group": ",",
},
"signDisplay": "auto",
"style": "currency",
"useGrouping": true,
}
`;

exports[`Get format number should get format for 'PT' 1`] = `
Object {
"locale": "pt",
"maximumFractionDigits": 3,
"minimumFractionDigits": 0,
"minimumIntegerDigits": 1,
"notation": "standard",
"numberingSystem": "latn",
"parts": Object {
"decimal": ",",
"group": ".",
},
"signDisplay": "auto",
"style": "decimal",
"useGrouping": true,
}
`;

exports[`Get format number should get format for [ 'en-US', 'pt-PT', [length]: 2 ] 1`] = `
Object {
"locale": "en-US",
"maximumFractionDigits": 3,
"minimumFractionDigits": 0,
"minimumIntegerDigits": 1,
"notation": "standard",
"numberingSystem": "latn",
"parts": Object {
"decimal": ".",
"group": ",",
},
"signDisplay": "auto",
"style": "decimal",
"useGrouping": true,
}
`;
35 changes: 34 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { Locale, Options } from './types';
import { Locale, Options, Format, Parts } from './types';
import { isIntlSupported, getNumberFormat } from './lib/intl';

type Args = Array<unknown>;
Expand Down Expand Up @@ -52,3 +52,36 @@ export function formatToPartsFactory<T extends Args>(
return numberFormat.formatToParts(value);
};
}

const TEST_VALUE = 1001001001.11111;

export function getFormatFactory<T extends Args>(getOptions: GetOptions<T>) {
return (locales?: Locale | Locale[], ...args: T): Format | null => {
if (!isIntlSupported) {
return null;
}

const options = getOptions(locales, ...args);
const numberFormat = getNumberFormat(locales, options);
const resolvedOptions = numberFormat.resolvedOptions();
const rawParts = numberFormat.formatToParts(TEST_VALUE);

const parts = rawParts.reduce((allParts, part) => {
if (part.type === 'integer' || part.type === 'fraction') {
return allParts;
}
// eslint-disable-next-line no-param-reassign
allParts[part.type] = part.value;
return allParts;
}, {} as Parts);

if (options.style === 'currency') {
const currencyIndex = rawParts.findIndex(
(part) => part.type === 'currency',
);
return { ...resolvedOptions, parts, currencyIndex };
}

return { ...resolvedOptions, parts };
};
}
84 changes: 32 additions & 52 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
formatCurrency,
formatToParts,
formatCurrencyToParts,
getFormat,
getCurrencyFormat,
} from '.';

describe('Format', () => {
Expand Down Expand Up @@ -47,65 +49,43 @@ describe('Format', () => {
describe('Format to parts', () => {
describe('number', () => {
it.each([
[
123456.789,
'PT',
[
{ type: 'integer', value: '123' },
{ type: 'group', value: '.' },
{ type: 'integer', value: '456' },
{ type: 'decimal', value: ',' },
{ type: 'fraction', value: '789' },
],
],
[
123456.789,
['CN', 'PT'],
[
{ type: 'integer', value: '123' },
{ type: 'group', value: '.' },
{ type: 'integer', value: '456' },
{ type: 'decimal', value: ',' },
{ type: 'fraction', value: '789' },
],
],
])('should format %f for %o', (number, locales, expected) => {
[123456.789, 'PT'],
[123456.789, ['CN', 'PT']],
])('should format %f for %o', (number, locales) => {
const actual = formatToParts(number, locales);
expect(actual).toEqual(expected);
expect(actual).toMatchSnapshot();
});
});

describe('currency', () => {
it.each([
[
123456.789,
'PT',
[
{ type: 'currency', value: '€' },
{ type: 'literal', value: ' ' },
{ type: 'integer', value: '123' },
{ type: 'group', value: '.' },
{ type: 'integer', value: '456' },
{ type: 'decimal', value: ',' },
{ type: 'fraction', value: '79' },
],
],
[
123456.789,
['CN', 'PT'],
[
{ type: 'currency', value: '€' },
{ type: 'literal', value: ' ' },
{ type: 'integer', value: '123' },
{ type: 'group', value: '.' },
{ type: 'integer', value: '456' },
{ type: 'decimal', value: ',' },
{ type: 'fraction', value: '79' },
],
],
])('should format %f for %o', (number, locales, expected) => {
[123456.789, 'PT'],
[123456.789, ['CN', 'PT']],
])('should format %f for %o', (number, locales) => {
const actual = formatCurrencyToParts(number, locales);
expect(actual).toEqual(expected);
expect(actual).toMatchSnapshot();
});
});
});

describe('Get format', () => {
describe('number', () => {
it.each([['PT'], [['en-US', 'pt-PT']]])(
'should get format for %o',
(locales) => {
const actual = getFormat(locales);
expect(actual).toMatchSnapshot();
},
);
});

describe('currency', () => {
it.each([['PT'], [['en-US', 'pt-PT']]])(
'should get format for %o',
(locales) => {
const actual = getCurrencyFormat(locales);
expect(actual).toMatchSnapshot();
},
);
});
});
25 changes: 11 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
/* eslint-disable @typescript-eslint/unbound-method */

import { Currency } from './types';
import { formatFactory, formatToPartsFactory } from './base';
import { formatFactory, formatToPartsFactory, getFormatFactory } from './base';
import { isIntlSupported, resolveLocale } from './lib/intl';
import { getDecimalOptions } from './lib/numbers';
import { getCurrencyOptions, resolveCurrency } from './lib/currencies';
import {
getDecimalSeparator,
getGroupSeparator,
getCurrencySymbol,
} from './lib/parts';
getCurrencyOptions,
resolveCurrency,
CURRENCIES,
} from './lib/currencies';

type NumberArgs = [Intl.NumberFormatOptions?];
type CurrencyArgs = [Currency?, Intl.NumberFormatOptions?];
Expand All @@ -39,11 +38,9 @@ export const formatCurrencyToParts = formatToPartsFactory<CurrencyArgs>(
getCurrencyOptions,
);

export {
isIntlSupported,
resolveLocale,
resolveCurrency,
getDecimalSeparator,
getGroupSeparator,
getCurrencySymbol,
};
export const getFormat = getFormatFactory<NumberArgs>(getDecimalOptions);
export const getCurrencyFormat = getFormatFactory<CurrencyArgs>(
getCurrencyOptions,
);

export { isIntlSupported, resolveLocale, resolveCurrency, CURRENCIES };
Loading

0 comments on commit c2a5946

Please sign in to comment.