@formatjs/intl-getcanonicallocales 3.2.2 → 3.2.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/src/parser.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import { type UnicodeLocaleId, type UnicodeLanguageId } from "./types.js";
2
- export declare const SEPARATOR = "-";
3
- export declare function isUnicodeLanguageSubtag(lang: string): boolean;
4
- export declare function isStructurallyValidLanguageTag(tag: string): boolean;
5
- export declare function isUnicodeRegionSubtag(region: string): boolean;
6
- export declare function isUnicodeScriptSubtag(script: string): boolean;
7
- export declare function isUnicodeVariantSubtag(variant: string): boolean;
8
- export declare function parseUnicodeLanguageId(chunks: string[] | string): UnicodeLanguageId;
9
- export declare function parseUnicodeLocaleId(locale: string): UnicodeLocaleId;
package/src/parser.js DELETED
@@ -1,235 +0,0 @@
1
- import "./types.js";
2
- const ALPHANUM_1_8 = /^[a-z0-9]{1,8}$/i;
3
- const ALPHANUM_2_8 = /^[a-z0-9]{2,8}$/i;
4
- const ALPHANUM_3_8 = /^[a-z0-9]{3,8}$/i;
5
- const KEY_REGEX = /^[a-z0-9][a-z]$/i;
6
- const TYPE_REGEX = /^[a-z0-9]{3,8}$/i;
7
- const ALPHA_4 = /^[a-z]{4}$/i;
8
- // alphanum-[tTuUxX]
9
- const OTHER_EXTENSION_TYPE = /^[0-9a-svwyz]$/i;
10
- const UNICODE_REGION_SUBTAG_REGEX = /^([a-z]{2}|[0-9]{3})$/i;
11
- const UNICODE_VARIANT_SUBTAG_REGEX = /^([a-z0-9]{5,8}|[0-9][a-z0-9]{3})$/i;
12
- const UNICODE_LANGUAGE_SUBTAG_REGEX = /^([a-z]{2,3}|[a-z]{5,8})$/i;
13
- const TKEY_REGEX = /^[a-z][0-9]$/i;
14
- export const SEPARATOR = "-";
15
- export function isUnicodeLanguageSubtag(lang) {
16
- return UNICODE_LANGUAGE_SUBTAG_REGEX.test(lang);
17
- }
18
- export function isStructurallyValidLanguageTag(tag) {
19
- try {
20
- parseUnicodeLanguageId(tag.split(SEPARATOR));
21
- } catch {
22
- return false;
23
- }
24
- return true;
25
- }
26
- export function isUnicodeRegionSubtag(region) {
27
- return UNICODE_REGION_SUBTAG_REGEX.test(region);
28
- }
29
- export function isUnicodeScriptSubtag(script) {
30
- return ALPHA_4.test(script);
31
- }
32
- export function isUnicodeVariantSubtag(variant) {
33
- return UNICODE_VARIANT_SUBTAG_REGEX.test(variant);
34
- }
35
- export function parseUnicodeLanguageId(chunks) {
36
- if (typeof chunks === "string") {
37
- chunks = chunks.split(SEPARATOR);
38
- }
39
- const lang = chunks.shift();
40
- if (!lang) {
41
- throw new RangeError("Missing unicode_language_subtag");
42
- }
43
- if (lang === "root") {
44
- return {
45
- lang: "root",
46
- variants: []
47
- };
48
- }
49
- // unicode_language_subtag
50
- if (!isUnicodeLanguageSubtag(lang)) {
51
- throw new RangeError("Malformed unicode_language_subtag");
52
- }
53
- let script;
54
- // unicode_script_subtag
55
- if (chunks.length && isUnicodeScriptSubtag(chunks[0])) {
56
- script = chunks.shift();
57
- }
58
- let region;
59
- // unicode_region_subtag
60
- if (chunks.length && isUnicodeRegionSubtag(chunks[0])) {
61
- region = chunks.shift();
62
- }
63
- const variants = {};
64
- while (chunks.length && isUnicodeVariantSubtag(chunks[0])) {
65
- const variant = chunks.shift();
66
- if (variant in variants) {
67
- throw new RangeError(`Duplicate variant "${variant}"`);
68
- }
69
- variants[variant] = 1;
70
- }
71
- return {
72
- lang,
73
- script,
74
- region,
75
- variants: Object.keys(variants)
76
- };
77
- }
78
- function parseUnicodeExtension(chunks) {
79
- const keywords = [];
80
- let keyword;
81
- while (chunks.length && (keyword = parseKeyword(chunks))) {
82
- keywords.push(keyword);
83
- }
84
- if (keywords.length) {
85
- return {
86
- type: "u",
87
- keywords,
88
- attributes: []
89
- };
90
- }
91
- // Mix of attributes & keywords
92
- // Check for attributes first
93
- const attributes = [];
94
- while (chunks.length && ALPHANUM_3_8.test(chunks[0])) {
95
- attributes.push(chunks.shift());
96
- }
97
- while (chunks.length && (keyword = parseKeyword(chunks))) {
98
- keywords.push(keyword);
99
- }
100
- if (keywords.length || attributes.length) {
101
- return {
102
- type: "u",
103
- attributes,
104
- keywords
105
- };
106
- }
107
- throw new RangeError("Malformed unicode_extension");
108
- }
109
- function parseKeyword(chunks) {
110
- let key;
111
- if (!KEY_REGEX.test(chunks[0])) {
112
- return;
113
- }
114
- key = chunks.shift();
115
- const type = [];
116
- while (chunks.length && TYPE_REGEX.test(chunks[0])) {
117
- type.push(chunks.shift());
118
- }
119
- let value = "";
120
- if (type.length) {
121
- value = type.join(SEPARATOR);
122
- }
123
- return [key, value];
124
- }
125
- function parseTransformedExtension(chunks) {
126
- let lang;
127
- try {
128
- lang = parseUnicodeLanguageId(chunks);
129
- } catch {}
130
- const fields = [];
131
- while (chunks.length && TKEY_REGEX.test(chunks[0])) {
132
- const key = chunks.shift();
133
- const value = [];
134
- while (chunks.length && ALPHANUM_3_8.test(chunks[0])) {
135
- value.push(chunks.shift());
136
- }
137
- if (!value.length) {
138
- throw new RangeError(`Missing tvalue for tkey "${key}"`);
139
- }
140
- fields.push([key, value.join(SEPARATOR)]);
141
- }
142
- if (fields.length) {
143
- return {
144
- type: "t",
145
- fields,
146
- lang
147
- };
148
- }
149
- throw new RangeError("Malformed transformed_extension");
150
- }
151
- function parsePuExtension(chunks) {
152
- const exts = [];
153
- while (chunks.length && ALPHANUM_1_8.test(chunks[0])) {
154
- exts.push(chunks.shift());
155
- }
156
- if (exts.length) {
157
- return {
158
- type: "x",
159
- value: exts.join(SEPARATOR)
160
- };
161
- }
162
- throw new RangeError("Malformed private_use_extension");
163
- }
164
- function parseOtherExtensionValue(chunks) {
165
- const exts = [];
166
- while (chunks.length && ALPHANUM_2_8.test(chunks[0])) {
167
- exts.push(chunks.shift());
168
- }
169
- if (exts.length) {
170
- return exts.join(SEPARATOR);
171
- }
172
- return "";
173
- }
174
- function parseExtensions(chunks) {
175
- if (!chunks.length) {
176
- return { extensions: [] };
177
- }
178
- const extensions = [];
179
- let unicodeExtension;
180
- let transformedExtension;
181
- let puExtension;
182
- const otherExtensionMap = {};
183
- do {
184
- const type = chunks.shift();
185
- switch (type) {
186
- case "u":
187
- case "U":
188
- if (unicodeExtension) {
189
- throw new RangeError("There can only be 1 -u- extension");
190
- }
191
- unicodeExtension = parseUnicodeExtension(chunks);
192
- extensions.push(unicodeExtension);
193
- break;
194
- case "t":
195
- case "T":
196
- if (transformedExtension) {
197
- throw new RangeError("There can only be 1 -t- extension");
198
- }
199
- transformedExtension = parseTransformedExtension(chunks);
200
- extensions.push(transformedExtension);
201
- break;
202
- case "x":
203
- case "X":
204
- if (puExtension) {
205
- throw new RangeError("There can only be 1 -x- extension");
206
- }
207
- puExtension = parsePuExtension(chunks);
208
- extensions.push(puExtension);
209
- break;
210
- default:
211
- if (!OTHER_EXTENSION_TYPE.test(type)) {
212
- throw new RangeError("Malformed extension type");
213
- }
214
- if (type in otherExtensionMap) {
215
- throw new RangeError(`There can only be 1 -${type}- extension`);
216
- }
217
- const extension = {
218
- type,
219
- value: parseOtherExtensionValue(chunks)
220
- };
221
- otherExtensionMap[extension.type] = extension;
222
- extensions.push(extension);
223
- break;
224
- }
225
- } while (chunks.length);
226
- return { extensions };
227
- }
228
- export function parseUnicodeLocaleId(locale) {
229
- const chunks = locale.split(SEPARATOR);
230
- const lang = parseUnicodeLanguageId(chunks);
231
- return {
232
- lang,
233
- ...parseExtensions(chunks)
234
- };
235
- }
package/src/types.d.ts DELETED
@@ -1,32 +0,0 @@
1
- export interface UnicodeLocaleId {
2
- lang: UnicodeLanguageId;
3
- extensions: Array<UnicodeExtension | TransformedExtension | PuExtension | OtherExtension>;
4
- }
5
- export interface UnicodeLanguageId {
6
- lang: string;
7
- script?: string;
8
- region?: string;
9
- variants: string[];
10
- }
11
- export type KV = [string, string] | [string];
12
- export interface Extension {
13
- type: string;
14
- }
15
- export interface UnicodeExtension extends Extension {
16
- type: "u";
17
- keywords: KV[];
18
- attributes: string[];
19
- }
20
- export interface TransformedExtension extends Extension {
21
- type: "t";
22
- fields: KV[];
23
- lang?: UnicodeLanguageId;
24
- }
25
- export interface PuExtension extends Extension {
26
- type: "x";
27
- value: string;
28
- }
29
- export interface OtherExtension extends Extension {
30
- type: "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "v" | "w" | "y" | "z";
31
- value: string;
32
- }
package/src/types.js DELETED
@@ -1 +0,0 @@
1
- export {};