@formatjs/intl-localematcher 0.2.29 → 0.2.30

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.
Files changed (78) hide show
  1. package/abstract/BestAvailableLocale.d.ts +7 -0
  2. package/abstract/BestAvailableLocale.d.ts.map +1 -0
  3. package/abstract/BestAvailableLocale.js +25 -0
  4. package/abstract/BestFitMatcher.d.ts +9 -0
  5. package/abstract/BestFitMatcher.d.ts.map +1 -0
  6. package/abstract/BestFitMatcher.js +65 -0
  7. package/abstract/CanonicalizeLocaleList.d.ts +6 -0
  8. package/abstract/CanonicalizeLocaleList.d.ts.map +1 -0
  9. package/abstract/CanonicalizeLocaleList.js +12 -0
  10. package/abstract/LookupMatcher.d.ts +9 -0
  11. package/abstract/LookupMatcher.d.ts.map +1 -0
  12. package/abstract/LookupMatcher.js +29 -0
  13. package/abstract/LookupSupportedLocales.d.ts +7 -0
  14. package/abstract/LookupSupportedLocales.d.ts.map +1 -0
  15. package/abstract/LookupSupportedLocales.js +23 -0
  16. package/abstract/ResolveLocale.d.ts +15 -0
  17. package/abstract/ResolveLocale.d.ts.map +1 -0
  18. package/abstract/ResolveLocale.js +78 -0
  19. package/abstract/UnicodeExtensionValue.d.ts +7 -0
  20. package/abstract/UnicodeExtensionValue.d.ts.map +1 -0
  21. package/abstract/UnicodeExtensionValue.js +50 -0
  22. package/abstract/types.d.ts +6 -0
  23. package/abstract/types.d.ts.map +1 -0
  24. package/abstract/types.js +2 -0
  25. package/abstract/utils.d.ts +3 -0
  26. package/abstract/utils.d.ts.map +1 -0
  27. package/abstract/utils.js +11 -0
  28. package/index.d.ts +7 -0
  29. package/index.d.ts.map +1 -0
  30. package/index.js +19 -0
  31. package/lib/abstract/BestAvailableLocale.d.ts +7 -0
  32. package/lib/abstract/BestAvailableLocale.d.ts.map +1 -0
  33. package/lib/abstract/BestAvailableLocale.js +21 -0
  34. package/lib/abstract/BestFitMatcher.d.ts +9 -0
  35. package/lib/abstract/BestFitMatcher.d.ts.map +1 -0
  36. package/lib/abstract/BestFitMatcher.js +61 -0
  37. package/lib/abstract/CanonicalizeLocaleList.d.ts +6 -0
  38. package/lib/abstract/CanonicalizeLocaleList.d.ts.map +1 -0
  39. package/lib/abstract/CanonicalizeLocaleList.js +8 -0
  40. package/lib/abstract/LookupMatcher.d.ts +9 -0
  41. package/lib/abstract/LookupMatcher.d.ts.map +1 -0
  42. package/lib/abstract/LookupMatcher.js +25 -0
  43. package/lib/abstract/LookupSupportedLocales.d.ts +7 -0
  44. package/lib/abstract/LookupSupportedLocales.d.ts.map +1 -0
  45. package/lib/abstract/LookupSupportedLocales.js +19 -0
  46. package/lib/abstract/ResolveLocale.d.ts +15 -0
  47. package/lib/abstract/ResolveLocale.d.ts.map +1 -0
  48. package/lib/abstract/ResolveLocale.js +74 -0
  49. package/lib/abstract/UnicodeExtensionValue.d.ts +7 -0
  50. package/lib/abstract/UnicodeExtensionValue.d.ts.map +1 -0
  51. package/lib/abstract/UnicodeExtensionValue.js +46 -0
  52. package/lib/abstract/types.d.ts +6 -0
  53. package/lib/abstract/types.d.ts.map +1 -0
  54. package/lib/abstract/types.js +1 -0
  55. package/lib/abstract/utils.d.ts +3 -0
  56. package/lib/abstract/utils.d.ts.map +1 -0
  57. package/lib/abstract/utils.js +7 -0
  58. package/lib/index.d.ts +7 -0
  59. package/lib/index.d.ts.map +1 -0
  60. package/lib/index.js +13 -0
  61. package/package.json +1 -1
  62. package/BUILD +0 -73
  63. package/CHANGELOG.md +0 -150
  64. package/abstract/BestAvailableLocale.ts +0 -24
  65. package/abstract/BestFitMatcher.ts +0 -80
  66. package/abstract/CanonicalizeLocaleList.ts +0 -8
  67. package/abstract/LookupMatcher.ts +0 -39
  68. package/abstract/LookupSupportedLocales.ts +0 -28
  69. package/abstract/ResolveLocale.ts +0 -100
  70. package/abstract/UnicodeExtensionValue.ts +0 -44
  71. package/abstract/types.ts +0 -5
  72. package/abstract/utils.ts +0 -11
  73. package/index.ts +0 -32
  74. package/tests/BestFitMatcher.test.ts +0 -23
  75. package/tests/LookupMatcher.test.ts +0 -17
  76. package/tests/ResolveLocale.test.ts +0 -47
  77. package/tests/index.test.ts +0 -242
  78. package/tsconfig.json +0 -5
