@formatjs/utils 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -1
- package/package.json +1 -1
- package/src/countryCodes.js +1 -1
- package/src/defaultCurrency.js +1 -1
- package/src/defaultLocale.js +1 -1
- package/src/iso4217.js +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
1
|
# @formatjs/utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Collection of useful internationalization (i18n) utilities for working with locales, countries, currencies, and timezones.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @formatjs/utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Country code canonicalization (ISO 3166)
|
|
14
|
+
- Default currency lookup by country
|
|
15
|
+
- Currency minor units and scale calculations (ISO 4217)
|
|
16
|
+
- Default locale detection
|
|
17
|
+
- Default timezone detection
|
|
18
|
+
|
|
19
|
+
## API
|
|
20
|
+
|
|
21
|
+
### Country Codes
|
|
22
|
+
|
|
23
|
+
#### `canonicalizeCountryCode(alpha3OrAlpha2?: string): string | undefined`
|
|
24
|
+
|
|
25
|
+
Canonicalize a country code to an ISO 3166 alpha-2 country code (uppercase).
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import {canonicalizeCountryCode} from '@formatjs/utils'
|
|
29
|
+
|
|
30
|
+
canonicalizeCountryCode('usa') // 'US'
|
|
31
|
+
canonicalizeCountryCode('USA') // 'US'
|
|
32
|
+
canonicalizeCountryCode('us') // 'US'
|
|
33
|
+
canonicalizeCountryCode('US') // 'US'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Currency
|
|
37
|
+
|
|
38
|
+
#### `defaultCurrency(countryCode?: string): string`
|
|
39
|
+
|
|
40
|
+
Look up the default currency for a country code. Returns USD if not found.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import {defaultCurrency} from '@formatjs/utils'
|
|
44
|
+
|
|
45
|
+
defaultCurrency('US') // 'USD'
|
|
46
|
+
defaultCurrency('GB') // 'GBP'
|
|
47
|
+
defaultCurrency('JP') // 'JPY'
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### `countriesUsingDefaultCurrency(currencyCode: string): string[]`
|
|
51
|
+
|
|
52
|
+
Look up countries that use a specific currency as their default.
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import {countriesUsingDefaultCurrency} from '@formatjs/utils'
|
|
56
|
+
|
|
57
|
+
countriesUsingDefaultCurrency('EUR') // ['AT', 'BE', 'CY', 'DE', 'EE', ...]
|
|
58
|
+
countriesUsingDefaultCurrency('USD') // ['US', 'EC', 'SV', ...]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### `currencyMinorScale(currencyCode: string): number`
|
|
62
|
+
|
|
63
|
+
Returns the minor unit scale for a given ISO 4217 currency code. The minor unit scale is the power of 10 representing the number of decimal places used for the currency.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import {currencyMinorScale} from '@formatjs/utils'
|
|
67
|
+
|
|
68
|
+
currencyMinorScale('USD') // 100 (2 decimal places)
|
|
69
|
+
currencyMinorScale('JPY') // 1 (0 decimal places)
|
|
70
|
+
currencyMinorScale('BHD') // 1000 (3 decimal places)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Locale
|
|
74
|
+
|
|
75
|
+
#### `defaultLocale(): string`
|
|
76
|
+
|
|
77
|
+
Returns the default locale for the current environment.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import {defaultLocale} from '@formatjs/utils'
|
|
81
|
+
|
|
82
|
+
defaultLocale() // e.g., 'en-US'
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Timezone
|
|
86
|
+
|
|
87
|
+
#### `defaultTimezone(): string`
|
|
88
|
+
|
|
89
|
+
Returns the default timezone for the current environment.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import {defaultTimezone} from '@formatjs/utils'
|
|
93
|
+
|
|
94
|
+
defaultTimezone() // e.g., 'America/New_York'
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT
|
package/package.json
CHANGED
package/src/countryCodes.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as alpha3CountryCodes from './iso3166Alpha3CountryCodes.json';
|
|
1
|
+
import * as alpha3CountryCodes from './iso3166Alpha3CountryCodes.json' with { type: 'json' };
|
|
2
2
|
var COUNTRY_CODE_ALPHA2 = new Set(Object.keys(alpha3CountryCodes).map(function (key) { return alpha3CountryCodes[key]; }));
|
|
3
3
|
/**
|
|
4
4
|
* Canonicalize a country code to a alpha2 country code (uppercase).
|
package/src/defaultCurrency.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { canonicalizeCountryCode } from './countryCodes.js';
|
|
2
|
-
import * as data from './defaultCurrencyData.generated.json';
|
|
2
|
+
import * as data from './defaultCurrencyData.generated.json' with { type: 'json' };
|
|
3
3
|
var COUNTRIES_BY_DEFAULT_CURRENCY = Object.keys(data).reduce(function (acc, countryCode) {
|
|
4
4
|
var currencyCode = data[countryCode];
|
|
5
5
|
if (!acc[currencyCode]) {
|
package/src/defaultLocale.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { canonicalizeCountryCode } from './countryCodes.js';
|
|
2
|
-
import * as data from './defaultLocaleData.generated.json';
|
|
2
|
+
import * as data from './defaultLocaleData.generated.json' with { type: 'json' };
|
|
3
3
|
/**
|
|
4
4
|
* Look up default locale for a country code.
|
|
5
5
|
* @param countryCode country code (alpha-2)
|
package/src/iso4217.js
CHANGED