Skip to content

Commit

Permalink
fix: check support for Intl ahead of time
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Jul 29, 2020
1 parent ef5ad48 commit a792f26
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
53 changes: 33 additions & 20 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Locale, Options, Format } from './types';
import {
isNumberFormatSupported,
isNumberFormatToPartsSupported,
getNumberFormat,
memoizeIntl,
} from './lib/intl';
import { findIndex } from './lib/findIndex';

Expand All @@ -28,12 +28,17 @@ type GetOptions<T extends Args> = (
...args: T
) => Options;

export function formatFactory<T extends Args>(getOptions: GetOptions<T>) {
return (value: number, locales?: Locale | Locale[], ...args: T): string => {
if (!isNumberFormatSupported) {
return `${value}`;
}
export function formatFactory<T extends Args>(
getOptions: GetOptions<T>,
): (value: number, locales?: Locale | Locale[], ...args: T) => string {
if (!isNumberFormatSupported) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (value, _locales, ..._args): string => `${value}`;
}

const getNumberFormat = memoizeIntl();

return (value, locales, ...args): string => {
const options = getOptions(locales, ...args);
const numberFormat = getNumberFormat(locales, options);
return numberFormat.format(value);
Expand All @@ -42,16 +47,21 @@ export function formatFactory<T extends Args>(getOptions: GetOptions<T>) {

export function formatToPartsFactory<T extends Args>(
getOptions: GetOptions<T>,
) {
return (
value: number,
locales?: Locale | Locale[],
...args: T
): Intl.NumberFormatPart[] => {
if (!isNumberFormatToPartsSupported) {
return [{ type: 'integer', value: value.toString() }];
}
): (
value: number,
locales?: Locale | Locale[],
...args: T
) => Intl.NumberFormatPart[] {
if (!isNumberFormatToPartsSupported) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (value, _locales, ..._args): Intl.NumberFormatPart[] => [
{ type: 'integer', value: value.toString() },
];
}

const getNumberFormat = memoizeIntl();

return (value, locales, ...args): Intl.NumberFormatPart[] => {
const options = getOptions(locales, ...args);
const numberFormat = getNumberFormat(locales, options);
return numberFormat.formatToParts(value);
Expand All @@ -66,12 +76,15 @@ function getPart(parts: Intl.NumberFormatPart[], name: string): string {

export function resolveFormatFactory<T extends Args>(
getOptions: GetOptions<T>,
) {
return (locales?: Locale | Locale[], ...args: T): Format | null => {
if (!isNumberFormatToPartsSupported) {
return null;
}
): (locales?: Locale | Locale[], ...args: T) => null | Format {
if (!isNumberFormatToPartsSupported) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (_locales, ..._args) => null;
}

const getNumberFormat = memoizeIntl();

return (locales, ...args): Format => {
const options = getOptions(locales, ...args);
const numberFormat = getNumberFormat(locales, options);
const resolvedOptions = numberFormat.resolvedOptions();
Expand Down
15 changes: 13 additions & 2 deletions src/lib/intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,26 @@ export const isIntlSupported = (() => {
return isNumberFormatSupported;
})();

export const getNumberFormat: (
let memoizedIntl: (
locales?: Locale | Locale[],
options?: Intl.NumberFormatOptions,
) => Intl.NumberFormat = memoizeFormatConstructor(Intl.NumberFormat);
) => Intl.NumberFormat;

export const memoizeIntl: () => (
locales?: Locale | Locale[],
options?: Intl.NumberFormatOptions,
) => Intl.NumberFormat = () => {
if (!memoizedIntl) {
memoizedIntl = memoizeFormatConstructor(Intl.NumberFormat);
}
return memoizedIntl;
};

export function resolveLocale(locales?: Locale | Locale[]): Locale | Locale[] {
if (locales && locales.length >= 0) {
return locales;
}
const getNumberFormat = memoizeIntl();
const numberFormat = getNumberFormat();
return numberFormat.resolvedOptions().locale;
}

0 comments on commit a792f26

Please sign in to comment.