Skip to content

Commit

Permalink
feat: replace isIntlSupported with isNumberFormatSupported and add is…
Browse files Browse the repository at this point in the history
…NumberFormatToPartsSupported

This makes it clearer what these constants do. This change also makes the check itself safer.
  • Loading branch information
connor-baer committed Jul 28, 2020
1 parent f4121a2 commit ef5ad48
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,20 @@ resolveCurrencyFormat('en-GB', { currencyDisplay: 'name' });

### Constants

#### `isIntlSupported`
#### `isNumberFormatSupported`

Whether the `Intl` and `Intl.NumberFormat` APIs are supported by the runtime.

```ts
const isIntlSupported: boolean;
const isNumberFormatSupported: boolean;
```

#### `isNumberFormatToPartsSupported`

Whether the `Intl`, `Intl.NumberFormat`, and `Intl.NumberFormat.formatToParts` APIs are supported by the runtime.

```ts
const isNumberFormatToPartsSupported: boolean;
```

#### `CURRENCIES`
Expand Down
18 changes: 8 additions & 10 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
*/

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

type Args = Array<unknown>;
Expand All @@ -26,7 +30,7 @@ type GetOptions<T extends Args> = (

export function formatFactory<T extends Args>(getOptions: GetOptions<T>) {
return (value: number, locales?: Locale | Locale[], ...args: T): string => {
if (!isIntlSupported) {
if (!isNumberFormatSupported) {
return `${value}`;
}

Expand All @@ -44,10 +48,7 @@ export function formatToPartsFactory<T extends Args>(
locales?: Locale | Locale[],
...args: T
): Intl.NumberFormatPart[] => {
if (
!isIntlSupported ||
typeof Intl.NumberFormat.prototype.formatToParts === 'undefined'
) {
if (!isNumberFormatToPartsSupported) {
return [{ type: 'integer', value: value.toString() }];
}

Expand All @@ -67,10 +68,7 @@ export function resolveFormatFactory<T extends Args>(
getOptions: GetOptions<T>,
) {
return (locales?: Locale | Locale[], ...args: T): Format | null => {
if (
!isIntlSupported ||
typeof Intl.NumberFormat.prototype.formatToParts === 'undefined'
) {
if (!isNumberFormatToPartsSupported) {
return null;
}

Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import {
import { getDecimalOptions } from './lib/numbers';
import { getCurrencyOptions } from './lib/currencies';

export { isIntlSupported } from './lib/intl';
export {
isNumberFormatSupported,
isNumberFormatToPartsSupported,
isIntlSupported,
} from './lib/intl';
export { CURRENCIES } from './data/currencies';

type NumberArgs = [Intl.NumberFormatOptions?];
Expand Down
44 changes: 41 additions & 3 deletions src/lib/intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,48 @@ import memoizeFormatConstructor from 'intl-format-cache';
import { Locale } from '../types';

/**
* Whether the `Intl` and `Intl.NumberFormat` APIs are supported by the runtime.
* Whether the `Intl` and `Intl.NumberFormat` APIs
* are supported by the runtime.
*/
export const isIntlSupported =
typeof Intl !== 'undefined' && typeof Intl.NumberFormat !== 'undefined';
export const isNumberFormatSupported = (() => {
try {
return (
typeof Intl !== 'undefined' && typeof Intl.NumberFormat !== 'undefined'
);
} catch (error) {
return false;
}
})();

/**
* Whether the `Intl`, `Intl.NumberFormat`, and
* `Intl.NumberFormat.formatToParts` APIs are supported by the runtime.
*/
export const isNumberFormatToPartsSupported = (() => {
try {
return typeof Intl.NumberFormat.prototype.formatToParts !== 'undefined';
} catch (error) {
return false;
}
})();

/**
* @deprecated Whether the `Intl` and `Intl.NumberFormat` APIs
* are supported by the runtime.
*/
export const isIntlSupported = (() => {
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.warn(
[
'DEPRECATED: isIntlSupported has been replaced',
'by isNumberFormatSupported & isNumberFormatToPartsSupported',
'and will be removed in the next major version.',
].join(' '),
);
}
return isNumberFormatSupported;
})();

export const getNumberFormat: (
locales?: Locale | Locale[],
Expand Down

0 comments on commit ef5ad48

Please sign in to comment.