@formatjs/intl-supportedvaluesof 2.1.2
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/BUILD.bazel +135 -0
- package/CHANGELOG.md +249 -0
- package/LICENSE.md +9 -0
- package/README.md +270 -0
- package/index.ts +9 -0
- package/package.json +33 -0
- package/polyfill-force.ts +8 -0
- package/polyfill.ts +11 -0
- package/scripts/calendars.ts +21 -0
- package/scripts/collations.ts +21 -0
- package/scripts/currencies.ts +21 -0
- package/scripts/timezones.ts +22 -0
- package/scripts/units.ts +23 -0
- package/should-polyfill.ts +3 -0
- package/src/calendars.generated.ts +4 -0
- package/src/collations.generated.ts +4 -0
- package/src/currencies.generated.ts +4 -0
- package/src/get-supported-calendars.ts +32 -0
- package/src/get-supported-collations.ts +30 -0
- package/src/get-supported-currencies.ts +62 -0
- package/src/get-supported-numbering-systems.ts +35 -0
- package/src/get-supported-timezones.ts +29 -0
- package/src/get-supported-units.ts +73 -0
- package/src/index.ts +75 -0
- package/src/memoize.ts +19 -0
- package/src/numbering-systems.generated.ts +99 -0
- package/src/timezones.generated.ts +4 -0
- package/src/units.generated.ts +4 -0
- package/tests/index.test.ts +155 -0
- package/tsconfig.json +5 -0
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@formatjs/intl-supportedvaluesof",
|
|
3
|
+
"description": "Intl.supportedValuesOf polyfill",
|
|
4
|
+
"version": "2.1.2",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Michele Riva <ciao@micheleriva.it>",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./index.js",
|
|
11
|
+
"./polyfill.js": "./polyfill.js",
|
|
12
|
+
"./polyfill-force.js": "./polyfill-force.js",
|
|
13
|
+
"./should-polyfill.js": "./should-polyfill.js"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@formatjs/ecma402-abstract": "workspace:*",
|
|
17
|
+
"@formatjs/fast-memoize": "workspace:*",
|
|
18
|
+
"tslib": "^2.8.1"
|
|
19
|
+
},
|
|
20
|
+
"bugs": "https://github.com/formatjs/formatjs/issues",
|
|
21
|
+
"homepage": "https://github.com/formatjs/formatjs#readme",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"ecma402",
|
|
24
|
+
"enumerator",
|
|
25
|
+
"formatjs",
|
|
26
|
+
"i18n",
|
|
27
|
+
"intl",
|
|
28
|
+
"react-intl",
|
|
29
|
+
"supportedvaluesof",
|
|
30
|
+
"tc39"
|
|
31
|
+
],
|
|
32
|
+
"repository": "formatjs/formatjs.git"
|
|
33
|
+
}
|
package/polyfill.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {shouldPolyfill} from './should-polyfill.js'
|
|
2
|
+
import {supportedValuesOf} from './src'
|
|
3
|
+
|
|
4
|
+
if (shouldPolyfill()) {
|
|
5
|
+
Object.defineProperty(Intl, 'supportedValuesOf', {
|
|
6
|
+
value: supportedValuesOf,
|
|
7
|
+
enumerable: true,
|
|
8
|
+
writable: false,
|
|
9
|
+
configurable: false,
|
|
10
|
+
})
|
|
11
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import minimist from 'minimist'
|
|
2
|
+
import {outputFileSync} from 'fs-extra/esm'
|
|
3
|
+
import {keyword} from 'cldr-bcp47/bcp47/calendar.json'
|
|
4
|
+
function main(args: minimist.ParsedArgs) {
|
|
5
|
+
const {out} = args
|
|
6
|
+
const calendars = Object.keys(keyword.u.ca).filter(k => !k.startsWith('_'))
|
|
7
|
+
|
|
8
|
+
// Output numbering systems file
|
|
9
|
+
outputFileSync(
|
|
10
|
+
out,
|
|
11
|
+
`/* @generated */
|
|
12
|
+
// prettier-ignore
|
|
13
|
+
export const calendars = ${JSON.stringify(calendars)} as const
|
|
14
|
+
export type Calendar = typeof calendars[number]
|
|
15
|
+
`
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (require.main === module) {
|
|
20
|
+
main(minimist(process.argv))
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import minimist from 'minimist'
|
|
2
|
+
import {outputFileSync} from 'fs-extra/esm'
|
|
3
|
+
import {keyword} from 'cldr-bcp47/bcp47/collation.json'
|
|
4
|
+
function main(args: minimist.ParsedArgs) {
|
|
5
|
+
const {out} = args
|
|
6
|
+
const collations = Object.keys(keyword.u.co).filter(k => !k.startsWith('_'))
|
|
7
|
+
|
|
8
|
+
// Output numbering systems file
|
|
9
|
+
outputFileSync(
|
|
10
|
+
out,
|
|
11
|
+
`/* @generated */
|
|
12
|
+
// prettier-ignore
|
|
13
|
+
export const collations = ${JSON.stringify(collations)} as const
|
|
14
|
+
export type Collation = typeof collations[number]
|
|
15
|
+
`
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (require.main === module) {
|
|
20
|
+
main(minimist(process.argv))
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import minimist from 'minimist'
|
|
2
|
+
import {outputFileSync} from 'fs-extra/esm'
|
|
3
|
+
import {main as data} from 'cldr-numbers-full/main/en/currencies.json'
|
|
4
|
+
function main(args: minimist.ParsedArgs) {
|
|
5
|
+
const {out} = args
|
|
6
|
+
const currencies = Object.keys(data.en.numbers.currencies)
|
|
7
|
+
|
|
8
|
+
// Output numbering systems file
|
|
9
|
+
outputFileSync(
|
|
10
|
+
out,
|
|
11
|
+
`/* @generated */
|
|
12
|
+
// prettier-ignore
|
|
13
|
+
export const currencies = ${JSON.stringify(currencies)} as const
|
|
14
|
+
export type Currency = typeof currencies[number]
|
|
15
|
+
`
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (require.main === module) {
|
|
20
|
+
main(minimist(process.argv))
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import minimist from 'minimist'
|
|
2
|
+
import {outputFileSync} from 'fs-extra/esm'
|
|
3
|
+
interface Args extends minimist.ParsedArgs {
|
|
4
|
+
zone: string[]
|
|
5
|
+
}
|
|
6
|
+
function main(args: Args) {
|
|
7
|
+
const {out, zone: timezones} = args
|
|
8
|
+
|
|
9
|
+
// Output numbering systems file
|
|
10
|
+
outputFileSync(
|
|
11
|
+
out,
|
|
12
|
+
`/* @generated */
|
|
13
|
+
// prettier-ignore
|
|
14
|
+
export const timezones = ${JSON.stringify(timezones)} as const
|
|
15
|
+
export type Timezone = typeof timezones[number]
|
|
16
|
+
`
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (require.main === module) {
|
|
21
|
+
main(minimist<Args>(process.argv))
|
|
22
|
+
}
|
package/scripts/units.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import minimist from 'minimist'
|
|
2
|
+
import {outputFileSync} from 'fs-extra/esm'
|
|
3
|
+
import {SIMPLE_UNITS} from '@formatjs/ecma402-abstract'
|
|
4
|
+
|
|
5
|
+
interface Args extends minimist.ParsedArgs {
|
|
6
|
+
zone: string[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function main(args: Args) {
|
|
10
|
+
const {out} = args
|
|
11
|
+
|
|
12
|
+
outputFileSync(
|
|
13
|
+
out,
|
|
14
|
+
`/* @generated */
|
|
15
|
+
// prettier-ignore
|
|
16
|
+
export const units = ${JSON.stringify(SIMPLE_UNITS)} as const
|
|
17
|
+
export type Unit = typeof units[number]`
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (require.main === module) {
|
|
22
|
+
main(minimist<Args>(process.argv))
|
|
23
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/* @generated */
|
|
2
|
+
// prettier-ignore
|
|
3
|
+
export const calendars = ["buddhist","chinese","coptic","dangi","ethioaa","ethiopic","gregory","hebrew","indian","islamic","islamic-civil","islamic-rgsa","islamic-tbla","islamic-umalqura","islamicc","iso8601","japanese","persian","roc"] as const
|
|
4
|
+
export type Calendar = (typeof calendars)[number]
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/* @generated */
|
|
2
|
+
// prettier-ignore
|
|
3
|
+
export const collations = ["big5han","compat","dict","direct","ducet","emoji","eor","gb2312","phonebk","phonetic","pinyin","reformed","search","searchjl","standard","stroke","trad","unihan","zhuyin"] as const
|
|
4
|
+
export type Collation = (typeof collations)[number]
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/* @generated */
|
|
2
|
+
// prettier-ignore
|
|
3
|
+
export const currencies = ["ADP","AED","AFA","AFN","ALK","ALL","AMD","ANG","AOA","AOK","AON","AOR","ARA","ARL","ARM","ARP","ARS","ATS","AUD","AWG","AZM","AZN","BAD","BAM","BAN","BBD","BDT","BEC","BEF","BEL","BGL","BGM","BGN","BGO","BHD","BIF","BMD","BND","BOB","BOL","BOP","BOV","BRB","BRC","BRE","BRL","BRN","BRR","BRZ","BSD","BTN","BUK","BWP","BYB","BYN","BYR","BZD","CAD","CDF","CHE","CHF","CHW","CLE","CLF","CLP","CNH","CNX","CNY","COP","COU","CRC","CSD","CSK","CUC","CUP","CVE","CYP","CZK","DDM","DEM","DJF","DKK","DOP","DZD","ECS","ECV","EEK","EGP","ERN","ESA","ESB","ESP","ETB","EUR","FIM","FJD","FKP","FRF","GBP","GEK","GEL","GHC","GHS","GIP","GMD","GNF","GNS","GQE","GRD","GTQ","GWE","GWP","GYD","HKD","HNL","HRD","HRK","HTG","HUF","IDR","IEP","ILP","ILR","ILS","INR","IQD","IRR","ISJ","ISK","ITL","JMD","JOD","JPY","KES","KGS","KHR","KMF","KPW","KRH","KRO","KRW","KWD","KYD","KZT","LAK","LBP","LKR","LRD","LSL","LTL","LTT","LUC","LUF","LUL","LVL","LVR","LYD","MAD","MAF","MCF","MDC","MDL","MGA","MGF","MKD","MKN","MLF","MMK","MNT","MOP","MRO","MRU","MTL","MTP","MUR","MVP","MVR","MWK","MXN","MXP","MXV","MYR","MZE","MZM","MZN","NAD","NGN","NIC","NIO","NLG","NOK","NPR","NZD","OMR","PAB","PEI","PEN","PES","PGK","PHP","PKR","PLN","PLZ","PTE","PYG","QAR","RHD","ROL","RON","RSD","RUB","RUR","RWF","SAR","SBD","SCR","SDD","SDG","SDP","SEK","SGD","SHP","SIT","SKK","SLE","SLL","SOS","SRD","SRG","SSP","STD","STN","SUR","SVC","SYP","SZL","THB","TJR","TJS","TMM","TMT","TND","TOP","TPE","TRL","TRY","TTD","TWD","TZS","UAH","UAK","UGS","UGX","USD","USN","USS","UYI","UYP","UYU","UYW","UZS","VEB","VED","VEF","VES","VND","VNN","VUV","WST","XAF","XAG","XAU","XBA","XBB","XBC","XBD","XCD","XCG","XDR","XEU","XFO","XFU","XOF","XPD","XPF","XPT","XRE","XSU","XTS","XUA","XXX","YDD","YER","YUD","YUM","YUN","YUR","ZAL","ZAR","ZMK","ZMW","ZRN","ZRZ","ZWD","ZWG","ZWL","ZWR"] as const
|
|
4
|
+
export type Currency = (typeof currencies)[number]
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {createMemoizedDateTimeFormat} from './memoize.js'
|
|
2
|
+
import type {Calendar} from './calendars.generated.js'
|
|
3
|
+
import {calendars} from './calendars.generated.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Implementation: Tests if a calendar is supported by attempting to create
|
|
7
|
+
* a DateTimeFormat with that calendar and verifying it was accepted.
|
|
8
|
+
*
|
|
9
|
+
* CLDR Data: Candidate values come from CLDR calendar types
|
|
10
|
+
*/
|
|
11
|
+
function isSupportedCalendar(item: Calendar): boolean {
|
|
12
|
+
try {
|
|
13
|
+
// Always use 'en' for testing
|
|
14
|
+
const dateTimeFormat = createMemoizedDateTimeFormat(`en-u-ca-${item}`)
|
|
15
|
+
const options = dateTimeFormat.resolvedOptions().calendar
|
|
16
|
+
|
|
17
|
+
// Verify the calendar was actually accepted (resolved calendar matches requested)
|
|
18
|
+
return options === item
|
|
19
|
+
} catch {}
|
|
20
|
+
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* ECMA-402 Spec: Returns supported calendar identifiers
|
|
26
|
+
* ECMA-402 Spec: Results must be sorted lexicographically
|
|
27
|
+
*
|
|
28
|
+
* Implementation: Filters CLDR list against actual runtime support
|
|
29
|
+
*/
|
|
30
|
+
export function getSupportedCalendars(): Calendar[] {
|
|
31
|
+
return calendars.filter(isSupportedCalendar).sort()
|
|
32
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type {Collation} from './collations.generated.js'
|
|
2
|
+
import {collations} from './collations.generated.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Implementation: Tests if a collation is supported by attempting to create
|
|
6
|
+
* a Collator with that collation and verifying it was accepted.
|
|
7
|
+
*
|
|
8
|
+
* CLDR Data: Candidate values come from CLDR collation types
|
|
9
|
+
*/
|
|
10
|
+
function isSupportedCollation(collation: Collation): boolean {
|
|
11
|
+
try {
|
|
12
|
+
// Always use 'en' for testing
|
|
13
|
+
return (
|
|
14
|
+
Intl.Collator(`en-u-co-${collation}`).resolvedOptions().collation ===
|
|
15
|
+
collation
|
|
16
|
+
)
|
|
17
|
+
} catch {}
|
|
18
|
+
|
|
19
|
+
return false
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* ECMA-402 Spec: Returns supported collation identifiers
|
|
24
|
+
* ECMA-402 Spec: Results must be sorted lexicographically
|
|
25
|
+
*
|
|
26
|
+
* Implementation: Filters CLDR list against actual runtime support
|
|
27
|
+
*/
|
|
28
|
+
export function getSupportedCollations(): Collation[] {
|
|
29
|
+
return collations.filter(isSupportedCollation).sort()
|
|
30
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {createMemoizedNumberFormat} from '@formatjs/ecma402-abstract'
|
|
2
|
+
import type {Currency} from './currencies.generated.js'
|
|
3
|
+
import {currencies} from './currencies.generated.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Implementation: Tests if a currency is supported by attempting to create
|
|
7
|
+
* a NumberFormat with that currency and verifying it was accepted.
|
|
8
|
+
*
|
|
9
|
+
* CLDR Data: Candidate values come from CLDR currency codes (ISO 4217)
|
|
10
|
+
*/
|
|
11
|
+
function isSupportedCurrency(currency: Currency): boolean {
|
|
12
|
+
try {
|
|
13
|
+
// Always use 'en' for testing
|
|
14
|
+
const numberFormat = createMemoizedNumberFormat('en', {
|
|
15
|
+
style: 'currency',
|
|
16
|
+
currencyDisplay: 'name',
|
|
17
|
+
currency,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const format = numberFormat.format(123)
|
|
21
|
+
|
|
22
|
+
if (
|
|
23
|
+
format.substring(0, 3) !== currency &&
|
|
24
|
+
format.substring(format.length - 3) !== currency
|
|
25
|
+
) {
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
28
|
+
} catch {}
|
|
29
|
+
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* ECMA-402 Spec: Returns supported currency identifiers
|
|
35
|
+
* ECMA-402 Spec: Results must be sorted lexicographically
|
|
36
|
+
*
|
|
37
|
+
* Implementation: Filters CLDR list against actual runtime support
|
|
38
|
+
*/
|
|
39
|
+
export function getSupportedCurrencies(): Currency[] {
|
|
40
|
+
const ATOZ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
41
|
+
const supportedCurrencies: Currency[] = []
|
|
42
|
+
|
|
43
|
+
for (const currency of currencies) {
|
|
44
|
+
if (currency.length === 3) {
|
|
45
|
+
if (isSupportedCurrency(currency)) {
|
|
46
|
+
supportedCurrencies.push(currency)
|
|
47
|
+
}
|
|
48
|
+
} else if (currency.length === 5 && currency[3] === '~') {
|
|
49
|
+
const start = ATOZ.indexOf(currency[2])
|
|
50
|
+
const end = ATOZ.indexOf(currency[4])
|
|
51
|
+
|
|
52
|
+
for (let i = start; i <= end; i++) {
|
|
53
|
+
const currentCurrency = (currency.substring(0, 2) + ATOZ[i]) as Currency
|
|
54
|
+
if (isSupportedCurrency(currentCurrency)) {
|
|
55
|
+
supportedCurrencies.push(currentCurrency)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return supportedCurrencies.sort()
|
|
62
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {createMemoizedNumberFormat} from '@formatjs/ecma402-abstract'
|
|
2
|
+
import {numberingSystemNames} from './numbering-systems.generated.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Implementation: Tests if a numbering system is supported by attempting to create
|
|
6
|
+
* a NumberFormat with that numbering system and verifying it was accepted.
|
|
7
|
+
*
|
|
8
|
+
* CLDR Data: Candidate values come from CLDR numbering system types
|
|
9
|
+
*/
|
|
10
|
+
function isSupportedNumberingSystem(system: string): boolean {
|
|
11
|
+
try {
|
|
12
|
+
// Always use 'en' for testing
|
|
13
|
+
const numberFormat = createMemoizedNumberFormat(`en-u-nu-${system}`)
|
|
14
|
+
const options = numberFormat.resolvedOptions().numberingSystem
|
|
15
|
+
|
|
16
|
+
if (
|
|
17
|
+
(options === system && system === 'latn') ||
|
|
18
|
+
numberFormat.format(123) !== '123'
|
|
19
|
+
) {
|
|
20
|
+
return true
|
|
21
|
+
}
|
|
22
|
+
} catch {}
|
|
23
|
+
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* ECMA-402 Spec: Returns supported numbering system identifiers
|
|
29
|
+
* ECMA-402 Spec: Results must be sorted lexicographically
|
|
30
|
+
*
|
|
31
|
+
* Implementation: Filters CLDR list against actual runtime support
|
|
32
|
+
*/
|
|
33
|
+
export function getSupportedNumberingSystems(): string[] {
|
|
34
|
+
return numberingSystemNames.filter(isSupportedNumberingSystem).sort()
|
|
35
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {createMemoizedDateTimeFormat} from './memoize.js'
|
|
2
|
+
import type {Timezone} from './timezones.generated.js'
|
|
3
|
+
import {timezones} from './timezones.generated.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Implementation: Tests if a timezone is supported by attempting to create
|
|
7
|
+
* a DateTimeFormat with that timezone and verifying it was accepted.
|
|
8
|
+
*
|
|
9
|
+
* CLDR Data: Candidate values come from IANA Time Zone Database
|
|
10
|
+
*/
|
|
11
|
+
function isSupportedTimeZone(timeZone: Timezone): boolean {
|
|
12
|
+
try {
|
|
13
|
+
// Always use 'en' for testing
|
|
14
|
+
const formatter = createMemoizedDateTimeFormat('en', {timeZone})
|
|
15
|
+
return formatter.resolvedOptions().timeZone === timeZone
|
|
16
|
+
} catch {}
|
|
17
|
+
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* ECMA-402 Spec: Returns supported timezone identifiers
|
|
23
|
+
* ECMA-402 Spec: Results must be sorted lexicographically
|
|
24
|
+
*
|
|
25
|
+
* Implementation: Filters CLDR list against actual runtime support
|
|
26
|
+
*/
|
|
27
|
+
export function getSupportedTimeZones(): Timezone[] {
|
|
28
|
+
return timezones.filter(isSupportedTimeZone).sort()
|
|
29
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {createMemoizedNumberFormat} from '@formatjs/ecma402-abstract'
|
|
2
|
+
import type {Unit} from './units.generated.js'
|
|
3
|
+
import {units} from './units.generated.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Implementation: Tests if a unit is supported by attempting to create
|
|
7
|
+
* a NumberFormat with that unit and verifying it was accepted.
|
|
8
|
+
*
|
|
9
|
+
* CLDR Data: Candidate values come from CLDR unit types
|
|
10
|
+
*/
|
|
11
|
+
function isSupportedUnit(unit: Unit): boolean {
|
|
12
|
+
try {
|
|
13
|
+
// Always use 'en' for testing
|
|
14
|
+
const formatter = createMemoizedNumberFormat('en', {style: 'unit', unit})
|
|
15
|
+
return formatter.resolvedOptions().unit === unit
|
|
16
|
+
} catch {}
|
|
17
|
+
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* ECMA-402 Spec: Returns supported unit identifiers
|
|
23
|
+
* ECMA-402 Spec: Results must be sorted lexicographically
|
|
24
|
+
*
|
|
25
|
+
* Implementation: Filters CLDR list against actual runtime support
|
|
26
|
+
*/
|
|
27
|
+
export function getSupportedUnits(): (
|
|
28
|
+
| 'degree'
|
|
29
|
+
| 'acre'
|
|
30
|
+
| 'hectare'
|
|
31
|
+
| 'percent'
|
|
32
|
+
| 'bit'
|
|
33
|
+
| 'byte'
|
|
34
|
+
| 'gigabit'
|
|
35
|
+
| 'gigabyte'
|
|
36
|
+
| 'kilobit'
|
|
37
|
+
| 'kilobyte'
|
|
38
|
+
| 'megabit'
|
|
39
|
+
| 'megabyte'
|
|
40
|
+
| 'petabyte'
|
|
41
|
+
| 'terabit'
|
|
42
|
+
| 'terabyte'
|
|
43
|
+
| 'day'
|
|
44
|
+
| 'hour'
|
|
45
|
+
| 'millisecond'
|
|
46
|
+
| 'minute'
|
|
47
|
+
| 'month'
|
|
48
|
+
| 'second'
|
|
49
|
+
| 'week'
|
|
50
|
+
| 'year'
|
|
51
|
+
| 'centimeter'
|
|
52
|
+
| 'foot'
|
|
53
|
+
| 'inch'
|
|
54
|
+
| 'kilometer'
|
|
55
|
+
| 'meter'
|
|
56
|
+
| 'mile-scandinavian'
|
|
57
|
+
| 'mile'
|
|
58
|
+
| 'millimeter'
|
|
59
|
+
| 'yard'
|
|
60
|
+
| 'gram'
|
|
61
|
+
| 'kilogram'
|
|
62
|
+
| 'ounce'
|
|
63
|
+
| 'pound'
|
|
64
|
+
| 'stone'
|
|
65
|
+
| 'celsius'
|
|
66
|
+
| 'fahrenheit'
|
|
67
|
+
| 'fluid-ounce'
|
|
68
|
+
| 'gallon'
|
|
69
|
+
| 'liter'
|
|
70
|
+
| 'milliliter'
|
|
71
|
+
)[] {
|
|
72
|
+
return units.filter(isSupportedUnit).sort()
|
|
73
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {getSupportedCalendars} from './get-supported-calendars.js'
|
|
2
|
+
import {getSupportedCollations} from './get-supported-collations.js'
|
|
3
|
+
import {getSupportedCurrencies} from './get-supported-currencies.js'
|
|
4
|
+
import {getSupportedNumberingSystems} from './get-supported-numbering-systems.js'
|
|
5
|
+
import {getSupportedTimeZones} from './get-supported-timezones.js'
|
|
6
|
+
import {getSupportedUnits} from './get-supported-units.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* ECMA-402 Spec: Intl.supportedValuesOf
|
|
10
|
+
* https://tc39.es/ecma402/#sec-intl.supportedvaluesof
|
|
11
|
+
*/
|
|
12
|
+
declare global {
|
|
13
|
+
namespace Intl {
|
|
14
|
+
/**
|
|
15
|
+
* Returns an array containing the supported values for the given key.
|
|
16
|
+
* @param key - The category of values to return
|
|
17
|
+
* @returns A sorted array of strings
|
|
18
|
+
*/
|
|
19
|
+
function supportedValuesOf(
|
|
20
|
+
key:
|
|
21
|
+
| 'calendar'
|
|
22
|
+
| 'collation'
|
|
23
|
+
| 'currency'
|
|
24
|
+
| 'numberingSystem'
|
|
25
|
+
| 'timeZone'
|
|
26
|
+
| 'unit'
|
|
27
|
+
): string[]
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* ECMA-402 Spec: Supported value categories
|
|
33
|
+
*/
|
|
34
|
+
export type SupportedValuesOf =
|
|
35
|
+
| 'calendar'
|
|
36
|
+
| 'collation'
|
|
37
|
+
| 'currency'
|
|
38
|
+
| 'numberingSystem'
|
|
39
|
+
| 'timeZone'
|
|
40
|
+
| 'unit'
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* ECMA-402 Spec: Intl.supportedValuesOf(key)
|
|
44
|
+
* https://tc39.es/ecma402/#sec-intl.supportedvaluesof
|
|
45
|
+
*
|
|
46
|
+
* Returns an array containing the supported calendar, collation, currency,
|
|
47
|
+
* numbering systems, time zone, or unit values supported by the implementation.
|
|
48
|
+
*
|
|
49
|
+
* Implementation: We validate candidate values from CLDR against the actual
|
|
50
|
+
* Intl formatters to determine runtime support rather than using static lists.
|
|
51
|
+
* This ensures accuracy across different JavaScript engines and runtimes.
|
|
52
|
+
*
|
|
53
|
+
* @param key - The category of values to return
|
|
54
|
+
* @returns A sorted array of unique string values
|
|
55
|
+
*/
|
|
56
|
+
export function supportedValuesOf(key: SupportedValuesOf): string[] {
|
|
57
|
+
// ECMA-402 Spec: Dispatch to appropriate getter based on key
|
|
58
|
+
switch (key) {
|
|
59
|
+
case 'calendar':
|
|
60
|
+
return getSupportedCalendars()
|
|
61
|
+
case 'collation':
|
|
62
|
+
return getSupportedCollations()
|
|
63
|
+
case 'currency':
|
|
64
|
+
return getSupportedCurrencies()
|
|
65
|
+
case 'numberingSystem':
|
|
66
|
+
return getSupportedNumberingSystems()
|
|
67
|
+
case 'timeZone':
|
|
68
|
+
return getSupportedTimeZones()
|
|
69
|
+
case 'unit':
|
|
70
|
+
return getSupportedUnits()
|
|
71
|
+
default:
|
|
72
|
+
// ECMA-402 Spec: Throw RangeError for invalid keys
|
|
73
|
+
throw RangeError('Invalid key: ' + key)
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/memoize.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {memoize, strategies} from '@formatjs/fast-memoize'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Implementation: Memoized factory for Intl.DateTimeFormat instances
|
|
5
|
+
*
|
|
6
|
+
* Creates and caches DateTimeFormat instances to avoid repeated instantiation overhead.
|
|
7
|
+
* This is critical for performance since we test many candidate values against formatters.
|
|
8
|
+
*
|
|
9
|
+
* Uses variadic memoization strategy to handle varying numbers of constructor arguments.
|
|
10
|
+
*/
|
|
11
|
+
export const createMemoizedDateTimeFormat: (
|
|
12
|
+
...args: ConstructorParameters<typeof Intl.DateTimeFormat>
|
|
13
|
+
) => Intl.DateTimeFormat = memoize(
|
|
14
|
+
(...args: ConstructorParameters<typeof Intl.DateTimeFormat>) =>
|
|
15
|
+
new Intl.DateTimeFormat(...args),
|
|
16
|
+
{
|
|
17
|
+
strategy: strategies.variadic,
|
|
18
|
+
}
|
|
19
|
+
)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export const numberingSystemNames: ReadonlyArray<string> = [
|
|
2
|
+
'adlm',
|
|
3
|
+
'ahom',
|
|
4
|
+
'arab',
|
|
5
|
+
'arabext',
|
|
6
|
+
'armn',
|
|
7
|
+
'armnlow',
|
|
8
|
+
'bali',
|
|
9
|
+
'beng',
|
|
10
|
+
'bhks',
|
|
11
|
+
'brah',
|
|
12
|
+
'cakm',
|
|
13
|
+
'cham',
|
|
14
|
+
'cyrl',
|
|
15
|
+
'deva',
|
|
16
|
+
'diak',
|
|
17
|
+
'ethi',
|
|
18
|
+
'fullwide',
|
|
19
|
+
'gara',
|
|
20
|
+
'geor',
|
|
21
|
+
'gong',
|
|
22
|
+
'gonm',
|
|
23
|
+
'grek',
|
|
24
|
+
'greklow',
|
|
25
|
+
'gujr',
|
|
26
|
+
'gukh',
|
|
27
|
+
'guru',
|
|
28
|
+
'hanidays',
|
|
29
|
+
'hanidec',
|
|
30
|
+
'hans',
|
|
31
|
+
'hansfin',
|
|
32
|
+
'hant',
|
|
33
|
+
'hantfin',
|
|
34
|
+
'hebr',
|
|
35
|
+
'hmng',
|
|
36
|
+
'hmnp',
|
|
37
|
+
'java',
|
|
38
|
+
'jpan',
|
|
39
|
+
'jpanfin',
|
|
40
|
+
'jpanyear',
|
|
41
|
+
'kali',
|
|
42
|
+
'kawi',
|
|
43
|
+
'khmr',
|
|
44
|
+
'knda',
|
|
45
|
+
'krai',
|
|
46
|
+
'lana',
|
|
47
|
+
'lanatham',
|
|
48
|
+
'laoo',
|
|
49
|
+
'latn',
|
|
50
|
+
'lepc',
|
|
51
|
+
'limb',
|
|
52
|
+
'mathbold',
|
|
53
|
+
'mathdbl',
|
|
54
|
+
'mathmono',
|
|
55
|
+
'mathsanb',
|
|
56
|
+
'mathsans',
|
|
57
|
+
'mlym',
|
|
58
|
+
'modi',
|
|
59
|
+
'mong',
|
|
60
|
+
'mroo',
|
|
61
|
+
'mtei',
|
|
62
|
+
'mymr',
|
|
63
|
+
'mymrepka',
|
|
64
|
+
'mymrpao',
|
|
65
|
+
'mymrshan',
|
|
66
|
+
'mymrtlng',
|
|
67
|
+
'nagm',
|
|
68
|
+
'newa',
|
|
69
|
+
'nkoo',
|
|
70
|
+
'olck',
|
|
71
|
+
'onao',
|
|
72
|
+
'orya',
|
|
73
|
+
'osma',
|
|
74
|
+
'outlined',
|
|
75
|
+
'rohg',
|
|
76
|
+
'roman',
|
|
77
|
+
'romanlow',
|
|
78
|
+
'saur',
|
|
79
|
+
'segment',
|
|
80
|
+
'shrd',
|
|
81
|
+
'sind',
|
|
82
|
+
'sinh',
|
|
83
|
+
'sora',
|
|
84
|
+
'sund',
|
|
85
|
+
'sunu',
|
|
86
|
+
'takr',
|
|
87
|
+
'talu',
|
|
88
|
+
'taml',
|
|
89
|
+
'tamldec',
|
|
90
|
+
'telu',
|
|
91
|
+
'thai',
|
|
92
|
+
'tibt',
|
|
93
|
+
'tirh',
|
|
94
|
+
'tnsa',
|
|
95
|
+
'tols',
|
|
96
|
+
'vaii',
|
|
97
|
+
'wara',
|
|
98
|
+
'wcho',
|
|
99
|
+
]
|