Skip to content

Commit

Permalink
feat: add support for formatting with unit
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Dec 24, 2020
1 parent 0db9e61 commit b00d0ab
Show file tree
Hide file tree
Showing 7 changed files with 8,838 additions and 20 deletions.
8,806 changes: 8,806 additions & 0 deletions src/__snapshots__/index.spec.ts.snap

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ describe('Format', () => {
const actual = format(number, locale);
expect(actual).toMatchSnapshot();
});

it.each(locales)('should format a unit number for %o', (locale) => {
const actual = format(number, locale, { style: 'unit', unit: 'liter' });
expect(actual).toMatchSnapshot();
});
});

describe('currency', () => {
Expand All @@ -56,6 +61,14 @@ describe('Format to parts', () => {
const actual = formatToParts(number, locale);
expect(actual).toMatchSnapshot();
});

it.each(locales)('should format a unit number for %o', (locale) => {
const actual = formatToParts(number, locale, {
style: 'unit',
unit: 'liter',
});
expect(actual).toMatchSnapshot();
});
});

describe('currency', () => {
Expand Down
12 changes: 4 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
formatToPartsFactory,
resolveFormatFactory,
} from './base';
import { getDecimalOptions } from './lib/numbers';
import { getNumberOptions } from './lib/numbers';
import { getCurrencyOptions } from './lib/currencies';

export {
Expand All @@ -39,7 +39,7 @@ type CurrencyArgs = [Currency?, Intl.NumberFormatOptions?];
* [styles, units](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_style_and_unit),
* and [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const format = formatFactory<NumberArgs>(getDecimalOptions);
export const format = formatFactory<NumberArgs>(getNumberOptions);

/**
* Formats a number according to the locale in the country's official curreny
Expand All @@ -52,9 +52,7 @@ export const formatCurrency = formatFactory<CurrencyArgs>(getCurrencyOptions);
* [styles, units](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_style_and_unit),
* and [notations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#Using_notation).
*/
export const formatToParts = formatToPartsFactory<NumberArgs>(
getDecimalOptions,
);
export const formatToParts = formatToPartsFactory<NumberArgs>(getNumberOptions);

/**
* Formats a number according to the locale in the country's official curreny
Expand All @@ -67,9 +65,7 @@ export const formatCurrencyToParts = formatToPartsFactory<CurrencyArgs>(
/**
* Resolves the locale and collation options that are used to format a number.
*/
export const resolveFormat = resolveFormatFactory<NumberArgs>(
getDecimalOptions,
);
export const resolveFormat = resolveFormatFactory<NumberArgs>(getNumberOptions);

/**
* Resolves the locale and collation options that are used to format a number
Expand Down
9 changes: 7 additions & 2 deletions src/lib/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

/* eslint-disable no-continue */

import { Locale, CurrencyOptions, DecimalOptions, Currency } from '../types';
import {
Locale,
CurrencyFormatOptions,
NumberFormatOptions,
Currency,
} from '../types';
import { CURRENCIES } from '../data/currencies';

import { resolveLocale } from './intl';
Expand Down Expand Up @@ -64,7 +69,7 @@ export function getCurrencyOptions(
locales?: Locale | Locale[],
currency?: Currency,
options?: Intl.NumberFormatOptions,
): CurrencyOptions | DecimalOptions {
): CurrencyFormatOptions | NumberFormatOptions {
const finalCurrency = currency || resolveCurrency(locales);

if (!finalCurrency) {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
* limitations under the License.
*/

import { Locale, DecimalOptions } from '../types';
import { Locale, NumberFormatOptions } from '../types';

export function getDecimalOptions(
export function getNumberOptions(
locales?: Locale | Locale[],
options?: Intl.NumberFormatOptions,
): DecimalOptions {
return { ...options, style: 'decimal' };
): NumberFormatOptions {
return { style: 'decimal', ...options };
}

export function normalize(value?: string): number {
Expand Down
8 changes: 3 additions & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ export type Format = Intl.ResolvedNumberFormatOptions & {
currencyPosition?: 'prefix' | 'suffix';
};

export interface DecimalOptions extends Intl.NumberFormatOptions {
style: 'decimal';
}
export type NumberFormatOptions = Intl.NumberFormatOptions;

export interface CurrencyOptions extends Intl.NumberFormatOptions {
export interface CurrencyFormatOptions extends Intl.NumberFormatOptions {
style: 'currency';
currency: Currency;
}

export type Options = DecimalOptions | CurrencyOptions;
export type Options = NumberFormatOptions | CurrencyFormatOptions;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ES2017",
"module": "ES2020",
"lib": ["es2017", "es7", "es6", "dom"],
"lib": ["es2020", "es2017", "es7", "es6", "dom"],
"declaration": true,
"strict": true,
"skipLibCheck": true,
Expand Down

0 comments on commit b00d0ab

Please sign in to comment.