1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/**
* @typedef {import('bcp-47').Warning} Warning
* @typedef {import('bcp-47').Schema} Schema
* @typedef {import('bcp-47').Extension} Extension
*
* @typedef Options
* Configuration (optional).
* @property {boolean} [forgiving]
* Passed to `bcp-47` as `options.forgiving`.
* @property {Warning} [warning]
* Passed to `bcp-47` as `options.warning`.
*
* One additional warning is given:
*
* | code | reason |
* | :--- | :--------------------------------------------------------- |
* | 7 | Deprecated region `CURRENT`, expected one of `SUGGESTIONS` |
*
* This warning is only given if the region cannot be automatically fixed
* (when regions split into multiple regions).
*/
import {parse, stringify} from 'bcp-47'
import {extendedFilter} from 'bcp-47-match'
import {matches} from './matches.js'
import {fields} from './fields.js'
import {many} from './many.js'
import {likely} from './likely.js'
const own = {}.hasOwnProperty
/**
* @param {Schema} base
* @param {Partial<Schema>} changes
* @returns {Schema}
*/
function merge(base, changes) {
if (!base.language) base.language = changes.language
if (!base.script) base.script = changes.script
if (!base.region) base.region = changes.region
if (changes.variants) base.variants.push(...changes.variants)
return base
}
/**
* Mostly like:
* <https://github.com/formatjs/formatjs/blob/a15e757/packages/intl-locale/index.ts#L254>
* But doesn’t crash.
*
* @param {Schema} schema
* @returns {string}
*/
function addLikelySubtags(schema) {
const {language, script, region} = schema
/** @type {string|undefined} */
let match
if (
script &&
region &&
(match = likely[stringify({language, script, region})])
) {
schema.script = undefined
schema.region = undefined
} else if (script && (match = likely[stringify({language, script})])) {
schema.script = undefined
} else if (region && (match = likely[stringify({language, region})])) {
schema.region = undefined
} else if (language && (match = likely[language])) {
// Empty.
}
if (match) {
schema.language = undefined
merge(schema, parse(match))
}
return stringify(schema)
}
/**
* @param {Schema} schema
*/
function removeLikelySubtags(schema) {
addLikelySubtags(schema)
const {language, script, region} = schema
if (!language) return schema
const maxLocale = stringify({language, script, region})
if (maxLocale === addLikelySubtags(parse(language))) {
schema.script = undefined
schema.region = undefined
} else if (
region &&
maxLocale === addLikelySubtags(parse(language + '-' + region))
) {
schema.script = undefined
} else if (
script &&
maxLocale === addLikelySubtags(parse(language + '-' + script))
) {
schema.region = undefined
}
return schema
}
/**
* Normalize the given BCP 47 tag according to Unicode CLDR suggestions.
*
* @param {string} tag
* BCP 47 tag.
* @param {Options} [options]
* Configuration (optional).
* @returns {string}
* Normal, canonical, and pretty BCP 47 tag.
*/
export function bcp47Normalize(tag, options) {
const settings = options || {}
// 1. normalize and lowercase the tag (`sgn-be-fr` -> `sfb`).
const schema = parse(String(tag || '').toLowerCase(), settings)
const value = stringify(schema)
if (!value) {
return value
}
let index = -1
// 2. Do fancy, expensive replaces (`ha-latn-gh` -> `ha-gh`).
while (++index < matches.length) {
let from = matches[index].from
if (from.slice(0, 4) === 'und-' && schema.language) {
from = schema.language + from.slice(3)
}
if (extendedFilter(value, from).length > 0) {
replace(schema, from, matches[index].to)
}
}
// 3. Do basic field replaces (`en-840` -> `en-us`).
index = -1
while (++index < fields.length) {
if (remove(schema, fields[index].from.field, fields[index].from.value)) {
add(schema, fields[index].to.field, fields[index].to.value)
}
}
// 4. Minimize.
removeLikelySubtags(schema)
// 5. Sort variants, and sort extensions on singleton.
schema.variants.sort()
schema.extensions.sort(compareSingleton)
// 6. Warn if fields (currently only regions) should be updated but have
// multiple choices.
if (settings.warning) {
/** @type {keyof many} */
let key
for (key in many) {
if (own.call(many, key)) {
const map = many[key]
const value = schema[key]
if (value && own.call(map, value)) {
const replacements = map[value]
settings.warning(
'Deprecated ' +
key +
' `' +
value +
'`, expected one of `' +
replacements.join('`, `') +
'`',
-1,
7
)
}
}
}
}
// 7. Add proper casing back.
// Format script (ISO 15924) as titlecase (example: `Latn`):
if (schema.script) {
schema.script =
schema.script.charAt(0).toUpperCase() + schema.script.slice(1)
}
// Format region (ISO 3166) as uppercase (note: this doesn’t affect numeric
// codes, which is fine):
if (schema.region) {
schema.region = schema.region.toUpperCase()
}
return stringify(schema)
}
/**
* @param {Schema} schema
* @param {string} from
* @param {string} to
* @returns {void}
*/
function replace(schema, from, to) {
const left = parse(from)
const right = parse(to)
/** @type {Array<string>} */
const removed = []
/** @type {string|null|undefined} */
const lang = left.language
/** @type {keyof schema} */
let key
// Remove values from `from`:
for (key in left) {
if (own.call(left, key)) {
const value = left[key]
if (value && remove(schema, key, value)) {
removed.push(key)
}
}
}
// Add values from `to`:
for (key in right) {
if (own.call(right, key)) {
const value = right[key]
// Only add values that are defined on `to`, and that were either removed by
// `from` or are currently empty.
if (lang && value && (removed.includes(key) || !schema[key])) {
add(schema, key, key === 'language' && value === 'und' ? lang : value)
}
}
}
}
/**
* @param {Schema} object
* @param {keyof Schema} key
* @param {string|Array<string>|Array<Extension>} value
* @returns {boolean}
*/
function remove(object, key, value) {
let removed = false
/** @type {string|Array<string>|Array<Extension>|null|undefined} */
let result
if (value) {
const current = object[key]
result = current
if (Array.isArray(current)) {
result = []
let index = -1
while (++index < current.length) {
const item = current[index]
// @ts-expect-error: TS can’t handle the two lists.
if (value.includes(item)) {
removed = true
} else {
// @ts-expect-error: TS can’t handle the two lists.
result.push(item)
}
}
} else if (current === value) {
result = null
removed = true
}
// @ts-expect-error: Assume the value matches.
object[key] = result
}
return removed
}
/**
* @param {Schema} object
* @param {keyof Schema} key
* @param {string|Array<string>|Array<Extension>} value
* @returns {void}
*/
function add(object, key, value) {
/** @type {string|Array<string>|Array<Extension>|null|undefined} */
const current = object[key]
if (Array.isArray(current)) {
const list = Array.isArray(value) ? value : [value]
/** @type {number} */
let index = -1
while (++index < list.length) {
const item = list[index]
// @ts-expect-error: TS can’t handle the two lists.
if (!current.includes(item)) {
// @ts-expect-error: TS can’t handle the two lists.
current.push(item)
}
}
} else {
// @ts-expect-error: Assume the value matches.
object[key] = value
}
}
/**
* @param {Extension} left
* @param {Extension} right
* @returns {number}
*/
function compareSingleton(left, right) {
if (left.singleton > right.singleton) {
return 1
}
if (left.singleton < right.singleton) {
return -1
}
// It is invalid to have more than one extension with the same singleton so
// we should never reach this code.
return 0
}
|