@@ -1,100 +0,0 @@
1
- import {LookupMatcherResult} from './types'
2
- import {LookupMatcher} from './LookupMatcher'
3
- import {BestFitMatcher} from './BestFitMatcher'
4
- import {invariant} from './utils'
5
- import {UnicodeExtensionValue} from './UnicodeExtensionValue'
6
-
7
- export interface ResolveLocaleResult {
8
- locale: string
9
- dataLocale: string
10
- [k: string]: any
11
- }
12
-
13
- /**
14
- * https://tc39.es/ecma402/#sec-resolvelocale
15
- */
16
- export function ResolveLocale<K extends string, D extends {[k in K]: any}>(
17
- availableLocales: Set<string>,
18
- requestedLocales: string[],
19
- options: {localeMatcher: string; [k: string]: string},
20
- relevantExtensionKeys: K[],
21
- localeData: Record<string, D | undefined>,
22
- getDefaultLocale: () => string
23
- ): ResolveLocaleResult {
24
- const matcher = options.localeMatcher
25
- let r: LookupMatcherResult
26
- if (matcher === 'lookup') {
27
- r = LookupMatcher(availableLocales, requestedLocales, getDefaultLocale)
28
- } else {
29
- r = BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale)
30
- }
31
- let foundLocale = r.locale
32
- const result: ResolveLocaleResult = {locale: '', dataLocale: foundLocale}
33
- let supportedExtension = '-u'
34
- for (const key of relevantExtensionKeys) {
35
- invariant(
36
- foundLocale in localeData,
37
- `Missing locale data for ${foundLocale}`
38
- )
39
- const foundLocaleData = localeData[foundLocale]
40
- invariant(
41
- typeof foundLocaleData === 'object' && foundLocaleData !== null,
42
- `locale data ${key} must be an object`
43
- )
44
- const keyLocaleData = foundLocaleData[key]
45
- invariant(
46
- Array.isArray(keyLocaleData),
47
- `keyLocaleData for ${key} must be an array`
48
- )
49
- let value = keyLocaleData[0]
50
- invariant(
51
- typeof value === 'string' || value === null,
52
- `value must be string or null but got ${typeof value} in key ${key}`
53
- )
54
- let supportedExtensionAddition = ''
55
- if (r.extension) {
56
- const requestedValue = UnicodeExtensionValue(r.extension, key)
57
- if (requestedValue !== undefined) {
58
- if (requestedValue !== '') {
59
- if (~keyLocaleData.indexOf(requestedValue)) {
60
- value = requestedValue
61
- supportedExtensionAddition = `-${key}-${value}`
62
- }
63
- } else if (~requestedValue.indexOf('true')) {
64
- value = 'true'
65
- supportedExtensionAddition = `-${key}`
66
- }
67
- }
68
- }
69
- if (key in options) {
70
- const optionsValue = options[key]
71
- invariant(
72
- typeof optionsValue === 'string' ||
73
- typeof optionsValue === 'undefined' ||
74
- optionsValue === null,
75
- 'optionsValue must be String, Undefined or Null'
76
- )
77
- if (~keyLocaleData.indexOf(optionsValue)) {
78
- if (optionsValue !== value) {
79
- value = optionsValue
80
- supportedExtensionAddition = ''
81
- }
82
- }
83
- }
84
- result[key] = value
85
- supportedExtension += supportedExtensionAddition
86
- }
87
- if (supportedExtension.length > 2) {
88
- const privateIndex = foundLocale.indexOf('-x-')
89
- if (privateIndex === -1) {
90
- foundLocale = foundLocale + supportedExtension
91
- } else {
92
- const preExtension = foundLocale.slice(0, privateIndex)
93
- const postExtension = foundLocale.slice(privateIndex, foundLocale.length)
94
- foundLocale = preExtension + supportedExtension + postExtension
95
- }
96
- foundLocale = (Intl as any).getCanonicalLocales(foundLocale)[0]
97
- }
98
- result.locale = foundLocale
99
- return result
100
- }
@@ -1,44 +0,0 @@
1
- import {invariant} from './utils'
2
-
3
- /**
4
- * https://tc39.es/ecma402/#sec-unicodeextensionvalue
5
- * @param extension
6
- * @param key
7
- */
8
- export function UnicodeExtensionValue(extension: string, key: string) {
9
- invariant(key.length === 2, 'key must have 2 elements')
10
- const size = extension.length
11
- let searchValue = `-${key}-`
12
- let pos = extension.indexOf(searchValue)
13
- if (pos !== -1) {
14
- const start = pos + 4
15
- let end = start
16
- let k = start
17
- let done = false
18
- while (!done) {
19
- const e = extension.indexOf('-', k)
20
- let len
21
- if (e === -1) {
22
- len = size - k
23
- } else {
24
- len = e - k
25
- }
26
- if (len === 2) {
27
- done = true
28
- } else if (e === -1) {
29
- end = size
30
- done = true
31
- } else {
32
- end = e
33
- k = e + 1
34
- }
35
- }
36
- return extension.slice(start, end)
37
- }
38
- searchValue = `-${key}`
39
- pos = extension.indexOf(searchValue)
40
- if (pos !== -1 && pos + 3 === size) {
41
- return ''
42
- }
43
- return undefined
44
- }
package/abstract/types.ts DELETED
@@ -1,5 +0,0 @@
1
- export interface LookupMatcherResult {
2
- locale: string
3
- extension?: string
4
- nu?: string
5
- }
package/abstract/utils.ts DELETED
@@ -1,11 +0,0 @@
1
- export const UNICODE_EXTENSION_SEQUENCE_REGEX = /-u(?:-[0-9a-z]{2,8})+/gi
2
-
3
- export function invariant(
4
- condition: boolean,
5
- message: string,
6
- Err: any = Error
7
- ): asserts condition {
8
- if (!condition) {
9
- throw new Err(message)
10
- }
11
- }
package/index.ts DELETED
@@ -1,32 +0,0 @@
1
- import {CanonicalizeLocaleList} from './abstract/CanonicalizeLocaleList'
2
- import {ResolveLocale} from './abstract/ResolveLocale'
3
-
4
- export interface Opts {
5
- algorithm: 'lookup' | 'best fit'
6
- }
7
-
8
- export function match(
9
- requestedLocales: string[],
10
- availableLocales: string[],
11
- defaultLocale: string,
12
- opts?: Opts
13
- ): string {
14
- const locales = availableLocales.reduce((all, l) => {
15
- all.add(l)
16
- return all
17
- }, new Set<string>())
18
-
19
- return ResolveLocale(
20
- locales,
21
- CanonicalizeLocaleList(requestedLocales),
22
- {
23
- localeMatcher: opts?.algorithm || 'best fit',
24
- },
25
- [],
26
- {},
27
- () => defaultLocale
28
- ).locale
29
- }
30
-
31
- export {LookupSupportedLocales} from './abstract/LookupSupportedLocales'
32
- export {ResolveLocale} from './abstract/ResolveLocale'
@@ -1,23 +0,0 @@
1
- import {BestFitMatcher} from '../abstract/BestFitMatcher'
2
-
3
- test('BestFitMatcher', function () {
4
- expect(
5
- BestFitMatcher(new Set(['fr', 'en']), ['fr-XX', 'en'], () => 'en')
6
- ).toEqual({
7
- locale: 'fr',
8
- })
9
- })
10
-
11
- test('BestFitMatcher zh-TW', function () {
12
- expect(
13
- BestFitMatcher(new Set(['zh', 'zh-Hant']), ['zh-TW'], () => 'en')
14
- ).toEqual({
15
- locale: 'zh-Hant',
16
- })
17
- })
18
-
19
- test('BestFitMatcher en', function () {
20
- expect(BestFitMatcher(new Set(['en', 'und']), ['en'], () => 'en')).toEqual({
21
- locale: 'en',
22
- })
23
- })
@@ -1,17 +0,0 @@
1
- import {LookupMatcher} from '../abstract/LookupMatcher'
2
-
3
- test('LookupMatcher', function () {
4
- expect(
5
- LookupMatcher(new Set(['fr', 'en']), ['fr-XX', 'en'], () => 'en')
6
- ).toEqual({
7
- locale: 'fr',
8
- })
9
- })
10
-
11
- test('LookupMatcher', function () {
12
- expect(
13
- LookupMatcher(new Set(['zh', 'zh-Hant']), ['zh-Hans'], () => 'en')
14
- ).toEqual({
15
- locale: 'zh',
16
- })
17
- })
@@ -1,47 +0,0 @@
1
- import {ResolveLocale} from '../abstract/ResolveLocale'
2
-
3
- test('ResolveLocale', function () {
4
- expect(
5
- ResolveLocale(
6
- new Set(['fr', 'en']),
7
- ['fr-XX', 'en'],
8
- {localeMatcher: 'best fit'},
9
- [],
10
- {},
11
- () => 'en'
12
- )
13
- ).toEqual({
14
- dataLocale: 'fr',
15
- locale: 'fr',
16
- })
17
-
18
- expect(
19
- ResolveLocale(
20
- new Set(['zh-Hant-TW', 'en']),
21
- ['zh-TW', 'en'],
22
- {localeMatcher: 'best fit'},
23
- [],
24
- {},
25
- () => 'en'
26
- )
27
- ).toEqual({
28
- dataLocale: 'zh-Hant-TW',
29
- locale: 'zh-Hant-TW',
30
- })
31
- })
32
-
33
- test('empty requested', function () {
34
- expect(
35
- ResolveLocale(
36
- new Set(['zh-Hant-TW', 'en']),
37
- [],
38
- {localeMatcher: 'best fit'},
39
- [],
40
- {},
41
- () => 'en'
42
- )
43
- ).toEqual({
44
- dataLocale: 'en',
45
- locale: 'en',
46
- })
47
- })
@@ -1,242 +0,0 @@
1
- import {match} from '../'
2
-
3
- test('zh-HK', function () {
4
- expect(match(['zh-HK'], ['zh', 'zh-HANT', 'en'], 'en')).toEqual('zh-HANT')
5
- })
6
-
7
- test('Intl.LocaleMatcher', function () {
8
- expect(match(['fr-XX', 'en'], ['fr', 'en'], 'en')).toEqual('fr')
9
- expect(match(['zh-TW', 'en'], ['zh-Hant-TW', 'en'], 'en')).toEqual(
10
- 'zh-Hant-TW'
11
- )
12
- expect(
13
- match(
14
- ['sr-Latn-BA'],
15
- [
16
- 'af',
17
- 'ak',
18
- 'am',
19
- 'an',
20
- 'ar',
21
- 'ars',
22
- 'as',
23
- 'asa',
24
- 'ast',
25
- 'az',
26
- 'be',
27
- 'bem',
28
- 'bez',
29
- 'bg',
30
- 'bho',
31
- 'bm',
32
- 'bn',
33
- 'bo',
34
- 'br',
35
- 'brx',
36
- 'bs',
37
- 'ca',
38
- 'ce',
39
- 'ceb',
40
- 'cgg',
41
- 'chr',
42
- 'ckb',
43
- 'cs',
44
- 'cy',
45
- 'da',
46
- 'de',
47
- 'doi',
48
- 'dsb',
49
- 'dv',
50
- 'dz',
51
- 'ee',
52
- 'el',
53
- 'en',
54
- 'eo',
55
- 'es',
56
- 'et',
57
- 'eu',
58
- 'fa',
59
- 'ff',
60
- 'fi',
61
- 'fil',
62
- 'fo',
63
- 'fr',
64
- 'fur',
65
- 'fy',
66
- 'ga',
67
- 'gd',
68
- 'gl',
69
- 'gsw',
70
- 'gu',
71
- 'guw',
72
- 'gv',
73
- 'ha',
74
- 'haw',
75
- 'he',
76
- 'hi',
77
- 'hr',
78
- 'hsb',
79
- 'hu',
80
- 'hy',
81
- 'ia',
82
- 'id',
83
- 'ig',
84
- 'ii',
85
- 'in',
86
- 'io',
87
- 'is',
88
- 'it',
89
- 'iu',
90
- 'iw',
91
- 'ja',
92
- 'jbo',
93
- 'jgo',
94
- 'ji',
95
- 'jmc',
96
- 'jv',
97
- 'jw',
98
- 'ka',
99
- 'kab',
100
- 'kaj',
101
- 'kcg',
102
- 'kde',
103
- 'kea',
104
- 'kk',
105
- 'kkj',
106
- 'kl',
107
- 'km',
108
- 'kn',
109
- 'ko',
110
- 'ks',
111
- 'ksb',
112
- 'ksh',
113
- 'ku',
114
- 'kw',
115
- 'ky',
116
- 'lag',
117
- 'lb',
118
- 'lg',
119
- 'lij',
120
- 'lkt',
121
- 'ln',
122
- 'lo',
123
- 'lt',
124
- 'lv',
125
- 'mas',
126
- 'mg',
127
- 'mgo',
128
- 'mk',
129
- 'ml',
130
- 'mn',
131
- 'mo',
132
- 'mr',
133
- 'ms',
134
- 'mt',
135
- 'my',
136
- 'nah',
137
- 'naq',
138
- 'nb',
139
- 'nd',
140
- 'ne',
141
- 'nl',
142
- 'nn',
143
- 'nnh',
144
- 'no',
145
- 'nqo',
146
- 'nr',
147
- 'nso',
148
- 'ny',
149
- 'nyn',
150
- 'om',
151
- 'or',
152
- 'os',
153
- 'osa',
154
- 'pa',
155
- 'pap',
156
- 'pcm',
157
- 'pl',
158
- 'prg',
159
- 'ps',
160
- 'pt-PT',
161
- 'pt',
162
- 'rm',
163
- 'ro',
164
- 'rof',
165
- 'ru',
166
- 'rwk',
167
- 'sah',
168
- 'saq',
169
- 'sat',
170
- 'sc',
171
- 'scn',
172
- 'sd',
173
- 'sdh',
174
- 'se',
175
- 'seh',
176
- 'ses',
177
- 'sg',
178
- 'sh',
179
- 'shi',
180
- 'si',
181
- 'sk',
182
- 'sl',
183
- 'sma',
184
- 'smi',
185
- 'smj',
186
- 'smn',
187
- 'sms',
188
- 'sn',
189
- 'so',
190
- 'sq',
191
- 'sr',
192
- 'ss',
193
- 'ssy',
194
- 'st',
195
- 'su',
196
- 'sv',
197
- 'sw',
198
- 'syr',
199
- 'ta',
200
- 'te',
201
- 'teo',
202
- 'th',
203
- 'ti',
204
- 'tig',
205
- 'tk',
206
- 'tl',
207
- 'tn',
208
- 'to',
209
- 'tr',
210
- 'ts',
211
- 'tzm',
212
- 'ug',
213
- 'uk',
214
- 'ur',
215
- 'uz',
216
- 've',
217
- 'vi',
218
- 'vo',
219
- 'vun',
220
- 'wa',
221
- 'wae',
222
- 'wo',
223
- 'xh',
224
- 'xog',
225
- 'yi',
226
- 'yo',
227
- 'yue',
228
- 'zh',
229
- 'zu',
230
- ],
231
- 'en'
232
- )
233
- ).toEqual('sh')
234
- })
235
-
236
- test('empty requested', function () {
237
- expect(match([], ['zh-Hant-TW', 'en'], 'en')).toEqual('en')
238
- })
239
-
240
- test('extension', function () {
241
- expect(match(['fr-CA-x-foo'], ['zh-Hant-TW', 'fr', 'en'], 'en')).toEqual('fr')
242
- })
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- // @generated
2
- {
3
- // This is purely for IDE, not for compilation
4
- "extends": "../../tsconfig.json"
5
- }