@formatjs/utils 2.0.2 → 2.0.4

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 CHANGED
@@ -1,3 +1,99 @@
1
1
  # @formatjs/utils
2
2
 
3
- This is a collection of generally intl-related utilities that are useful.
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/utils",
3
3
  "description": "Collection of useful intl utilities",
4
- "version": "2.0.2",
4
+ "version": "2.0.4",
5
5
  "license": "MIT",
6
6
  "author": "Long Ho <holevietlong@gmail.com>",
7
7
  "type": "module",
@@ -1,4 +1,4 @@
1
- import * as alpha3CountryCodes from './iso3166Alpha3CountryCodes.json';
1
+ import alpha3CountryCodes from './iso3166Alpha3CountryCodes.js';
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).
@@ -0,0 +1,169 @@
1
+ declare const _default: {
2
+ readonly AFN: 2;
3
+ readonly EUR: 2;
4
+ readonly ALL: 2;
5
+ readonly DZD: 2;
6
+ readonly USD: 2;
7
+ readonly AOA: 2;
8
+ readonly XCD: 2;
9
+ readonly XAD: 2;
10
+ readonly ARS: 2;
11
+ readonly AMD: 2;
12
+ readonly AWG: 2;
13
+ readonly AUD: 2;
14
+ readonly AZN: 2;
15
+ readonly BSD: 2;
16
+ readonly BHD: 3;
17
+ readonly BDT: 2;
18
+ readonly BBD: 2;
19
+ readonly BYN: 2;
20
+ readonly BZD: 2;
21
+ readonly XOF: 0;
22
+ readonly BMD: 2;
23
+ readonly INR: 2;
24
+ readonly BTN: 2;
25
+ readonly BOB: 2;
26
+ readonly BOV: 2;
27
+ readonly BAM: 2;
28
+ readonly BWP: 2;
29
+ readonly NOK: 2;
30
+ readonly BRL: 2;
31
+ readonly BND: 2;
32
+ readonly BGN: 2;
33
+ readonly BIF: 0;
34
+ readonly CVE: 2;
35
+ readonly KHR: 2;
36
+ readonly XAF: 0;
37
+ readonly CAD: 2;
38
+ readonly KYD: 2;
39
+ readonly CLP: 0;
40
+ readonly CLF: 4;
41
+ readonly CNY: 2;
42
+ readonly COP: 2;
43
+ readonly COU: 2;
44
+ readonly KMF: 0;
45
+ readonly CDF: 2;
46
+ readonly NZD: 2;
47
+ readonly CRC: 2;
48
+ readonly CUP: 2;
49
+ readonly XCG: 2;
50
+ readonly CZK: 2;
51
+ readonly DKK: 2;
52
+ readonly DJF: 0;
53
+ readonly DOP: 2;
54
+ readonly EGP: 2;
55
+ readonly SVC: 2;
56
+ readonly ERN: 2;
57
+ readonly SZL: 2;
58
+ readonly ETB: 2;
59
+ readonly FKP: 2;
60
+ readonly FJD: 2;
61
+ readonly XPF: 0;
62
+ readonly GMD: 2;
63
+ readonly GEL: 2;
64
+ readonly GHS: 2;
65
+ readonly GIP: 2;
66
+ readonly GTQ: 2;
67
+ readonly GBP: 2;
68
+ readonly GNF: 0;
69
+ readonly GYD: 2;
70
+ readonly HTG: 2;
71
+ readonly HNL: 2;
72
+ readonly HKD: 2;
73
+ readonly HUF: 2;
74
+ readonly ISK: 0;
75
+ readonly IDR: 2;
76
+ readonly IRR: 2;
77
+ readonly IQD: 3;
78
+ readonly ILS: 2;
79
+ readonly JMD: 2;
80
+ readonly JPY: 0;
81
+ readonly JOD: 3;
82
+ readonly KZT: 2;
83
+ readonly KES: 2;
84
+ readonly KPW: 2;
85
+ readonly KRW: 0;
86
+ readonly KWD: 3;
87
+ readonly KGS: 2;
88
+ readonly LAK: 2;
89
+ readonly LBP: 2;
90
+ readonly LSL: 2;
91
+ readonly ZAR: 2;
92
+ readonly LRD: 2;
93
+ readonly LYD: 3;
94
+ readonly CHF: 2;
95
+ readonly MOP: 2;
96
+ readonly MKD: 2;
97
+ readonly MGA: 2;
98
+ readonly MWK: 2;
99
+ readonly MYR: 2;
100
+ readonly MVR: 2;
101
+ readonly MRU: 2;
102
+ readonly MUR: 2;
103
+ readonly MXN: 2;
104
+ readonly MXV: 2;
105
+ readonly MDL: 2;
106
+ readonly MNT: 2;
107
+ readonly MAD: 2;
108
+ readonly MZN: 2;
109
+ readonly MMK: 2;
110
+ readonly NAD: 2;
111
+ readonly NPR: 2;
112
+ readonly NIO: 2;
113
+ readonly NGN: 2;
114
+ readonly OMR: 3;
115
+ readonly PKR: 2;
116
+ readonly PAB: 2;
117
+ readonly PGK: 2;
118
+ readonly PYG: 0;
119
+ readonly PEN: 2;
120
+ readonly PHP: 2;
121
+ readonly PLN: 2;
122
+ readonly QAR: 2;
123
+ readonly RON: 2;
124
+ readonly RUB: 2;
125
+ readonly RWF: 0;
126
+ readonly SHP: 2;
127
+ readonly WST: 2;
128
+ readonly STN: 2;
129
+ readonly SAR: 2;
130
+ readonly RSD: 2;
131
+ readonly SCR: 2;
132
+ readonly SLE: 2;
133
+ readonly SGD: 2;
134
+ readonly SBD: 2;
135
+ readonly SOS: 2;
136
+ readonly SSP: 2;
137
+ readonly LKR: 2;
138
+ readonly SDG: 2;
139
+ readonly SRD: 2;
140
+ readonly SEK: 2;
141
+ readonly CHE: 2;
142
+ readonly CHW: 2;
143
+ readonly SYP: 2;
144
+ readonly TWD: 2;
145
+ readonly TJS: 2;
146
+ readonly TZS: 2;
147
+ readonly THB: 2;
148
+ readonly TOP: 2;
149
+ readonly TTD: 2;
150
+ readonly TND: 3;
151
+ readonly TRY: 2;
152
+ readonly TMT: 2;
153
+ readonly UGX: 0;
154
+ readonly UAH: 2;
155
+ readonly AED: 2;
156
+ readonly USN: 2;
157
+ readonly UYU: 2;
158
+ readonly UYI: 0;
159
+ readonly UYW: 4;
160
+ readonly UZS: 2;
161
+ readonly VUV: 0;
162
+ readonly VES: 2;
163
+ readonly VED: 2;
164
+ readonly VND: 0;
165
+ readonly YER: 2;
166
+ readonly ZMW: 2;
167
+ readonly ZWG: 2;
168
+ };
169
+ export default _default;
@@ -0,0 +1,169 @@
1
+ // This is a generated file. Do not edit directly.
2
+ export default {
3
+ "AFN": 2,
4
+ "EUR": 2,
5
+ "ALL": 2,
6
+ "DZD": 2,
7
+ "USD": 2,
8
+ "AOA": 2,
9
+ "XCD": 2,
10
+ "XAD": 2,
11
+ "ARS": 2,
12
+ "AMD": 2,
13
+ "AWG": 2,
14
+ "AUD": 2,
15
+ "AZN": 2,
16
+ "BSD": 2,
17
+ "BHD": 3,
18
+ "BDT": 2,
19
+ "BBD": 2,
20
+ "BYN": 2,
21
+ "BZD": 2,
22
+ "XOF": 0,
23
+ "BMD": 2,
24
+ "INR": 2,
25
+ "BTN": 2,
26
+ "BOB": 2,
27
+ "BOV": 2,
28
+ "BAM": 2,
29
+ "BWP": 2,
30
+ "NOK": 2,
31
+ "BRL": 2,
32
+ "BND": 2,
33
+ "BGN": 2,
34
+ "BIF": 0,
35
+ "CVE": 2,
36
+ "KHR": 2,
37
+ "XAF": 0,
38
+ "CAD": 2,
39
+ "KYD": 2,
40
+ "CLP": 0,
41
+ "CLF": 4,
42
+ "CNY": 2,
43
+ "COP": 2,
44
+ "COU": 2,
45
+ "KMF": 0,
46
+ "CDF": 2,
47
+ "NZD": 2,
48
+ "CRC": 2,
49
+ "CUP": 2,
50
+ "XCG": 2,
51
+ "CZK": 2,
52
+ "DKK": 2,
53
+ "DJF": 0,
54
+ "DOP": 2,
55
+ "EGP": 2,
56
+ "SVC": 2,
57
+ "ERN": 2,
58
+ "SZL": 2,
59
+ "ETB": 2,
60
+ "FKP": 2,
61
+ "FJD": 2,
62
+ "XPF": 0,
63
+ "GMD": 2,
64
+ "GEL": 2,
65
+ "GHS": 2,
66
+ "GIP": 2,
67
+ "GTQ": 2,
68
+ "GBP": 2,
69
+ "GNF": 0,
70
+ "GYD": 2,
71
+ "HTG": 2,
72
+ "HNL": 2,
73
+ "HKD": 2,
74
+ "HUF": 2,
75
+ "ISK": 0,
76
+ "IDR": 2,
77
+ "IRR": 2,
78
+ "IQD": 3,
79
+ "ILS": 2,
80
+ "JMD": 2,
81
+ "JPY": 0,
82
+ "JOD": 3,
83
+ "KZT": 2,
84
+ "KES": 2,
85
+ "KPW": 2,
86
+ "KRW": 0,
87
+ "KWD": 3,
88
+ "KGS": 2,
89
+ "LAK": 2,
90
+ "LBP": 2,
91
+ "LSL": 2,
92
+ "ZAR": 2,
93
+ "LRD": 2,
94
+ "LYD": 3,
95
+ "CHF": 2,
96
+ "MOP": 2,
97
+ "MKD": 2,
98
+ "MGA": 2,
99
+ "MWK": 2,
100
+ "MYR": 2,
101
+ "MVR": 2,
102
+ "MRU": 2,
103
+ "MUR": 2,
104
+ "MXN": 2,
105
+ "MXV": 2,
106
+ "MDL": 2,
107
+ "MNT": 2,
108
+ "MAD": 2,
109
+ "MZN": 2,
110
+ "MMK": 2,
111
+ "NAD": 2,
112
+ "NPR": 2,
113
+ "NIO": 2,
114
+ "NGN": 2,
115
+ "OMR": 3,
116
+ "PKR": 2,
117
+ "PAB": 2,
118
+ "PGK": 2,
119
+ "PYG": 0,
120
+ "PEN": 2,
121
+ "PHP": 2,
122
+ "PLN": 2,
123
+ "QAR": 2,
124
+ "RON": 2,
125
+ "RUB": 2,
126
+ "RWF": 0,
127
+ "SHP": 2,
128
+ "WST": 2,
129
+ "STN": 2,
130
+ "SAR": 2,
131
+ "RSD": 2,
132
+ "SCR": 2,
133
+ "SLE": 2,
134
+ "SGD": 2,
135
+ "SBD": 2,
136
+ "SOS": 2,
137
+ "SSP": 2,
138
+ "LKR": 2,
139
+ "SDG": 2,
140
+ "SRD": 2,
141
+ "SEK": 2,
142
+ "CHE": 2,
143
+ "CHW": 2,
144
+ "SYP": 2,
145
+ "TWD": 2,
146
+ "TJS": 2,
147
+ "TZS": 2,
148
+ "THB": 2,
149
+ "TOP": 2,
150
+ "TTD": 2,
151
+ "TND": 3,
152
+ "TRY": 2,
153
+ "TMT": 2,
154
+ "UGX": 0,
155
+ "UAH": 2,
156
+ "AED": 2,
157
+ "USN": 2,
158
+ "UYU": 2,
159
+ "UYI": 0,
160
+ "UYW": 4,
161
+ "UZS": 2,
162
+ "VUV": 0,
163
+ "VES": 2,
164
+ "VED": 2,
165
+ "VND": 0,
166
+ "YER": 2,
167
+ "ZMW": 2,
168
+ "ZWG": 2
169
+ };
@@ -1,5 +1,5 @@
1
1
  import { canonicalizeCountryCode } from './countryCodes.js';
2
- import * as data from './defaultCurrencyData.generated.json';
2
+ import data from './defaultCurrencyData.generated.js';
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]) {