Skip to content

Commit ef5ad48

Browse files
committed
feat: replace isIntlSupported with isNumberFormatSupported and add isNumberFormatToPartsSupported
This makes it clearer what these constants do. This change also makes the check itself safer.
1 parent f4121a2 commit ef5ad48

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,20 @@ resolveCurrencyFormat('en-GB', { currencyDisplay: 'name' });
314314

315315
### Constants
316316

317-
#### `isIntlSupported`
317+
#### `isNumberFormatSupported`
318318

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

321321
```ts
322-
const isIntlSupported: boolean;
322+
const isNumberFormatSupported: boolean;
323+
```
324+
325+
#### `isNumberFormatToPartsSupported`
326+
327+
Whether the `Intl`, `Intl.NumberFormat`, and `Intl.NumberFormat.formatToParts` APIs are supported by the runtime.
328+
329+
```ts
330+
const isNumberFormatToPartsSupported: boolean;
323331
```
324332

325333
#### `CURRENCIES`

src/base.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
*/
1515

1616
import { Locale, Options, Format } from './types';
17-
import { isIntlSupported, getNumberFormat } from './lib/intl';
17+
import {
18+
isNumberFormatSupported,
19+
isNumberFormatToPartsSupported,
20+
getNumberFormat,
21+
} from './lib/intl';
1822
import { findIndex } from './lib/findIndex';
1923

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

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

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

@@ -67,10 +68,7 @@ export function resolveFormatFactory<T extends Args>(
6768
getOptions: GetOptions<T>,
6869
) {
6970
return (locales?: Locale | Locale[], ...args: T): Format | null => {
70-
if (
71-
!isIntlSupported ||
72-
typeof Intl.NumberFormat.prototype.formatToParts === 'undefined'
73-
) {
71+
if (!isNumberFormatToPartsSupported) {
7472
return null;
7573
}
7674

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import {
2424
import { getDecimalOptions } from './lib/numbers';
2525
import { getCurrencyOptions } from './lib/currencies';
2626

27-
export { isIntlSupported } from './lib/intl';
27+
export {
28+
isNumberFormatSupported,
29+
isNumberFormatToPartsSupported,
30+
isIntlSupported,
31+
} from './lib/intl';
2832
export { CURRENCIES } from './data/currencies';
2933

3034
type NumberArgs = [Intl.NumberFormatOptions?];

src/lib/intl.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,48 @@ import memoizeFormatConstructor from 'intl-format-cache';
1818
import { Locale } from '../types';
1919

2020
/**
21-
* Whether the `Intl` and `Intl.NumberFormat` APIs are supported by the runtime.
21+
* Whether the `Intl` and `Intl.NumberFormat` APIs
22+
* are supported by the runtime.
2223
*/
23-
export const isIntlSupported =
24-
typeof Intl !== 'undefined' && typeof Intl.NumberFormat !== 'undefined';
24+
export const isNumberFormatSupported = (() => {
25+
try {
26+
return (
27+
typeof Intl !== 'undefined' && typeof Intl.NumberFormat !== 'undefined'
28+
);
29+
} catch (error) {
30+
return false;
31+
}
32+
})();
33+
34+
/**
35+
* Whether the `Intl`, `Intl.NumberFormat`, and
36+
* `Intl.NumberFormat.formatToParts` APIs are supported by the runtime.
37+
*/
38+
export const isNumberFormatToPartsSupported = (() => {
39+
try {
40+
return typeof Intl.NumberFormat.prototype.formatToParts !== 'undefined';
41+
} catch (error) {
42+
return false;
43+
}
44+
})();
45+
46+
/**
47+
* @deprecated Whether the `Intl` and `Intl.NumberFormat` APIs
48+
* are supported by the runtime.
49+
*/
50+
export const isIntlSupported = (() => {
51+
if (process.env.NODE_ENV !== 'production') {
52+
// eslint-disable-next-line no-console
53+
console.warn(
54+
[
55+
'DEPRECATED: isIntlSupported has been replaced',
56+
'by isNumberFormatSupported & isNumberFormatToPartsSupported',
57+
'and will be removed in the next major version.',
58+
].join(' '),
59+
);
60+
}
61+
return isNumberFormatSupported;
62+
})();
2563

2664
export const getNumberFormat: (
2765
locales?: Locale | Locale[],

0 commit comments

Comments
 (0)