@tolgee/format-icu 4.9.3-rc.128edeb.0

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.
@@ -0,0 +1,3924 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise */
16
+
17
+ var extendStatics = function(d, b) {
18
+ extendStatics = Object.setPrototypeOf ||
19
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
20
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
21
+ return extendStatics(d, b);
22
+ };
23
+
24
+ function __extends(d, b) {
25
+ if (typeof b !== "function" && b !== null)
26
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
27
+ extendStatics(d, b);
28
+ function __() { this.constructor = d; }
29
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
30
+ }
31
+
32
+ var __assign = function() {
33
+ __assign = Object.assign || function __assign(t) {
34
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
35
+ s = arguments[i];
36
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
37
+ }
38
+ return t;
39
+ };
40
+ return __assign.apply(this, arguments);
41
+ };
42
+
43
+ function __spreadArray(to, from, pack) {
44
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
45
+ if (ar || !(i in from)) {
46
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
47
+ ar[i] = from[i];
48
+ }
49
+ }
50
+ return to.concat(ar || Array.prototype.slice.call(from));
51
+ }
52
+
53
+ var ErrorKind;
54
+ (function (ErrorKind) {
55
+ /** Argument is unclosed (e.g. `{0`) */
56
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_CLOSING_BRACE"] = 1] = "EXPECT_ARGUMENT_CLOSING_BRACE";
57
+ /** Argument is empty (e.g. `{}`). */
58
+ ErrorKind[ErrorKind["EMPTY_ARGUMENT"] = 2] = "EMPTY_ARGUMENT";
59
+ /** Argument is malformed (e.g. `{foo!}``) */
60
+ ErrorKind[ErrorKind["MALFORMED_ARGUMENT"] = 3] = "MALFORMED_ARGUMENT";
61
+ /** Expect an argument type (e.g. `{foo,}`) */
62
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_TYPE"] = 4] = "EXPECT_ARGUMENT_TYPE";
63
+ /** Unsupported argument type (e.g. `{foo,foo}`) */
64
+ ErrorKind[ErrorKind["INVALID_ARGUMENT_TYPE"] = 5] = "INVALID_ARGUMENT_TYPE";
65
+ /** Expect an argument style (e.g. `{foo, number, }`) */
66
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_STYLE"] = 6] = "EXPECT_ARGUMENT_STYLE";
67
+ /** The number skeleton is invalid. */
68
+ ErrorKind[ErrorKind["INVALID_NUMBER_SKELETON"] = 7] = "INVALID_NUMBER_SKELETON";
69
+ /** The date time skeleton is invalid. */
70
+ ErrorKind[ErrorKind["INVALID_DATE_TIME_SKELETON"] = 8] = "INVALID_DATE_TIME_SKELETON";
71
+ /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
72
+ ErrorKind[ErrorKind["EXPECT_NUMBER_SKELETON"] = 9] = "EXPECT_NUMBER_SKELETON";
73
+ /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
74
+ ErrorKind[ErrorKind["EXPECT_DATE_TIME_SKELETON"] = 10] = "EXPECT_DATE_TIME_SKELETON";
75
+ /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
76
+ ErrorKind[ErrorKind["UNCLOSED_QUOTE_IN_ARGUMENT_STYLE"] = 11] = "UNCLOSED_QUOTE_IN_ARGUMENT_STYLE";
77
+ /** Missing select argument options (e.g. `{foo, select}`) */
78
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_OPTIONS"] = 12] = "EXPECT_SELECT_ARGUMENT_OPTIONS";
79
+ /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
80
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE"] = 13] = "EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE";
81
+ /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
82
+ ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_OFFSET_VALUE"] = 14] = "INVALID_PLURAL_ARGUMENT_OFFSET_VALUE";
83
+ /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
84
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR"] = 15] = "EXPECT_SELECT_ARGUMENT_SELECTOR";
85
+ /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
86
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR"] = 16] = "EXPECT_PLURAL_ARGUMENT_SELECTOR";
87
+ /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
88
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT"] = 17] = "EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT";
89
+ /**
90
+ * Expecting a message fragment after the `plural` or `selectordinal` selector
91
+ * (e.g. `{foo, plural, one}`)
92
+ */
93
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT"] = 18] = "EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT";
94
+ /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
95
+ ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_SELECTOR"] = 19] = "INVALID_PLURAL_ARGUMENT_SELECTOR";
96
+ /**
97
+ * Duplicate selectors in `plural` or `selectordinal` argument.
98
+ * (e.g. {foo, plural, one {#} one {#}})
99
+ */
100
+ ErrorKind[ErrorKind["DUPLICATE_PLURAL_ARGUMENT_SELECTOR"] = 20] = "DUPLICATE_PLURAL_ARGUMENT_SELECTOR";
101
+ /** Duplicate selectors in `select` argument.
102
+ * (e.g. {foo, select, apple {apple} apple {apple}})
103
+ */
104
+ ErrorKind[ErrorKind["DUPLICATE_SELECT_ARGUMENT_SELECTOR"] = 21] = "DUPLICATE_SELECT_ARGUMENT_SELECTOR";
105
+ /** Plural or select argument option must have `other` clause. */
106
+ ErrorKind[ErrorKind["MISSING_OTHER_CLAUSE"] = 22] = "MISSING_OTHER_CLAUSE";
107
+ /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
108
+ ErrorKind[ErrorKind["INVALID_TAG"] = 23] = "INVALID_TAG";
109
+ /** The tag name is invalid. (e.g. `<123>foo</123>`) */
110
+ ErrorKind[ErrorKind["INVALID_TAG_NAME"] = 25] = "INVALID_TAG_NAME";
111
+ /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
112
+ ErrorKind[ErrorKind["UNMATCHED_CLOSING_TAG"] = 26] = "UNMATCHED_CLOSING_TAG";
113
+ /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
114
+ ErrorKind[ErrorKind["UNCLOSED_TAG"] = 27] = "UNCLOSED_TAG";
115
+ })(ErrorKind || (ErrorKind = {}));
116
+
117
+ var TYPE;
118
+ (function (TYPE) {
119
+ /**
120
+ * Raw text
121
+ */
122
+ TYPE[TYPE["literal"] = 0] = "literal";
123
+ /**
124
+ * Variable w/o any format, e.g `var` in `this is a {var}`
125
+ */
126
+ TYPE[TYPE["argument"] = 1] = "argument";
127
+ /**
128
+ * Variable w/ number format
129
+ */
130
+ TYPE[TYPE["number"] = 2] = "number";
131
+ /**
132
+ * Variable w/ date format
133
+ */
134
+ TYPE[TYPE["date"] = 3] = "date";
135
+ /**
136
+ * Variable w/ time format
137
+ */
138
+ TYPE[TYPE["time"] = 4] = "time";
139
+ /**
140
+ * Variable w/ select format
141
+ */
142
+ TYPE[TYPE["select"] = 5] = "select";
143
+ /**
144
+ * Variable w/ plural format
145
+ */
146
+ TYPE[TYPE["plural"] = 6] = "plural";
147
+ /**
148
+ * Only possible within plural argument.
149
+ * This is the `#` symbol that will be substituted with the count.
150
+ */
151
+ TYPE[TYPE["pound"] = 7] = "pound";
152
+ /**
153
+ * XML-like tag
154
+ */
155
+ TYPE[TYPE["tag"] = 8] = "tag";
156
+ })(TYPE || (TYPE = {}));
157
+ var SKELETON_TYPE;
158
+ (function (SKELETON_TYPE) {
159
+ SKELETON_TYPE[SKELETON_TYPE["number"] = 0] = "number";
160
+ SKELETON_TYPE[SKELETON_TYPE["dateTime"] = 1] = "dateTime";
161
+ })(SKELETON_TYPE || (SKELETON_TYPE = {}));
162
+ /**
163
+ * Type Guards
164
+ */
165
+ function isLiteralElement(el) {
166
+ return el.type === TYPE.literal;
167
+ }
168
+ function isArgumentElement(el) {
169
+ return el.type === TYPE.argument;
170
+ }
171
+ function isNumberElement(el) {
172
+ return el.type === TYPE.number;
173
+ }
174
+ function isDateElement(el) {
175
+ return el.type === TYPE.date;
176
+ }
177
+ function isTimeElement(el) {
178
+ return el.type === TYPE.time;
179
+ }
180
+ function isSelectElement(el) {
181
+ return el.type === TYPE.select;
182
+ }
183
+ function isPluralElement(el) {
184
+ return el.type === TYPE.plural;
185
+ }
186
+ function isPoundElement(el) {
187
+ return el.type === TYPE.pound;
188
+ }
189
+ function isTagElement(el) {
190
+ return el.type === TYPE.tag;
191
+ }
192
+ function isNumberSkeleton(el) {
193
+ return !!(el && typeof el === 'object' && el.type === SKELETON_TYPE.number);
194
+ }
195
+ function isDateTimeSkeleton(el) {
196
+ return !!(el && typeof el === 'object' && el.type === SKELETON_TYPE.dateTime);
197
+ }
198
+
199
+ // @generated from regex-gen.ts
200
+ var SPACE_SEPARATOR_REGEX = /[ \xA0\u1680\u2000-\u200A\u202F\u205F\u3000]/;
201
+
202
+ /**
203
+ * https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
204
+ * Credit: https://github.com/caridy/intl-datetimeformat-pattern/blob/master/index.js
205
+ * with some tweaks
206
+ */
207
+ var DATE_TIME_REGEX = /(?:[Eec]{1,6}|G{1,5}|[Qq]{1,5}|(?:[yYur]+|U{1,5})|[ML]{1,5}|d{1,2}|D{1,3}|F{1}|[abB]{1,5}|[hkHK]{1,2}|w{1,2}|W{1}|m{1,2}|s{1,2}|[zZOvVxX]{1,4})(?=([^']*'[^']*')*[^']*$)/g;
208
+ /**
209
+ * Parse Date time skeleton into Intl.DateTimeFormatOptions
210
+ * Ref: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
211
+ * @public
212
+ * @param skeleton skeleton string
213
+ */
214
+ function parseDateTimeSkeleton(skeleton) {
215
+ var result = {};
216
+ skeleton.replace(DATE_TIME_REGEX, function (match) {
217
+ var len = match.length;
218
+ switch (match[0]) {
219
+ // Era
220
+ case 'G':
221
+ result.era = len === 4 ? 'long' : len === 5 ? 'narrow' : 'short';
222
+ break;
223
+ // Year
224
+ case 'y':
225
+ result.year = len === 2 ? '2-digit' : 'numeric';
226
+ break;
227
+ case 'Y':
228
+ case 'u':
229
+ case 'U':
230
+ case 'r':
231
+ throw new RangeError('`Y/u/U/r` (year) patterns are not supported, use `y` instead');
232
+ // Quarter
233
+ case 'q':
234
+ case 'Q':
235
+ throw new RangeError('`q/Q` (quarter) patterns are not supported');
236
+ // Month
237
+ case 'M':
238
+ case 'L':
239
+ result.month = ['numeric', '2-digit', 'short', 'long', 'narrow'][len - 1];
240
+ break;
241
+ // Week
242
+ case 'w':
243
+ case 'W':
244
+ throw new RangeError('`w/W` (week) patterns are not supported');
245
+ case 'd':
246
+ result.day = ['numeric', '2-digit'][len - 1];
247
+ break;
248
+ case 'D':
249
+ case 'F':
250
+ case 'g':
251
+ throw new RangeError('`D/F/g` (day) patterns are not supported, use `d` instead');
252
+ // Weekday
253
+ case 'E':
254
+ result.weekday = len === 4 ? 'short' : len === 5 ? 'narrow' : 'short';
255
+ break;
256
+ case 'e':
257
+ if (len < 4) {
258
+ throw new RangeError('`e..eee` (weekday) patterns are not supported');
259
+ }
260
+ result.weekday = ['short', 'long', 'narrow', 'short'][len - 4];
261
+ break;
262
+ case 'c':
263
+ if (len < 4) {
264
+ throw new RangeError('`c..ccc` (weekday) patterns are not supported');
265
+ }
266
+ result.weekday = ['short', 'long', 'narrow', 'short'][len - 4];
267
+ break;
268
+ // Period
269
+ case 'a': // AM, PM
270
+ result.hour12 = true;
271
+ break;
272
+ case 'b': // am, pm, noon, midnight
273
+ case 'B': // flexible day periods
274
+ throw new RangeError('`b/B` (period) patterns are not supported, use `a` instead');
275
+ // Hour
276
+ case 'h':
277
+ result.hourCycle = 'h12';
278
+ result.hour = ['numeric', '2-digit'][len - 1];
279
+ break;
280
+ case 'H':
281
+ result.hourCycle = 'h23';
282
+ result.hour = ['numeric', '2-digit'][len - 1];
283
+ break;
284
+ case 'K':
285
+ result.hourCycle = 'h11';
286
+ result.hour = ['numeric', '2-digit'][len - 1];
287
+ break;
288
+ case 'k':
289
+ result.hourCycle = 'h24';
290
+ result.hour = ['numeric', '2-digit'][len - 1];
291
+ break;
292
+ case 'j':
293
+ case 'J':
294
+ case 'C':
295
+ throw new RangeError('`j/J/C` (hour) patterns are not supported, use `h/H/K/k` instead');
296
+ // Minute
297
+ case 'm':
298
+ result.minute = ['numeric', '2-digit'][len - 1];
299
+ break;
300
+ // Second
301
+ case 's':
302
+ result.second = ['numeric', '2-digit'][len - 1];
303
+ break;
304
+ case 'S':
305
+ case 'A':
306
+ throw new RangeError('`S/A` (second) patterns are not supported, use `s` instead');
307
+ // Zone
308
+ case 'z': // 1..3, 4: specific non-location format
309
+ result.timeZoneName = len < 4 ? 'short' : 'long';
310
+ break;
311
+ case 'Z': // 1..3, 4, 5: The ISO8601 varios formats
312
+ case 'O': // 1, 4: miliseconds in day short, long
313
+ case 'v': // 1, 4: generic non-location format
314
+ case 'V': // 1, 2, 3, 4: time zone ID or city
315
+ case 'X': // 1, 2, 3, 4: The ISO8601 varios formats
316
+ case 'x': // 1, 2, 3, 4: The ISO8601 varios formats
317
+ throw new RangeError('`Z/O/v/V/X/x` (timeZone) patterns are not supported, use `z` instead');
318
+ }
319
+ return '';
320
+ });
321
+ return result;
322
+ }
323
+
324
+ // @generated from regex-gen.ts
325
+ var WHITE_SPACE_REGEX = /[\t-\r \x85\u200E\u200F\u2028\u2029]/i;
326
+
327
+ function parseNumberSkeletonFromString(skeleton) {
328
+ if (skeleton.length === 0) {
329
+ throw new Error('Number skeleton cannot be empty');
330
+ }
331
+ // Parse the skeleton
332
+ var stringTokens = skeleton
333
+ .split(WHITE_SPACE_REGEX)
334
+ .filter(function (x) { return x.length > 0; });
335
+ var tokens = [];
336
+ for (var _i = 0, stringTokens_1 = stringTokens; _i < stringTokens_1.length; _i++) {
337
+ var stringToken = stringTokens_1[_i];
338
+ var stemAndOptions = stringToken.split('/');
339
+ if (stemAndOptions.length === 0) {
340
+ throw new Error('Invalid number skeleton');
341
+ }
342
+ var stem = stemAndOptions[0], options = stemAndOptions.slice(1);
343
+ for (var _a = 0, options_1 = options; _a < options_1.length; _a++) {
344
+ var option = options_1[_a];
345
+ if (option.length === 0) {
346
+ throw new Error('Invalid number skeleton');
347
+ }
348
+ }
349
+ tokens.push({ stem: stem, options: options });
350
+ }
351
+ return tokens;
352
+ }
353
+ function icuUnitToEcma(unit) {
354
+ return unit.replace(/^(.*?)-/, '');
355
+ }
356
+ var FRACTION_PRECISION_REGEX = /^\.(?:(0+)(\*)?|(#+)|(0+)(#+))$/g;
357
+ var SIGNIFICANT_PRECISION_REGEX = /^(@+)?(\+|#+)?[rs]?$/g;
358
+ var INTEGER_WIDTH_REGEX = /(\*)(0+)|(#+)(0+)|(0+)/g;
359
+ var CONCISE_INTEGER_WIDTH_REGEX = /^(0+)$/;
360
+ function parseSignificantPrecision(str) {
361
+ var result = {};
362
+ if (str[str.length - 1] === 'r') {
363
+ result.roundingPriority = 'morePrecision';
364
+ }
365
+ else if (str[str.length - 1] === 's') {
366
+ result.roundingPriority = 'lessPrecision';
367
+ }
368
+ str.replace(SIGNIFICANT_PRECISION_REGEX, function (_, g1, g2) {
369
+ // @@@ case
370
+ if (typeof g2 !== 'string') {
371
+ result.minimumSignificantDigits = g1.length;
372
+ result.maximumSignificantDigits = g1.length;
373
+ }
374
+ // @@@+ case
375
+ else if (g2 === '+') {
376
+ result.minimumSignificantDigits = g1.length;
377
+ }
378
+ // .### case
379
+ else if (g1[0] === '#') {
380
+ result.maximumSignificantDigits = g1.length;
381
+ }
382
+ // .@@## or .@@@ case
383
+ else {
384
+ result.minimumSignificantDigits = g1.length;
385
+ result.maximumSignificantDigits =
386
+ g1.length + (typeof g2 === 'string' ? g2.length : 0);
387
+ }
388
+ return '';
389
+ });
390
+ return result;
391
+ }
392
+ function parseSign(str) {
393
+ switch (str) {
394
+ case 'sign-auto':
395
+ return {
396
+ signDisplay: 'auto',
397
+ };
398
+ case 'sign-accounting':
399
+ case '()':
400
+ return {
401
+ currencySign: 'accounting',
402
+ };
403
+ case 'sign-always':
404
+ case '+!':
405
+ return {
406
+ signDisplay: 'always',
407
+ };
408
+ case 'sign-accounting-always':
409
+ case '()!':
410
+ return {
411
+ signDisplay: 'always',
412
+ currencySign: 'accounting',
413
+ };
414
+ case 'sign-except-zero':
415
+ case '+?':
416
+ return {
417
+ signDisplay: 'exceptZero',
418
+ };
419
+ case 'sign-accounting-except-zero':
420
+ case '()?':
421
+ return {
422
+ signDisplay: 'exceptZero',
423
+ currencySign: 'accounting',
424
+ };
425
+ case 'sign-never':
426
+ case '+_':
427
+ return {
428
+ signDisplay: 'never',
429
+ };
430
+ }
431
+ }
432
+ function parseConciseScientificAndEngineeringStem(stem) {
433
+ // Engineering
434
+ var result;
435
+ if (stem[0] === 'E' && stem[1] === 'E') {
436
+ result = {
437
+ notation: 'engineering',
438
+ };
439
+ stem = stem.slice(2);
440
+ }
441
+ else if (stem[0] === 'E') {
442
+ result = {
443
+ notation: 'scientific',
444
+ };
445
+ stem = stem.slice(1);
446
+ }
447
+ if (result) {
448
+ var signDisplay = stem.slice(0, 2);
449
+ if (signDisplay === '+!') {
450
+ result.signDisplay = 'always';
451
+ stem = stem.slice(2);
452
+ }
453
+ else if (signDisplay === '+?') {
454
+ result.signDisplay = 'exceptZero';
455
+ stem = stem.slice(2);
456
+ }
457
+ if (!CONCISE_INTEGER_WIDTH_REGEX.test(stem)) {
458
+ throw new Error('Malformed concise eng/scientific notation');
459
+ }
460
+ result.minimumIntegerDigits = stem.length;
461
+ }
462
+ return result;
463
+ }
464
+ function parseNotationOptions(opt) {
465
+ var result = {};
466
+ var signOpts = parseSign(opt);
467
+ if (signOpts) {
468
+ return signOpts;
469
+ }
470
+ return result;
471
+ }
472
+ /**
473
+ * https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options
474
+ */
475
+ function parseNumberSkeleton(tokens) {
476
+ var result = {};
477
+ for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
478
+ var token = tokens_1[_i];
479
+ switch (token.stem) {
480
+ case 'percent':
481
+ case '%':
482
+ result.style = 'percent';
483
+ continue;
484
+ case '%x100':
485
+ result.style = 'percent';
486
+ result.scale = 100;
487
+ continue;
488
+ case 'currency':
489
+ result.style = 'currency';
490
+ result.currency = token.options[0];
491
+ continue;
492
+ case 'group-off':
493
+ case ',_':
494
+ result.useGrouping = false;
495
+ continue;
496
+ case 'precision-integer':
497
+ case '.':
498
+ result.maximumFractionDigits = 0;
499
+ continue;
500
+ case 'measure-unit':
501
+ case 'unit':
502
+ result.style = 'unit';
503
+ result.unit = icuUnitToEcma(token.options[0]);
504
+ continue;
505
+ case 'compact-short':
506
+ case 'K':
507
+ result.notation = 'compact';
508
+ result.compactDisplay = 'short';
509
+ continue;
510
+ case 'compact-long':
511
+ case 'KK':
512
+ result.notation = 'compact';
513
+ result.compactDisplay = 'long';
514
+ continue;
515
+ case 'scientific':
516
+ result = __assign(__assign(__assign({}, result), { notation: 'scientific' }), token.options.reduce(function (all, opt) { return (__assign(__assign({}, all), parseNotationOptions(opt))); }, {}));
517
+ continue;
518
+ case 'engineering':
519
+ result = __assign(__assign(__assign({}, result), { notation: 'engineering' }), token.options.reduce(function (all, opt) { return (__assign(__assign({}, all), parseNotationOptions(opt))); }, {}));
520
+ continue;
521
+ case 'notation-simple':
522
+ result.notation = 'standard';
523
+ continue;
524
+ // https://github.com/unicode-org/icu/blob/master/icu4c/source/i18n/unicode/unumberformatter.h
525
+ case 'unit-width-narrow':
526
+ result.currencyDisplay = 'narrowSymbol';
527
+ result.unitDisplay = 'narrow';
528
+ continue;
529
+ case 'unit-width-short':
530
+ result.currencyDisplay = 'code';
531
+ result.unitDisplay = 'short';
532
+ continue;
533
+ case 'unit-width-full-name':
534
+ result.currencyDisplay = 'name';
535
+ result.unitDisplay = 'long';
536
+ continue;
537
+ case 'unit-width-iso-code':
538
+ result.currencyDisplay = 'symbol';
539
+ continue;
540
+ case 'scale':
541
+ result.scale = parseFloat(token.options[0]);
542
+ continue;
543
+ // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width
544
+ case 'integer-width':
545
+ if (token.options.length > 1) {
546
+ throw new RangeError('integer-width stems only accept a single optional option');
547
+ }
548
+ token.options[0].replace(INTEGER_WIDTH_REGEX, function (_, g1, g2, g3, g4, g5) {
549
+ if (g1) {
550
+ result.minimumIntegerDigits = g2.length;
551
+ }
552
+ else if (g3 && g4) {
553
+ throw new Error('We currently do not support maximum integer digits');
554
+ }
555
+ else if (g5) {
556
+ throw new Error('We currently do not support exact integer digits');
557
+ }
558
+ return '';
559
+ });
560
+ continue;
561
+ }
562
+ // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width
563
+ if (CONCISE_INTEGER_WIDTH_REGEX.test(token.stem)) {
564
+ result.minimumIntegerDigits = token.stem.length;
565
+ continue;
566
+ }
567
+ if (FRACTION_PRECISION_REGEX.test(token.stem)) {
568
+ // Precision
569
+ // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#fraction-precision
570
+ // precision-integer case
571
+ if (token.options.length > 1) {
572
+ throw new RangeError('Fraction-precision stems only accept a single optional option');
573
+ }
574
+ token.stem.replace(FRACTION_PRECISION_REGEX, function (_, g1, g2, g3, g4, g5) {
575
+ // .000* case (before ICU67 it was .000+)
576
+ if (g2 === '*') {
577
+ result.minimumFractionDigits = g1.length;
578
+ }
579
+ // .### case
580
+ else if (g3 && g3[0] === '#') {
581
+ result.maximumFractionDigits = g3.length;
582
+ }
583
+ // .00## case
584
+ else if (g4 && g5) {
585
+ result.minimumFractionDigits = g4.length;
586
+ result.maximumFractionDigits = g4.length + g5.length;
587
+ }
588
+ else {
589
+ result.minimumFractionDigits = g1.length;
590
+ result.maximumFractionDigits = g1.length;
591
+ }
592
+ return '';
593
+ });
594
+ var opt = token.options[0];
595
+ // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#trailing-zero-display
596
+ if (opt === 'w') {
597
+ result = __assign(__assign({}, result), { trailingZeroDisplay: 'stripIfInteger' });
598
+ }
599
+ else if (opt) {
600
+ result = __assign(__assign({}, result), parseSignificantPrecision(opt));
601
+ }
602
+ continue;
603
+ }
604
+ // https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#significant-digits-precision
605
+ if (SIGNIFICANT_PRECISION_REGEX.test(token.stem)) {
606
+ result = __assign(__assign({}, result), parseSignificantPrecision(token.stem));
607
+ continue;
608
+ }
609
+ var signOpts = parseSign(token.stem);
610
+ if (signOpts) {
611
+ result = __assign(__assign({}, result), signOpts);
612
+ }
613
+ var conciseScientificAndEngineeringOpts = parseConciseScientificAndEngineeringStem(token.stem);
614
+ if (conciseScientificAndEngineeringOpts) {
615
+ result = __assign(__assign({}, result), conciseScientificAndEngineeringOpts);
616
+ }
617
+ }
618
+ return result;
619
+ }
620
+
621
+ // @generated from time-data-gen.ts
622
+ // prettier-ignore
623
+ var timeData = {
624
+ "AX": [
625
+ "H"
626
+ ],
627
+ "BQ": [
628
+ "H"
629
+ ],
630
+ "CP": [
631
+ "H"
632
+ ],
633
+ "CZ": [
634
+ "H"
635
+ ],
636
+ "DK": [
637
+ "H"
638
+ ],
639
+ "FI": [
640
+ "H"
641
+ ],
642
+ "ID": [
643
+ "H"
644
+ ],
645
+ "IS": [
646
+ "H"
647
+ ],
648
+ "ML": [
649
+ "H"
650
+ ],
651
+ "NE": [
652
+ "H"
653
+ ],
654
+ "RU": [
655
+ "H"
656
+ ],
657
+ "SE": [
658
+ "H"
659
+ ],
660
+ "SJ": [
661
+ "H"
662
+ ],
663
+ "SK": [
664
+ "H"
665
+ ],
666
+ "AS": [
667
+ "h",
668
+ "H"
669
+ ],
670
+ "BT": [
671
+ "h",
672
+ "H"
673
+ ],
674
+ "DJ": [
675
+ "h",
676
+ "H"
677
+ ],
678
+ "ER": [
679
+ "h",
680
+ "H"
681
+ ],
682
+ "GH": [
683
+ "h",
684
+ "H"
685
+ ],
686
+ "IN": [
687
+ "h",
688
+ "H"
689
+ ],
690
+ "LS": [
691
+ "h",
692
+ "H"
693
+ ],
694
+ "PG": [
695
+ "h",
696
+ "H"
697
+ ],
698
+ "PW": [
699
+ "h",
700
+ "H"
701
+ ],
702
+ "SO": [
703
+ "h",
704
+ "H"
705
+ ],
706
+ "TO": [
707
+ "h",
708
+ "H"
709
+ ],
710
+ "VU": [
711
+ "h",
712
+ "H"
713
+ ],
714
+ "WS": [
715
+ "h",
716
+ "H"
717
+ ],
718
+ "001": [
719
+ "H",
720
+ "h"
721
+ ],
722
+ "AL": [
723
+ "h",
724
+ "H",
725
+ "hB"
726
+ ],
727
+ "TD": [
728
+ "h",
729
+ "H",
730
+ "hB"
731
+ ],
732
+ "ca-ES": [
733
+ "H",
734
+ "h",
735
+ "hB"
736
+ ],
737
+ "CF": [
738
+ "H",
739
+ "h",
740
+ "hB"
741
+ ],
742
+ "CM": [
743
+ "H",
744
+ "h",
745
+ "hB"
746
+ ],
747
+ "fr-CA": [
748
+ "H",
749
+ "h",
750
+ "hB"
751
+ ],
752
+ "gl-ES": [
753
+ "H",
754
+ "h",
755
+ "hB"
756
+ ],
757
+ "it-CH": [
758
+ "H",
759
+ "h",
760
+ "hB"
761
+ ],
762
+ "it-IT": [
763
+ "H",
764
+ "h",
765
+ "hB"
766
+ ],
767
+ "LU": [
768
+ "H",
769
+ "h",
770
+ "hB"
771
+ ],
772
+ "NP": [
773
+ "H",
774
+ "h",
775
+ "hB"
776
+ ],
777
+ "PF": [
778
+ "H",
779
+ "h",
780
+ "hB"
781
+ ],
782
+ "SC": [
783
+ "H",
784
+ "h",
785
+ "hB"
786
+ ],
787
+ "SM": [
788
+ "H",
789
+ "h",
790
+ "hB"
791
+ ],
792
+ "SN": [
793
+ "H",
794
+ "h",
795
+ "hB"
796
+ ],
797
+ "TF": [
798
+ "H",
799
+ "h",
800
+ "hB"
801
+ ],
802
+ "VA": [
803
+ "H",
804
+ "h",
805
+ "hB"
806
+ ],
807
+ "CY": [
808
+ "h",
809
+ "H",
810
+ "hb",
811
+ "hB"
812
+ ],
813
+ "GR": [
814
+ "h",
815
+ "H",
816
+ "hb",
817
+ "hB"
818
+ ],
819
+ "CO": [
820
+ "h",
821
+ "H",
822
+ "hB",
823
+ "hb"
824
+ ],
825
+ "DO": [
826
+ "h",
827
+ "H",
828
+ "hB",
829
+ "hb"
830
+ ],
831
+ "KP": [
832
+ "h",
833
+ "H",
834
+ "hB",
835
+ "hb"
836
+ ],
837
+ "KR": [
838
+ "h",
839
+ "H",
840
+ "hB",
841
+ "hb"
842
+ ],
843
+ "NA": [
844
+ "h",
845
+ "H",
846
+ "hB",
847
+ "hb"
848
+ ],
849
+ "PA": [
850
+ "h",
851
+ "H",
852
+ "hB",
853
+ "hb"
854
+ ],
855
+ "PR": [
856
+ "h",
857
+ "H",
858
+ "hB",
859
+ "hb"
860
+ ],
861
+ "VE": [
862
+ "h",
863
+ "H",
864
+ "hB",
865
+ "hb"
866
+ ],
867
+ "AC": [
868
+ "H",
869
+ "h",
870
+ "hb",
871
+ "hB"
872
+ ],
873
+ "AI": [
874
+ "H",
875
+ "h",
876
+ "hb",
877
+ "hB"
878
+ ],
879
+ "BW": [
880
+ "H",
881
+ "h",
882
+ "hb",
883
+ "hB"
884
+ ],
885
+ "BZ": [
886
+ "H",
887
+ "h",
888
+ "hb",
889
+ "hB"
890
+ ],
891
+ "CC": [
892
+ "H",
893
+ "h",
894
+ "hb",
895
+ "hB"
896
+ ],
897
+ "CK": [
898
+ "H",
899
+ "h",
900
+ "hb",
901
+ "hB"
902
+ ],
903
+ "CX": [
904
+ "H",
905
+ "h",
906
+ "hb",
907
+ "hB"
908
+ ],
909
+ "DG": [
910
+ "H",
911
+ "h",
912
+ "hb",
913
+ "hB"
914
+ ],
915
+ "FK": [
916
+ "H",
917
+ "h",
918
+ "hb",
919
+ "hB"
920
+ ],
921
+ "GB": [
922
+ "H",
923
+ "h",
924
+ "hb",
925
+ "hB"
926
+ ],
927
+ "GG": [
928
+ "H",
929
+ "h",
930
+ "hb",
931
+ "hB"
932
+ ],
933
+ "GI": [
934
+ "H",
935
+ "h",
936
+ "hb",
937
+ "hB"
938
+ ],
939
+ "IE": [
940
+ "H",
941
+ "h",
942
+ "hb",
943
+ "hB"
944
+ ],
945
+ "IM": [
946
+ "H",
947
+ "h",
948
+ "hb",
949
+ "hB"
950
+ ],
951
+ "IO": [
952
+ "H",
953
+ "h",
954
+ "hb",
955
+ "hB"
956
+ ],
957
+ "JE": [
958
+ "H",
959
+ "h",
960
+ "hb",
961
+ "hB"
962
+ ],
963
+ "LT": [
964
+ "H",
965
+ "h",
966
+ "hb",
967
+ "hB"
968
+ ],
969
+ "MK": [
970
+ "H",
971
+ "h",
972
+ "hb",
973
+ "hB"
974
+ ],
975
+ "MN": [
976
+ "H",
977
+ "h",
978
+ "hb",
979
+ "hB"
980
+ ],
981
+ "MS": [
982
+ "H",
983
+ "h",
984
+ "hb",
985
+ "hB"
986
+ ],
987
+ "NF": [
988
+ "H",
989
+ "h",
990
+ "hb",
991
+ "hB"
992
+ ],
993
+ "NG": [
994
+ "H",
995
+ "h",
996
+ "hb",
997
+ "hB"
998
+ ],
999
+ "NR": [
1000
+ "H",
1001
+ "h",
1002
+ "hb",
1003
+ "hB"
1004
+ ],
1005
+ "NU": [
1006
+ "H",
1007
+ "h",
1008
+ "hb",
1009
+ "hB"
1010
+ ],
1011
+ "PN": [
1012
+ "H",
1013
+ "h",
1014
+ "hb",
1015
+ "hB"
1016
+ ],
1017
+ "SH": [
1018
+ "H",
1019
+ "h",
1020
+ "hb",
1021
+ "hB"
1022
+ ],
1023
+ "SX": [
1024
+ "H",
1025
+ "h",
1026
+ "hb",
1027
+ "hB"
1028
+ ],
1029
+ "TA": [
1030
+ "H",
1031
+ "h",
1032
+ "hb",
1033
+ "hB"
1034
+ ],
1035
+ "ZA": [
1036
+ "H",
1037
+ "h",
1038
+ "hb",
1039
+ "hB"
1040
+ ],
1041
+ "af-ZA": [
1042
+ "H",
1043
+ "h",
1044
+ "hB",
1045
+ "hb"
1046
+ ],
1047
+ "AR": [
1048
+ "H",
1049
+ "h",
1050
+ "hB",
1051
+ "hb"
1052
+ ],
1053
+ "CL": [
1054
+ "H",
1055
+ "h",
1056
+ "hB",
1057
+ "hb"
1058
+ ],
1059
+ "CR": [
1060
+ "H",
1061
+ "h",
1062
+ "hB",
1063
+ "hb"
1064
+ ],
1065
+ "CU": [
1066
+ "H",
1067
+ "h",
1068
+ "hB",
1069
+ "hb"
1070
+ ],
1071
+ "EA": [
1072
+ "H",
1073
+ "h",
1074
+ "hB",
1075
+ "hb"
1076
+ ],
1077
+ "es-BO": [
1078
+ "H",
1079
+ "h",
1080
+ "hB",
1081
+ "hb"
1082
+ ],
1083
+ "es-BR": [
1084
+ "H",
1085
+ "h",
1086
+ "hB",
1087
+ "hb"
1088
+ ],
1089
+ "es-EC": [
1090
+ "H",
1091
+ "h",
1092
+ "hB",
1093
+ "hb"
1094
+ ],
1095
+ "es-ES": [
1096
+ "H",
1097
+ "h",
1098
+ "hB",
1099
+ "hb"
1100
+ ],
1101
+ "es-GQ": [
1102
+ "H",
1103
+ "h",
1104
+ "hB",
1105
+ "hb"
1106
+ ],
1107
+ "es-PE": [
1108
+ "H",
1109
+ "h",
1110
+ "hB",
1111
+ "hb"
1112
+ ],
1113
+ "GT": [
1114
+ "H",
1115
+ "h",
1116
+ "hB",
1117
+ "hb"
1118
+ ],
1119
+ "HN": [
1120
+ "H",
1121
+ "h",
1122
+ "hB",
1123
+ "hb"
1124
+ ],
1125
+ "IC": [
1126
+ "H",
1127
+ "h",
1128
+ "hB",
1129
+ "hb"
1130
+ ],
1131
+ "KG": [
1132
+ "H",
1133
+ "h",
1134
+ "hB",
1135
+ "hb"
1136
+ ],
1137
+ "KM": [
1138
+ "H",
1139
+ "h",
1140
+ "hB",
1141
+ "hb"
1142
+ ],
1143
+ "LK": [
1144
+ "H",
1145
+ "h",
1146
+ "hB",
1147
+ "hb"
1148
+ ],
1149
+ "MA": [
1150
+ "H",
1151
+ "h",
1152
+ "hB",
1153
+ "hb"
1154
+ ],
1155
+ "MX": [
1156
+ "H",
1157
+ "h",
1158
+ "hB",
1159
+ "hb"
1160
+ ],
1161
+ "NI": [
1162
+ "H",
1163
+ "h",
1164
+ "hB",
1165
+ "hb"
1166
+ ],
1167
+ "PY": [
1168
+ "H",
1169
+ "h",
1170
+ "hB",
1171
+ "hb"
1172
+ ],
1173
+ "SV": [
1174
+ "H",
1175
+ "h",
1176
+ "hB",
1177
+ "hb"
1178
+ ],
1179
+ "UY": [
1180
+ "H",
1181
+ "h",
1182
+ "hB",
1183
+ "hb"
1184
+ ],
1185
+ "JP": [
1186
+ "H",
1187
+ "h",
1188
+ "K"
1189
+ ],
1190
+ "AD": [
1191
+ "H",
1192
+ "hB"
1193
+ ],
1194
+ "AM": [
1195
+ "H",
1196
+ "hB"
1197
+ ],
1198
+ "AO": [
1199
+ "H",
1200
+ "hB"
1201
+ ],
1202
+ "AT": [
1203
+ "H",
1204
+ "hB"
1205
+ ],
1206
+ "AW": [
1207
+ "H",
1208
+ "hB"
1209
+ ],
1210
+ "BE": [
1211
+ "H",
1212
+ "hB"
1213
+ ],
1214
+ "BF": [
1215
+ "H",
1216
+ "hB"
1217
+ ],
1218
+ "BJ": [
1219
+ "H",
1220
+ "hB"
1221
+ ],
1222
+ "BL": [
1223
+ "H",
1224
+ "hB"
1225
+ ],
1226
+ "BR": [
1227
+ "H",
1228
+ "hB"
1229
+ ],
1230
+ "CG": [
1231
+ "H",
1232
+ "hB"
1233
+ ],
1234
+ "CI": [
1235
+ "H",
1236
+ "hB"
1237
+ ],
1238
+ "CV": [
1239
+ "H",
1240
+ "hB"
1241
+ ],
1242
+ "DE": [
1243
+ "H",
1244
+ "hB"
1245
+ ],
1246
+ "EE": [
1247
+ "H",
1248
+ "hB"
1249
+ ],
1250
+ "FR": [
1251
+ "H",
1252
+ "hB"
1253
+ ],
1254
+ "GA": [
1255
+ "H",
1256
+ "hB"
1257
+ ],
1258
+ "GF": [
1259
+ "H",
1260
+ "hB"
1261
+ ],
1262
+ "GN": [
1263
+ "H",
1264
+ "hB"
1265
+ ],
1266
+ "GP": [
1267
+ "H",
1268
+ "hB"
1269
+ ],
1270
+ "GW": [
1271
+ "H",
1272
+ "hB"
1273
+ ],
1274
+ "HR": [
1275
+ "H",
1276
+ "hB"
1277
+ ],
1278
+ "IL": [
1279
+ "H",
1280
+ "hB"
1281
+ ],
1282
+ "IT": [
1283
+ "H",
1284
+ "hB"
1285
+ ],
1286
+ "KZ": [
1287
+ "H",
1288
+ "hB"
1289
+ ],
1290
+ "MC": [
1291
+ "H",
1292
+ "hB"
1293
+ ],
1294
+ "MD": [
1295
+ "H",
1296
+ "hB"
1297
+ ],
1298
+ "MF": [
1299
+ "H",
1300
+ "hB"
1301
+ ],
1302
+ "MQ": [
1303
+ "H",
1304
+ "hB"
1305
+ ],
1306
+ "MZ": [
1307
+ "H",
1308
+ "hB"
1309
+ ],
1310
+ "NC": [
1311
+ "H",
1312
+ "hB"
1313
+ ],
1314
+ "NL": [
1315
+ "H",
1316
+ "hB"
1317
+ ],
1318
+ "PM": [
1319
+ "H",
1320
+ "hB"
1321
+ ],
1322
+ "PT": [
1323
+ "H",
1324
+ "hB"
1325
+ ],
1326
+ "RE": [
1327
+ "H",
1328
+ "hB"
1329
+ ],
1330
+ "RO": [
1331
+ "H",
1332
+ "hB"
1333
+ ],
1334
+ "SI": [
1335
+ "H",
1336
+ "hB"
1337
+ ],
1338
+ "SR": [
1339
+ "H",
1340
+ "hB"
1341
+ ],
1342
+ "ST": [
1343
+ "H",
1344
+ "hB"
1345
+ ],
1346
+ "TG": [
1347
+ "H",
1348
+ "hB"
1349
+ ],
1350
+ "TR": [
1351
+ "H",
1352
+ "hB"
1353
+ ],
1354
+ "WF": [
1355
+ "H",
1356
+ "hB"
1357
+ ],
1358
+ "YT": [
1359
+ "H",
1360
+ "hB"
1361
+ ],
1362
+ "BD": [
1363
+ "h",
1364
+ "hB",
1365
+ "H"
1366
+ ],
1367
+ "PK": [
1368
+ "h",
1369
+ "hB",
1370
+ "H"
1371
+ ],
1372
+ "AZ": [
1373
+ "H",
1374
+ "hB",
1375
+ "h"
1376
+ ],
1377
+ "BA": [
1378
+ "H",
1379
+ "hB",
1380
+ "h"
1381
+ ],
1382
+ "BG": [
1383
+ "H",
1384
+ "hB",
1385
+ "h"
1386
+ ],
1387
+ "CH": [
1388
+ "H",
1389
+ "hB",
1390
+ "h"
1391
+ ],
1392
+ "GE": [
1393
+ "H",
1394
+ "hB",
1395
+ "h"
1396
+ ],
1397
+ "LI": [
1398
+ "H",
1399
+ "hB",
1400
+ "h"
1401
+ ],
1402
+ "ME": [
1403
+ "H",
1404
+ "hB",
1405
+ "h"
1406
+ ],
1407
+ "RS": [
1408
+ "H",
1409
+ "hB",
1410
+ "h"
1411
+ ],
1412
+ "UA": [
1413
+ "H",
1414
+ "hB",
1415
+ "h"
1416
+ ],
1417
+ "UZ": [
1418
+ "H",
1419
+ "hB",
1420
+ "h"
1421
+ ],
1422
+ "XK": [
1423
+ "H",
1424
+ "hB",
1425
+ "h"
1426
+ ],
1427
+ "AG": [
1428
+ "h",
1429
+ "hb",
1430
+ "H",
1431
+ "hB"
1432
+ ],
1433
+ "AU": [
1434
+ "h",
1435
+ "hb",
1436
+ "H",
1437
+ "hB"
1438
+ ],
1439
+ "BB": [
1440
+ "h",
1441
+ "hb",
1442
+ "H",
1443
+ "hB"
1444
+ ],
1445
+ "BM": [
1446
+ "h",
1447
+ "hb",
1448
+ "H",
1449
+ "hB"
1450
+ ],
1451
+ "BS": [
1452
+ "h",
1453
+ "hb",
1454
+ "H",
1455
+ "hB"
1456
+ ],
1457
+ "CA": [
1458
+ "h",
1459
+ "hb",
1460
+ "H",
1461
+ "hB"
1462
+ ],
1463
+ "DM": [
1464
+ "h",
1465
+ "hb",
1466
+ "H",
1467
+ "hB"
1468
+ ],
1469
+ "en-001": [
1470
+ "h",
1471
+ "hb",
1472
+ "H",
1473
+ "hB"
1474
+ ],
1475
+ "FJ": [
1476
+ "h",
1477
+ "hb",
1478
+ "H",
1479
+ "hB"
1480
+ ],
1481
+ "FM": [
1482
+ "h",
1483
+ "hb",
1484
+ "H",
1485
+ "hB"
1486
+ ],
1487
+ "GD": [
1488
+ "h",
1489
+ "hb",
1490
+ "H",
1491
+ "hB"
1492
+ ],
1493
+ "GM": [
1494
+ "h",
1495
+ "hb",
1496
+ "H",
1497
+ "hB"
1498
+ ],
1499
+ "GU": [
1500
+ "h",
1501
+ "hb",
1502
+ "H",
1503
+ "hB"
1504
+ ],
1505
+ "GY": [
1506
+ "h",
1507
+ "hb",
1508
+ "H",
1509
+ "hB"
1510
+ ],
1511
+ "JM": [
1512
+ "h",
1513
+ "hb",
1514
+ "H",
1515
+ "hB"
1516
+ ],
1517
+ "KI": [
1518
+ "h",
1519
+ "hb",
1520
+ "H",
1521
+ "hB"
1522
+ ],
1523
+ "KN": [
1524
+ "h",
1525
+ "hb",
1526
+ "H",
1527
+ "hB"
1528
+ ],
1529
+ "KY": [
1530
+ "h",
1531
+ "hb",
1532
+ "H",
1533
+ "hB"
1534
+ ],
1535
+ "LC": [
1536
+ "h",
1537
+ "hb",
1538
+ "H",
1539
+ "hB"
1540
+ ],
1541
+ "LR": [
1542
+ "h",
1543
+ "hb",
1544
+ "H",
1545
+ "hB"
1546
+ ],
1547
+ "MH": [
1548
+ "h",
1549
+ "hb",
1550
+ "H",
1551
+ "hB"
1552
+ ],
1553
+ "MP": [
1554
+ "h",
1555
+ "hb",
1556
+ "H",
1557
+ "hB"
1558
+ ],
1559
+ "MW": [
1560
+ "h",
1561
+ "hb",
1562
+ "H",
1563
+ "hB"
1564
+ ],
1565
+ "NZ": [
1566
+ "h",
1567
+ "hb",
1568
+ "H",
1569
+ "hB"
1570
+ ],
1571
+ "SB": [
1572
+ "h",
1573
+ "hb",
1574
+ "H",
1575
+ "hB"
1576
+ ],
1577
+ "SG": [
1578
+ "h",
1579
+ "hb",
1580
+ "H",
1581
+ "hB"
1582
+ ],
1583
+ "SL": [
1584
+ "h",
1585
+ "hb",
1586
+ "H",
1587
+ "hB"
1588
+ ],
1589
+ "SS": [
1590
+ "h",
1591
+ "hb",
1592
+ "H",
1593
+ "hB"
1594
+ ],
1595
+ "SZ": [
1596
+ "h",
1597
+ "hb",
1598
+ "H",
1599
+ "hB"
1600
+ ],
1601
+ "TC": [
1602
+ "h",
1603
+ "hb",
1604
+ "H",
1605
+ "hB"
1606
+ ],
1607
+ "TT": [
1608
+ "h",
1609
+ "hb",
1610
+ "H",
1611
+ "hB"
1612
+ ],
1613
+ "UM": [
1614
+ "h",
1615
+ "hb",
1616
+ "H",
1617
+ "hB"
1618
+ ],
1619
+ "US": [
1620
+ "h",
1621
+ "hb",
1622
+ "H",
1623
+ "hB"
1624
+ ],
1625
+ "VC": [
1626
+ "h",
1627
+ "hb",
1628
+ "H",
1629
+ "hB"
1630
+ ],
1631
+ "VG": [
1632
+ "h",
1633
+ "hb",
1634
+ "H",
1635
+ "hB"
1636
+ ],
1637
+ "VI": [
1638
+ "h",
1639
+ "hb",
1640
+ "H",
1641
+ "hB"
1642
+ ],
1643
+ "ZM": [
1644
+ "h",
1645
+ "hb",
1646
+ "H",
1647
+ "hB"
1648
+ ],
1649
+ "BO": [
1650
+ "H",
1651
+ "hB",
1652
+ "h",
1653
+ "hb"
1654
+ ],
1655
+ "EC": [
1656
+ "H",
1657
+ "hB",
1658
+ "h",
1659
+ "hb"
1660
+ ],
1661
+ "ES": [
1662
+ "H",
1663
+ "hB",
1664
+ "h",
1665
+ "hb"
1666
+ ],
1667
+ "GQ": [
1668
+ "H",
1669
+ "hB",
1670
+ "h",
1671
+ "hb"
1672
+ ],
1673
+ "PE": [
1674
+ "H",
1675
+ "hB",
1676
+ "h",
1677
+ "hb"
1678
+ ],
1679
+ "AE": [
1680
+ "h",
1681
+ "hB",
1682
+ "hb",
1683
+ "H"
1684
+ ],
1685
+ "ar-001": [
1686
+ "h",
1687
+ "hB",
1688
+ "hb",
1689
+ "H"
1690
+ ],
1691
+ "BH": [
1692
+ "h",
1693
+ "hB",
1694
+ "hb",
1695
+ "H"
1696
+ ],
1697
+ "DZ": [
1698
+ "h",
1699
+ "hB",
1700
+ "hb",
1701
+ "H"
1702
+ ],
1703
+ "EG": [
1704
+ "h",
1705
+ "hB",
1706
+ "hb",
1707
+ "H"
1708
+ ],
1709
+ "EH": [
1710
+ "h",
1711
+ "hB",
1712
+ "hb",
1713
+ "H"
1714
+ ],
1715
+ "HK": [
1716
+ "h",
1717
+ "hB",
1718
+ "hb",
1719
+ "H"
1720
+ ],
1721
+ "IQ": [
1722
+ "h",
1723
+ "hB",
1724
+ "hb",
1725
+ "H"
1726
+ ],
1727
+ "JO": [
1728
+ "h",
1729
+ "hB",
1730
+ "hb",
1731
+ "H"
1732
+ ],
1733
+ "KW": [
1734
+ "h",
1735
+ "hB",
1736
+ "hb",
1737
+ "H"
1738
+ ],
1739
+ "LB": [
1740
+ "h",
1741
+ "hB",
1742
+ "hb",
1743
+ "H"
1744
+ ],
1745
+ "LY": [
1746
+ "h",
1747
+ "hB",
1748
+ "hb",
1749
+ "H"
1750
+ ],
1751
+ "MO": [
1752
+ "h",
1753
+ "hB",
1754
+ "hb",
1755
+ "H"
1756
+ ],
1757
+ "MR": [
1758
+ "h",
1759
+ "hB",
1760
+ "hb",
1761
+ "H"
1762
+ ],
1763
+ "OM": [
1764
+ "h",
1765
+ "hB",
1766
+ "hb",
1767
+ "H"
1768
+ ],
1769
+ "PH": [
1770
+ "h",
1771
+ "hB",
1772
+ "hb",
1773
+ "H"
1774
+ ],
1775
+ "PS": [
1776
+ "h",
1777
+ "hB",
1778
+ "hb",
1779
+ "H"
1780
+ ],
1781
+ "QA": [
1782
+ "h",
1783
+ "hB",
1784
+ "hb",
1785
+ "H"
1786
+ ],
1787
+ "SA": [
1788
+ "h",
1789
+ "hB",
1790
+ "hb",
1791
+ "H"
1792
+ ],
1793
+ "SD": [
1794
+ "h",
1795
+ "hB",
1796
+ "hb",
1797
+ "H"
1798
+ ],
1799
+ "SY": [
1800
+ "h",
1801
+ "hB",
1802
+ "hb",
1803
+ "H"
1804
+ ],
1805
+ "TN": [
1806
+ "h",
1807
+ "hB",
1808
+ "hb",
1809
+ "H"
1810
+ ],
1811
+ "YE": [
1812
+ "h",
1813
+ "hB",
1814
+ "hb",
1815
+ "H"
1816
+ ],
1817
+ "AF": [
1818
+ "H",
1819
+ "hb",
1820
+ "hB",
1821
+ "h"
1822
+ ],
1823
+ "LA": [
1824
+ "H",
1825
+ "hb",
1826
+ "hB",
1827
+ "h"
1828
+ ],
1829
+ "CN": [
1830
+ "H",
1831
+ "hB",
1832
+ "hb",
1833
+ "h"
1834
+ ],
1835
+ "LV": [
1836
+ "H",
1837
+ "hB",
1838
+ "hb",
1839
+ "h"
1840
+ ],
1841
+ "TL": [
1842
+ "H",
1843
+ "hB",
1844
+ "hb",
1845
+ "h"
1846
+ ],
1847
+ "zu-ZA": [
1848
+ "H",
1849
+ "hB",
1850
+ "hb",
1851
+ "h"
1852
+ ],
1853
+ "CD": [
1854
+ "hB",
1855
+ "H"
1856
+ ],
1857
+ "IR": [
1858
+ "hB",
1859
+ "H"
1860
+ ],
1861
+ "hi-IN": [
1862
+ "hB",
1863
+ "h",
1864
+ "H"
1865
+ ],
1866
+ "kn-IN": [
1867
+ "hB",
1868
+ "h",
1869
+ "H"
1870
+ ],
1871
+ "ml-IN": [
1872
+ "hB",
1873
+ "h",
1874
+ "H"
1875
+ ],
1876
+ "te-IN": [
1877
+ "hB",
1878
+ "h",
1879
+ "H"
1880
+ ],
1881
+ "KH": [
1882
+ "hB",
1883
+ "h",
1884
+ "H",
1885
+ "hb"
1886
+ ],
1887
+ "ta-IN": [
1888
+ "hB",
1889
+ "h",
1890
+ "hb",
1891
+ "H"
1892
+ ],
1893
+ "BN": [
1894
+ "hb",
1895
+ "hB",
1896
+ "h",
1897
+ "H"
1898
+ ],
1899
+ "MY": [
1900
+ "hb",
1901
+ "hB",
1902
+ "h",
1903
+ "H"
1904
+ ],
1905
+ "ET": [
1906
+ "hB",
1907
+ "hb",
1908
+ "h",
1909
+ "H"
1910
+ ],
1911
+ "gu-IN": [
1912
+ "hB",
1913
+ "hb",
1914
+ "h",
1915
+ "H"
1916
+ ],
1917
+ "mr-IN": [
1918
+ "hB",
1919
+ "hb",
1920
+ "h",
1921
+ "H"
1922
+ ],
1923
+ "pa-IN": [
1924
+ "hB",
1925
+ "hb",
1926
+ "h",
1927
+ "H"
1928
+ ],
1929
+ "TW": [
1930
+ "hB",
1931
+ "hb",
1932
+ "h",
1933
+ "H"
1934
+ ],
1935
+ "KE": [
1936
+ "hB",
1937
+ "hb",
1938
+ "H",
1939
+ "h"
1940
+ ],
1941
+ "MM": [
1942
+ "hB",
1943
+ "hb",
1944
+ "H",
1945
+ "h"
1946
+ ],
1947
+ "TZ": [
1948
+ "hB",
1949
+ "hb",
1950
+ "H",
1951
+ "h"
1952
+ ],
1953
+ "UG": [
1954
+ "hB",
1955
+ "hb",
1956
+ "H",
1957
+ "h"
1958
+ ]
1959
+ };
1960
+
1961
+ /**
1962
+ * Returns the best matching date time pattern if a date time skeleton
1963
+ * pattern is provided with a locale. Follows the Unicode specification:
1964
+ * https://www.unicode.org/reports/tr35/tr35-dates.html#table-mapping-requested-time-skeletons-to-patterns
1965
+ * @param skeleton date time skeleton pattern that possibly includes j, J or C
1966
+ * @param locale
1967
+ */
1968
+ function getBestPattern(skeleton, locale) {
1969
+ var skeletonCopy = '';
1970
+ for (var patternPos = 0; patternPos < skeleton.length; patternPos++) {
1971
+ var patternChar = skeleton.charAt(patternPos);
1972
+ if (patternChar === 'j') {
1973
+ var extraLength = 0;
1974
+ while (patternPos + 1 < skeleton.length &&
1975
+ skeleton.charAt(patternPos + 1) === patternChar) {
1976
+ extraLength++;
1977
+ patternPos++;
1978
+ }
1979
+ var hourLen = 1 + (extraLength & 1);
1980
+ var dayPeriodLen = extraLength < 2 ? 1 : 3 + (extraLength >> 1);
1981
+ var dayPeriodChar = 'a';
1982
+ var hourChar = getDefaultHourSymbolFromLocale(locale);
1983
+ if (hourChar == 'H' || hourChar == 'k') {
1984
+ dayPeriodLen = 0;
1985
+ }
1986
+ while (dayPeriodLen-- > 0) {
1987
+ skeletonCopy += dayPeriodChar;
1988
+ }
1989
+ while (hourLen-- > 0) {
1990
+ skeletonCopy = hourChar + skeletonCopy;
1991
+ }
1992
+ }
1993
+ else if (patternChar === 'J') {
1994
+ skeletonCopy += 'H';
1995
+ }
1996
+ else {
1997
+ skeletonCopy += patternChar;
1998
+ }
1999
+ }
2000
+ return skeletonCopy;
2001
+ }
2002
+ /**
2003
+ * Maps the [hour cycle type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle)
2004
+ * of the given `locale` to the corresponding time pattern.
2005
+ * @param locale
2006
+ */
2007
+ function getDefaultHourSymbolFromLocale(locale) {
2008
+ var hourCycle = locale.hourCycle;
2009
+ if (hourCycle === undefined &&
2010
+ // @ts-ignore hourCycle(s) is not identified yet
2011
+ locale.hourCycles &&
2012
+ // @ts-ignore
2013
+ locale.hourCycles.length) {
2014
+ // @ts-ignore
2015
+ hourCycle = locale.hourCycles[0];
2016
+ }
2017
+ if (hourCycle) {
2018
+ switch (hourCycle) {
2019
+ case 'h24':
2020
+ return 'k';
2021
+ case 'h23':
2022
+ return 'H';
2023
+ case 'h12':
2024
+ return 'h';
2025
+ case 'h11':
2026
+ return 'K';
2027
+ default:
2028
+ throw new Error('Invalid hourCycle');
2029
+ }
2030
+ }
2031
+ // TODO: Once hourCycle is fully supported remove the following with data generation
2032
+ var languageTag = locale.language;
2033
+ var regionTag;
2034
+ if (languageTag !== 'root') {
2035
+ regionTag = locale.maximize().region;
2036
+ }
2037
+ var hourCycles = timeData[regionTag || ''] ||
2038
+ timeData[languageTag || ''] ||
2039
+ timeData["".concat(languageTag, "-001")] ||
2040
+ timeData['001'];
2041
+ return hourCycles[0];
2042
+ }
2043
+
2044
+ var _a;
2045
+ var SPACE_SEPARATOR_START_REGEX = new RegExp("^".concat(SPACE_SEPARATOR_REGEX.source, "*"));
2046
+ var SPACE_SEPARATOR_END_REGEX = new RegExp("".concat(SPACE_SEPARATOR_REGEX.source, "*$"));
2047
+ function createLocation(start, end) {
2048
+ return { start: start, end: end };
2049
+ }
2050
+ // #region Ponyfills
2051
+ // Consolidate these variables up top for easier toggling during debugging
2052
+ var hasNativeStartsWith = !!String.prototype.startsWith;
2053
+ var hasNativeFromCodePoint = !!String.fromCodePoint;
2054
+ var hasNativeFromEntries = !!Object.fromEntries;
2055
+ var hasNativeCodePointAt = !!String.prototype.codePointAt;
2056
+ var hasTrimStart = !!String.prototype.trimStart;
2057
+ var hasTrimEnd = !!String.prototype.trimEnd;
2058
+ var hasNativeIsSafeInteger = !!Number.isSafeInteger;
2059
+ var isSafeInteger = hasNativeIsSafeInteger
2060
+ ? Number.isSafeInteger
2061
+ : function (n) {
2062
+ return (typeof n === 'number' &&
2063
+ isFinite(n) &&
2064
+ Math.floor(n) === n &&
2065
+ Math.abs(n) <= 0x1fffffffffffff);
2066
+ };
2067
+ // IE11 does not support y and u.
2068
+ var REGEX_SUPPORTS_U_AND_Y = true;
2069
+ try {
2070
+ var re = RE('([^\\p{White_Space}\\p{Pattern_Syntax}]*)', 'yu');
2071
+ /**
2072
+ * legacy Edge or Xbox One browser
2073
+ * Unicode flag support: supported
2074
+ * Pattern_Syntax support: not supported
2075
+ * See https://github.com/formatjs/formatjs/issues/2822
2076
+ */
2077
+ REGEX_SUPPORTS_U_AND_Y = ((_a = re.exec('a')) === null || _a === void 0 ? void 0 : _a[0]) === 'a';
2078
+ }
2079
+ catch (_) {
2080
+ REGEX_SUPPORTS_U_AND_Y = false;
2081
+ }
2082
+ var startsWith = hasNativeStartsWith
2083
+ ? // Native
2084
+ function startsWith(s, search, position) {
2085
+ return s.startsWith(search, position);
2086
+ }
2087
+ : // For IE11
2088
+ function startsWith(s, search, position) {
2089
+ return s.slice(position, position + search.length) === search;
2090
+ };
2091
+ var fromCodePoint = hasNativeFromCodePoint
2092
+ ? String.fromCodePoint
2093
+ : // IE11
2094
+ function fromCodePoint() {
2095
+ var codePoints = [];
2096
+ for (var _i = 0; _i < arguments.length; _i++) {
2097
+ codePoints[_i] = arguments[_i];
2098
+ }
2099
+ var elements = '';
2100
+ var length = codePoints.length;
2101
+ var i = 0;
2102
+ var code;
2103
+ while (length > i) {
2104
+ code = codePoints[i++];
2105
+ if (code > 0x10ffff)
2106
+ throw RangeError(code + ' is not a valid code point');
2107
+ elements +=
2108
+ code < 0x10000
2109
+ ? String.fromCharCode(code)
2110
+ : String.fromCharCode(((code -= 0x10000) >> 10) + 0xd800, (code % 0x400) + 0xdc00);
2111
+ }
2112
+ return elements;
2113
+ };
2114
+ var fromEntries =
2115
+ // native
2116
+ hasNativeFromEntries
2117
+ ? Object.fromEntries
2118
+ : // Ponyfill
2119
+ function fromEntries(entries) {
2120
+ var obj = {};
2121
+ for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
2122
+ var _a = entries_1[_i], k = _a[0], v = _a[1];
2123
+ obj[k] = v;
2124
+ }
2125
+ return obj;
2126
+ };
2127
+ var codePointAt = hasNativeCodePointAt
2128
+ ? // Native
2129
+ function codePointAt(s, index) {
2130
+ return s.codePointAt(index);
2131
+ }
2132
+ : // IE 11
2133
+ function codePointAt(s, index) {
2134
+ var size = s.length;
2135
+ if (index < 0 || index >= size) {
2136
+ return undefined;
2137
+ }
2138
+ var first = s.charCodeAt(index);
2139
+ var second;
2140
+ return first < 0xd800 ||
2141
+ first > 0xdbff ||
2142
+ index + 1 === size ||
2143
+ (second = s.charCodeAt(index + 1)) < 0xdc00 ||
2144
+ second > 0xdfff
2145
+ ? first
2146
+ : ((first - 0xd800) << 10) + (second - 0xdc00) + 0x10000;
2147
+ };
2148
+ var trimStart = hasTrimStart
2149
+ ? // Native
2150
+ function trimStart(s) {
2151
+ return s.trimStart();
2152
+ }
2153
+ : // Ponyfill
2154
+ function trimStart(s) {
2155
+ return s.replace(SPACE_SEPARATOR_START_REGEX, '');
2156
+ };
2157
+ var trimEnd = hasTrimEnd
2158
+ ? // Native
2159
+ function trimEnd(s) {
2160
+ return s.trimEnd();
2161
+ }
2162
+ : // Ponyfill
2163
+ function trimEnd(s) {
2164
+ return s.replace(SPACE_SEPARATOR_END_REGEX, '');
2165
+ };
2166
+ // Prevent minifier to translate new RegExp to literal form that might cause syntax error on IE11.
2167
+ function RE(s, flag) {
2168
+ return new RegExp(s, flag);
2169
+ }
2170
+ // #endregion
2171
+ var matchIdentifierAtIndex;
2172
+ if (REGEX_SUPPORTS_U_AND_Y) {
2173
+ // Native
2174
+ var IDENTIFIER_PREFIX_RE_1 = RE('([^\\p{White_Space}\\p{Pattern_Syntax}]*)', 'yu');
2175
+ matchIdentifierAtIndex = function matchIdentifierAtIndex(s, index) {
2176
+ var _a;
2177
+ IDENTIFIER_PREFIX_RE_1.lastIndex = index;
2178
+ var match = IDENTIFIER_PREFIX_RE_1.exec(s);
2179
+ return (_a = match[1]) !== null && _a !== void 0 ? _a : '';
2180
+ };
2181
+ }
2182
+ else {
2183
+ // IE11
2184
+ matchIdentifierAtIndex = function matchIdentifierAtIndex(s, index) {
2185
+ var match = [];
2186
+ while (true) {
2187
+ var c = codePointAt(s, index);
2188
+ if (c === undefined || _isWhiteSpace(c) || _isPatternSyntax(c)) {
2189
+ break;
2190
+ }
2191
+ match.push(c);
2192
+ index += c >= 0x10000 ? 2 : 1;
2193
+ }
2194
+ return fromCodePoint.apply(void 0, match);
2195
+ };
2196
+ }
2197
+ var Parser = /** @class */ (function () {
2198
+ function Parser(message, options) {
2199
+ if (options === void 0) { options = {}; }
2200
+ this.message = message;
2201
+ this.position = { offset: 0, line: 1, column: 1 };
2202
+ this.ignoreTag = !!options.ignoreTag;
2203
+ this.locale = options.locale;
2204
+ this.requiresOtherClause = !!options.requiresOtherClause;
2205
+ this.shouldParseSkeletons = !!options.shouldParseSkeletons;
2206
+ }
2207
+ Parser.prototype.parse = function () {
2208
+ if (this.offset() !== 0) {
2209
+ throw Error('parser can only be used once');
2210
+ }
2211
+ return this.parseMessage(0, '', false);
2212
+ };
2213
+ Parser.prototype.parseMessage = function (nestingLevel, parentArgType, expectingCloseTag) {
2214
+ var elements = [];
2215
+ while (!this.isEOF()) {
2216
+ var char = this.char();
2217
+ if (char === 123 /* `{` */) {
2218
+ var result = this.parseArgument(nestingLevel, expectingCloseTag);
2219
+ if (result.err) {
2220
+ return result;
2221
+ }
2222
+ elements.push(result.val);
2223
+ }
2224
+ else if (char === 125 /* `}` */ && nestingLevel > 0) {
2225
+ break;
2226
+ }
2227
+ else if (char === 35 /* `#` */ &&
2228
+ (parentArgType === 'plural' || parentArgType === 'selectordinal')) {
2229
+ var position = this.clonePosition();
2230
+ this.bump();
2231
+ elements.push({
2232
+ type: TYPE.pound,
2233
+ location: createLocation(position, this.clonePosition()),
2234
+ });
2235
+ }
2236
+ else if (char === 60 /* `<` */ &&
2237
+ !this.ignoreTag &&
2238
+ this.peek() === 47 // char code for '/'
2239
+ ) {
2240
+ if (expectingCloseTag) {
2241
+ break;
2242
+ }
2243
+ else {
2244
+ return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(this.clonePosition(), this.clonePosition()));
2245
+ }
2246
+ }
2247
+ else if (char === 60 /* `<` */ &&
2248
+ !this.ignoreTag &&
2249
+ _isAlpha(this.peek() || 0)) {
2250
+ var result = this.parseTag(nestingLevel, parentArgType);
2251
+ if (result.err) {
2252
+ return result;
2253
+ }
2254
+ elements.push(result.val);
2255
+ }
2256
+ else {
2257
+ var result = this.parseLiteral(nestingLevel, parentArgType);
2258
+ if (result.err) {
2259
+ return result;
2260
+ }
2261
+ elements.push(result.val);
2262
+ }
2263
+ }
2264
+ return { val: elements, err: null };
2265
+ };
2266
+ /**
2267
+ * A tag name must start with an ASCII lower/upper case letter. The grammar is based on the
2268
+ * [custom element name][] except that a dash is NOT always mandatory and uppercase letters
2269
+ * are accepted:
2270
+ *
2271
+ * ```
2272
+ * tag ::= "<" tagName (whitespace)* "/>" | "<" tagName (whitespace)* ">" message "</" tagName (whitespace)* ">"
2273
+ * tagName ::= [a-z] (PENChar)*
2274
+ * PENChar ::=
2275
+ * "-" | "." | [0-9] | "_" | [a-z] | [A-Z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
2276
+ * [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
2277
+ * [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
2278
+ * ```
2279
+ *
2280
+ * [custom element name]: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
2281
+ * NOTE: We're a bit more lax here since HTML technically does not allow uppercase HTML element but we do
2282
+ * since other tag-based engines like React allow it
2283
+ */
2284
+ Parser.prototype.parseTag = function (nestingLevel, parentArgType) {
2285
+ var startPosition = this.clonePosition();
2286
+ this.bump(); // `<`
2287
+ var tagName = this.parseTagName();
2288
+ this.bumpSpace();
2289
+ if (this.bumpIf('/>')) {
2290
+ // Self closing tag
2291
+ return {
2292
+ val: {
2293
+ type: TYPE.literal,
2294
+ value: "<".concat(tagName, "/>"),
2295
+ location: createLocation(startPosition, this.clonePosition()),
2296
+ },
2297
+ err: null,
2298
+ };
2299
+ }
2300
+ else if (this.bumpIf('>')) {
2301
+ var childrenResult = this.parseMessage(nestingLevel + 1, parentArgType, true);
2302
+ if (childrenResult.err) {
2303
+ return childrenResult;
2304
+ }
2305
+ var children = childrenResult.val;
2306
+ // Expecting a close tag
2307
+ var endTagStartPosition = this.clonePosition();
2308
+ if (this.bumpIf('</')) {
2309
+ if (this.isEOF() || !_isAlpha(this.char())) {
2310
+ return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
2311
+ }
2312
+ var closingTagNameStartPosition = this.clonePosition();
2313
+ var closingTagName = this.parseTagName();
2314
+ if (tagName !== closingTagName) {
2315
+ return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(closingTagNameStartPosition, this.clonePosition()));
2316
+ }
2317
+ this.bumpSpace();
2318
+ if (!this.bumpIf('>')) {
2319
+ return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
2320
+ }
2321
+ return {
2322
+ val: {
2323
+ type: TYPE.tag,
2324
+ value: tagName,
2325
+ children: children,
2326
+ location: createLocation(startPosition, this.clonePosition()),
2327
+ },
2328
+ err: null,
2329
+ };
2330
+ }
2331
+ else {
2332
+ return this.error(ErrorKind.UNCLOSED_TAG, createLocation(startPosition, this.clonePosition()));
2333
+ }
2334
+ }
2335
+ else {
2336
+ return this.error(ErrorKind.INVALID_TAG, createLocation(startPosition, this.clonePosition()));
2337
+ }
2338
+ };
2339
+ /**
2340
+ * This method assumes that the caller has peeked ahead for the first tag character.
2341
+ */
2342
+ Parser.prototype.parseTagName = function () {
2343
+ var startOffset = this.offset();
2344
+ this.bump(); // the first tag name character
2345
+ while (!this.isEOF() && _isPotentialElementNameChar(this.char())) {
2346
+ this.bump();
2347
+ }
2348
+ return this.message.slice(startOffset, this.offset());
2349
+ };
2350
+ Parser.prototype.parseLiteral = function (nestingLevel, parentArgType) {
2351
+ var start = this.clonePosition();
2352
+ var value = '';
2353
+ while (true) {
2354
+ var parseQuoteResult = this.tryParseQuote(parentArgType);
2355
+ if (parseQuoteResult) {
2356
+ value += parseQuoteResult;
2357
+ continue;
2358
+ }
2359
+ var parseUnquotedResult = this.tryParseUnquoted(nestingLevel, parentArgType);
2360
+ if (parseUnquotedResult) {
2361
+ value += parseUnquotedResult;
2362
+ continue;
2363
+ }
2364
+ var parseLeftAngleResult = this.tryParseLeftAngleBracket();
2365
+ if (parseLeftAngleResult) {
2366
+ value += parseLeftAngleResult;
2367
+ continue;
2368
+ }
2369
+ break;
2370
+ }
2371
+ var location = createLocation(start, this.clonePosition());
2372
+ return {
2373
+ val: { type: TYPE.literal, value: value, location: location },
2374
+ err: null,
2375
+ };
2376
+ };
2377
+ Parser.prototype.tryParseLeftAngleBracket = function () {
2378
+ if (!this.isEOF() &&
2379
+ this.char() === 60 /* `<` */ &&
2380
+ (this.ignoreTag ||
2381
+ // If at the opening tag or closing tag position, bail.
2382
+ !_isAlphaOrSlash(this.peek() || 0))) {
2383
+ this.bump(); // `<`
2384
+ return '<';
2385
+ }
2386
+ return null;
2387
+ };
2388
+ /**
2389
+ * Starting with ICU 4.8, an ASCII apostrophe only starts quoted text if it immediately precedes
2390
+ * a character that requires quoting (that is, "only where needed"), and works the same in
2391
+ * nested messages as on the top level of the pattern. The new behavior is otherwise compatible.
2392
+ */
2393
+ Parser.prototype.tryParseQuote = function (parentArgType) {
2394
+ if (this.isEOF() || this.char() !== 39 /* `'` */) {
2395
+ return null;
2396
+ }
2397
+ // Parse escaped char following the apostrophe, or early return if there is no escaped char.
2398
+ // Check if is valid escaped character
2399
+ switch (this.peek()) {
2400
+ case 39 /* `'` */:
2401
+ // double quote, should return as a single quote.
2402
+ this.bump();
2403
+ this.bump();
2404
+ return "'";
2405
+ // '{', '<', '>', '}'
2406
+ case 123:
2407
+ case 60:
2408
+ case 62:
2409
+ case 125:
2410
+ break;
2411
+ case 35: // '#'
2412
+ if (parentArgType === 'plural' || parentArgType === 'selectordinal') {
2413
+ break;
2414
+ }
2415
+ return null;
2416
+ default:
2417
+ return null;
2418
+ }
2419
+ this.bump(); // apostrophe
2420
+ var codePoints = [this.char()]; // escaped char
2421
+ this.bump();
2422
+ // read chars until the optional closing apostrophe is found
2423
+ while (!this.isEOF()) {
2424
+ var ch = this.char();
2425
+ if (ch === 39 /* `'` */) {
2426
+ if (this.peek() === 39 /* `'` */) {
2427
+ codePoints.push(39);
2428
+ // Bump one more time because we need to skip 2 characters.
2429
+ this.bump();
2430
+ }
2431
+ else {
2432
+ // Optional closing apostrophe.
2433
+ this.bump();
2434
+ break;
2435
+ }
2436
+ }
2437
+ else {
2438
+ codePoints.push(ch);
2439
+ }
2440
+ this.bump();
2441
+ }
2442
+ return fromCodePoint.apply(void 0, codePoints);
2443
+ };
2444
+ Parser.prototype.tryParseUnquoted = function (nestingLevel, parentArgType) {
2445
+ if (this.isEOF()) {
2446
+ return null;
2447
+ }
2448
+ var ch = this.char();
2449
+ if (ch === 60 /* `<` */ ||
2450
+ ch === 123 /* `{` */ ||
2451
+ (ch === 35 /* `#` */ &&
2452
+ (parentArgType === 'plural' || parentArgType === 'selectordinal')) ||
2453
+ (ch === 125 /* `}` */ && nestingLevel > 0)) {
2454
+ return null;
2455
+ }
2456
+ else {
2457
+ this.bump();
2458
+ return fromCodePoint(ch);
2459
+ }
2460
+ };
2461
+ Parser.prototype.parseArgument = function (nestingLevel, expectingCloseTag) {
2462
+ var openingBracePosition = this.clonePosition();
2463
+ this.bump(); // `{`
2464
+ this.bumpSpace();
2465
+ if (this.isEOF()) {
2466
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
2467
+ }
2468
+ if (this.char() === 125 /* `}` */) {
2469
+ this.bump();
2470
+ return this.error(ErrorKind.EMPTY_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
2471
+ }
2472
+ // argument name
2473
+ var value = this.parseIdentifierIfPossible().value;
2474
+ if (!value) {
2475
+ return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
2476
+ }
2477
+ this.bumpSpace();
2478
+ if (this.isEOF()) {
2479
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
2480
+ }
2481
+ switch (this.char()) {
2482
+ // Simple argument: `{name}`
2483
+ case 125 /* `}` */: {
2484
+ this.bump(); // `}`
2485
+ return {
2486
+ val: {
2487
+ type: TYPE.argument,
2488
+ // value does not include the opening and closing braces.
2489
+ value: value,
2490
+ location: createLocation(openingBracePosition, this.clonePosition()),
2491
+ },
2492
+ err: null,
2493
+ };
2494
+ }
2495
+ // Argument with options: `{name, format, ...}`
2496
+ case 44 /* `,` */: {
2497
+ this.bump(); // `,`
2498
+ this.bumpSpace();
2499
+ if (this.isEOF()) {
2500
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
2501
+ }
2502
+ return this.parseArgumentOptions(nestingLevel, expectingCloseTag, value, openingBracePosition);
2503
+ }
2504
+ default:
2505
+ return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
2506
+ }
2507
+ };
2508
+ /**
2509
+ * Advance the parser until the end of the identifier, if it is currently on
2510
+ * an identifier character. Return an empty string otherwise.
2511
+ */
2512
+ Parser.prototype.parseIdentifierIfPossible = function () {
2513
+ var startingPosition = this.clonePosition();
2514
+ var startOffset = this.offset();
2515
+ var value = matchIdentifierAtIndex(this.message, startOffset);
2516
+ var endOffset = startOffset + value.length;
2517
+ this.bumpTo(endOffset);
2518
+ var endPosition = this.clonePosition();
2519
+ var location = createLocation(startingPosition, endPosition);
2520
+ return { value: value, location: location };
2521
+ };
2522
+ Parser.prototype.parseArgumentOptions = function (nestingLevel, expectingCloseTag, value, openingBracePosition) {
2523
+ var _a;
2524
+ // Parse this range:
2525
+ // {name, type, style}
2526
+ // ^---^
2527
+ var typeStartPosition = this.clonePosition();
2528
+ var argType = this.parseIdentifierIfPossible().value;
2529
+ var typeEndPosition = this.clonePosition();
2530
+ switch (argType) {
2531
+ case '':
2532
+ // Expecting a style string number, date, time, plural, selectordinal, or select.
2533
+ return this.error(ErrorKind.EXPECT_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
2534
+ case 'number':
2535
+ case 'date':
2536
+ case 'time': {
2537
+ // Parse this range:
2538
+ // {name, number, style}
2539
+ // ^-------^
2540
+ this.bumpSpace();
2541
+ var styleAndLocation = null;
2542
+ if (this.bumpIf(',')) {
2543
+ this.bumpSpace();
2544
+ var styleStartPosition = this.clonePosition();
2545
+ var result = this.parseSimpleArgStyleIfPossible();
2546
+ if (result.err) {
2547
+ return result;
2548
+ }
2549
+ var style = trimEnd(result.val);
2550
+ if (style.length === 0) {
2551
+ return this.error(ErrorKind.EXPECT_ARGUMENT_STYLE, createLocation(this.clonePosition(), this.clonePosition()));
2552
+ }
2553
+ var styleLocation = createLocation(styleStartPosition, this.clonePosition());
2554
+ styleAndLocation = { style: style, styleLocation: styleLocation };
2555
+ }
2556
+ var argCloseResult = this.tryParseArgumentClose(openingBracePosition);
2557
+ if (argCloseResult.err) {
2558
+ return argCloseResult;
2559
+ }
2560
+ var location_1 = createLocation(openingBracePosition, this.clonePosition());
2561
+ // Extract style or skeleton
2562
+ if (styleAndLocation && startsWith(styleAndLocation === null || styleAndLocation === void 0 ? void 0 : styleAndLocation.style, '::', 0)) {
2563
+ // Skeleton starts with `::`.
2564
+ var skeleton = trimStart(styleAndLocation.style.slice(2));
2565
+ if (argType === 'number') {
2566
+ var result = this.parseNumberSkeletonFromString(skeleton, styleAndLocation.styleLocation);
2567
+ if (result.err) {
2568
+ return result;
2569
+ }
2570
+ return {
2571
+ val: { type: TYPE.number, value: value, location: location_1, style: result.val },
2572
+ err: null,
2573
+ };
2574
+ }
2575
+ else {
2576
+ if (skeleton.length === 0) {
2577
+ return this.error(ErrorKind.EXPECT_DATE_TIME_SKELETON, location_1);
2578
+ }
2579
+ var dateTimePattern = skeleton;
2580
+ // Get "best match" pattern only if locale is passed, if not, let it
2581
+ // pass as-is where `parseDateTimeSkeleton()` will throw an error
2582
+ // for unsupported patterns.
2583
+ if (this.locale) {
2584
+ dateTimePattern = getBestPattern(skeleton, this.locale);
2585
+ }
2586
+ var style = {
2587
+ type: SKELETON_TYPE.dateTime,
2588
+ pattern: dateTimePattern,
2589
+ location: styleAndLocation.styleLocation,
2590
+ parsedOptions: this.shouldParseSkeletons
2591
+ ? parseDateTimeSkeleton(dateTimePattern)
2592
+ : {},
2593
+ };
2594
+ var type = argType === 'date' ? TYPE.date : TYPE.time;
2595
+ return {
2596
+ val: { type: type, value: value, location: location_1, style: style },
2597
+ err: null,
2598
+ };
2599
+ }
2600
+ }
2601
+ // Regular style or no style.
2602
+ return {
2603
+ val: {
2604
+ type: argType === 'number'
2605
+ ? TYPE.number
2606
+ : argType === 'date'
2607
+ ? TYPE.date
2608
+ : TYPE.time,
2609
+ value: value,
2610
+ location: location_1,
2611
+ style: (_a = styleAndLocation === null || styleAndLocation === void 0 ? void 0 : styleAndLocation.style) !== null && _a !== void 0 ? _a : null,
2612
+ },
2613
+ err: null,
2614
+ };
2615
+ }
2616
+ case 'plural':
2617
+ case 'selectordinal':
2618
+ case 'select': {
2619
+ // Parse this range:
2620
+ // {name, plural, options}
2621
+ // ^---------^
2622
+ var typeEndPosition_1 = this.clonePosition();
2623
+ this.bumpSpace();
2624
+ if (!this.bumpIf(',')) {
2625
+ return this.error(ErrorKind.EXPECT_SELECT_ARGUMENT_OPTIONS, createLocation(typeEndPosition_1, __assign({}, typeEndPosition_1)));
2626
+ }
2627
+ this.bumpSpace();
2628
+ // Parse offset:
2629
+ // {name, plural, offset:1, options}
2630
+ // ^-----^
2631
+ //
2632
+ // or the first option:
2633
+ //
2634
+ // {name, plural, one {...} other {...}}
2635
+ // ^--^
2636
+ var identifierAndLocation = this.parseIdentifierIfPossible();
2637
+ var pluralOffset = 0;
2638
+ if (argType !== 'select' && identifierAndLocation.value === 'offset') {
2639
+ if (!this.bumpIf(':')) {
2640
+ return this.error(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, createLocation(this.clonePosition(), this.clonePosition()));
2641
+ }
2642
+ this.bumpSpace();
2643
+ var result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, ErrorKind.INVALID_PLURAL_ARGUMENT_OFFSET_VALUE);
2644
+ if (result.err) {
2645
+ return result;
2646
+ }
2647
+ // Parse another identifier for option parsing
2648
+ this.bumpSpace();
2649
+ identifierAndLocation = this.parseIdentifierIfPossible();
2650
+ pluralOffset = result.val;
2651
+ }
2652
+ var optionsResult = this.tryParsePluralOrSelectOptions(nestingLevel, argType, expectingCloseTag, identifierAndLocation);
2653
+ if (optionsResult.err) {
2654
+ return optionsResult;
2655
+ }
2656
+ var argCloseResult = this.tryParseArgumentClose(openingBracePosition);
2657
+ if (argCloseResult.err) {
2658
+ return argCloseResult;
2659
+ }
2660
+ var location_2 = createLocation(openingBracePosition, this.clonePosition());
2661
+ if (argType === 'select') {
2662
+ return {
2663
+ val: {
2664
+ type: TYPE.select,
2665
+ value: value,
2666
+ options: fromEntries(optionsResult.val),
2667
+ location: location_2,
2668
+ },
2669
+ err: null,
2670
+ };
2671
+ }
2672
+ else {
2673
+ return {
2674
+ val: {
2675
+ type: TYPE.plural,
2676
+ value: value,
2677
+ options: fromEntries(optionsResult.val),
2678
+ offset: pluralOffset,
2679
+ pluralType: argType === 'plural' ? 'cardinal' : 'ordinal',
2680
+ location: location_2,
2681
+ },
2682
+ err: null,
2683
+ };
2684
+ }
2685
+ }
2686
+ default:
2687
+ return this.error(ErrorKind.INVALID_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
2688
+ }
2689
+ };
2690
+ Parser.prototype.tryParseArgumentClose = function (openingBracePosition) {
2691
+ // Parse: {value, number, ::currency/GBP }
2692
+ //
2693
+ if (this.isEOF() || this.char() !== 125 /* `}` */) {
2694
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
2695
+ }
2696
+ this.bump(); // `}`
2697
+ return { val: true, err: null };
2698
+ };
2699
+ /**
2700
+ * See: https://github.com/unicode-org/icu/blob/af7ed1f6d2298013dc303628438ec4abe1f16479/icu4c/source/common/messagepattern.cpp#L659
2701
+ */
2702
+ Parser.prototype.parseSimpleArgStyleIfPossible = function () {
2703
+ var nestedBraces = 0;
2704
+ var startPosition = this.clonePosition();
2705
+ while (!this.isEOF()) {
2706
+ var ch = this.char();
2707
+ switch (ch) {
2708
+ case 39 /* `'` */: {
2709
+ // Treat apostrophe as quoting but include it in the style part.
2710
+ // Find the end of the quoted literal text.
2711
+ this.bump();
2712
+ var apostrophePosition = this.clonePosition();
2713
+ if (!this.bumpUntil("'")) {
2714
+ return this.error(ErrorKind.UNCLOSED_QUOTE_IN_ARGUMENT_STYLE, createLocation(apostrophePosition, this.clonePosition()));
2715
+ }
2716
+ this.bump();
2717
+ break;
2718
+ }
2719
+ case 123 /* `{` */: {
2720
+ nestedBraces += 1;
2721
+ this.bump();
2722
+ break;
2723
+ }
2724
+ case 125 /* `}` */: {
2725
+ if (nestedBraces > 0) {
2726
+ nestedBraces -= 1;
2727
+ }
2728
+ else {
2729
+ return {
2730
+ val: this.message.slice(startPosition.offset, this.offset()),
2731
+ err: null,
2732
+ };
2733
+ }
2734
+ break;
2735
+ }
2736
+ default:
2737
+ this.bump();
2738
+ break;
2739
+ }
2740
+ }
2741
+ return {
2742
+ val: this.message.slice(startPosition.offset, this.offset()),
2743
+ err: null,
2744
+ };
2745
+ };
2746
+ Parser.prototype.parseNumberSkeletonFromString = function (skeleton, location) {
2747
+ var tokens = [];
2748
+ try {
2749
+ tokens = parseNumberSkeletonFromString(skeleton);
2750
+ }
2751
+ catch (e) {
2752
+ return this.error(ErrorKind.INVALID_NUMBER_SKELETON, location);
2753
+ }
2754
+ return {
2755
+ val: {
2756
+ type: SKELETON_TYPE.number,
2757
+ tokens: tokens,
2758
+ location: location,
2759
+ parsedOptions: this.shouldParseSkeletons
2760
+ ? parseNumberSkeleton(tokens)
2761
+ : {},
2762
+ },
2763
+ err: null,
2764
+ };
2765
+ };
2766
+ /**
2767
+ * @param nesting_level The current nesting level of messages.
2768
+ * This can be positive when parsing message fragment in select or plural argument options.
2769
+ * @param parent_arg_type The parent argument's type.
2770
+ * @param parsed_first_identifier If provided, this is the first identifier-like selector of
2771
+ * the argument. It is a by-product of a previous parsing attempt.
2772
+ * @param expecting_close_tag If true, this message is directly or indirectly nested inside
2773
+ * between a pair of opening and closing tags. The nested message will not parse beyond
2774
+ * the closing tag boundary.
2775
+ */
2776
+ Parser.prototype.tryParsePluralOrSelectOptions = function (nestingLevel, parentArgType, expectCloseTag, parsedFirstIdentifier) {
2777
+ var _a;
2778
+ var hasOtherClause = false;
2779
+ var options = [];
2780
+ var parsedSelectors = new Set();
2781
+ var selector = parsedFirstIdentifier.value, selectorLocation = parsedFirstIdentifier.location;
2782
+ // Parse:
2783
+ // one {one apple}
2784
+ // ^--^
2785
+ while (true) {
2786
+ if (selector.length === 0) {
2787
+ var startPosition = this.clonePosition();
2788
+ if (parentArgType !== 'select' && this.bumpIf('=')) {
2789
+ // Try parse `={number}` selector
2790
+ var result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, ErrorKind.INVALID_PLURAL_ARGUMENT_SELECTOR);
2791
+ if (result.err) {
2792
+ return result;
2793
+ }
2794
+ selectorLocation = createLocation(startPosition, this.clonePosition());
2795
+ selector = this.message.slice(startPosition.offset, this.offset());
2796
+ }
2797
+ else {
2798
+ break;
2799
+ }
2800
+ }
2801
+ // Duplicate selector clauses
2802
+ if (parsedSelectors.has(selector)) {
2803
+ return this.error(parentArgType === 'select'
2804
+ ? ErrorKind.DUPLICATE_SELECT_ARGUMENT_SELECTOR
2805
+ : ErrorKind.DUPLICATE_PLURAL_ARGUMENT_SELECTOR, selectorLocation);
2806
+ }
2807
+ if (selector === 'other') {
2808
+ hasOtherClause = true;
2809
+ }
2810
+ // Parse:
2811
+ // one {one apple}
2812
+ // ^----------^
2813
+ this.bumpSpace();
2814
+ var openingBracePosition = this.clonePosition();
2815
+ if (!this.bumpIf('{')) {
2816
+ return this.error(parentArgType === 'select'
2817
+ ? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT
2818
+ : ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT, createLocation(this.clonePosition(), this.clonePosition()));
2819
+ }
2820
+ var fragmentResult = this.parseMessage(nestingLevel + 1, parentArgType, expectCloseTag);
2821
+ if (fragmentResult.err) {
2822
+ return fragmentResult;
2823
+ }
2824
+ var argCloseResult = this.tryParseArgumentClose(openingBracePosition);
2825
+ if (argCloseResult.err) {
2826
+ return argCloseResult;
2827
+ }
2828
+ options.push([
2829
+ selector,
2830
+ {
2831
+ value: fragmentResult.val,
2832
+ location: createLocation(openingBracePosition, this.clonePosition()),
2833
+ },
2834
+ ]);
2835
+ // Keep track of the existing selectors
2836
+ parsedSelectors.add(selector);
2837
+ // Prep next selector clause.
2838
+ this.bumpSpace();
2839
+ (_a = this.parseIdentifierIfPossible(), selector = _a.value, selectorLocation = _a.location);
2840
+ }
2841
+ if (options.length === 0) {
2842
+ return this.error(parentArgType === 'select'
2843
+ ? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR
2844
+ : ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, createLocation(this.clonePosition(), this.clonePosition()));
2845
+ }
2846
+ if (this.requiresOtherClause && !hasOtherClause) {
2847
+ return this.error(ErrorKind.MISSING_OTHER_CLAUSE, createLocation(this.clonePosition(), this.clonePosition()));
2848
+ }
2849
+ return { val: options, err: null };
2850
+ };
2851
+ Parser.prototype.tryParseDecimalInteger = function (expectNumberError, invalidNumberError) {
2852
+ var sign = 1;
2853
+ var startingPosition = this.clonePosition();
2854
+ if (this.bumpIf('+')) ;
2855
+ else if (this.bumpIf('-')) {
2856
+ sign = -1;
2857
+ }
2858
+ var hasDigits = false;
2859
+ var decimal = 0;
2860
+ while (!this.isEOF()) {
2861
+ var ch = this.char();
2862
+ if (ch >= 48 /* `0` */ && ch <= 57 /* `9` */) {
2863
+ hasDigits = true;
2864
+ decimal = decimal * 10 + (ch - 48);
2865
+ this.bump();
2866
+ }
2867
+ else {
2868
+ break;
2869
+ }
2870
+ }
2871
+ var location = createLocation(startingPosition, this.clonePosition());
2872
+ if (!hasDigits) {
2873
+ return this.error(expectNumberError, location);
2874
+ }
2875
+ decimal *= sign;
2876
+ if (!isSafeInteger(decimal)) {
2877
+ return this.error(invalidNumberError, location);
2878
+ }
2879
+ return { val: decimal, err: null };
2880
+ };
2881
+ Parser.prototype.offset = function () {
2882
+ return this.position.offset;
2883
+ };
2884
+ Parser.prototype.isEOF = function () {
2885
+ return this.offset() === this.message.length;
2886
+ };
2887
+ Parser.prototype.clonePosition = function () {
2888
+ // This is much faster than `Object.assign` or spread.
2889
+ return {
2890
+ offset: this.position.offset,
2891
+ line: this.position.line,
2892
+ column: this.position.column,
2893
+ };
2894
+ };
2895
+ /**
2896
+ * Return the code point at the current position of the parser.
2897
+ * Throws if the index is out of bound.
2898
+ */
2899
+ Parser.prototype.char = function () {
2900
+ var offset = this.position.offset;
2901
+ if (offset >= this.message.length) {
2902
+ throw Error('out of bound');
2903
+ }
2904
+ var code = codePointAt(this.message, offset);
2905
+ if (code === undefined) {
2906
+ throw Error("Offset ".concat(offset, " is at invalid UTF-16 code unit boundary"));
2907
+ }
2908
+ return code;
2909
+ };
2910
+ Parser.prototype.error = function (kind, location) {
2911
+ return {
2912
+ val: null,
2913
+ err: {
2914
+ kind: kind,
2915
+ message: this.message,
2916
+ location: location,
2917
+ },
2918
+ };
2919
+ };
2920
+ /** Bump the parser to the next UTF-16 code unit. */
2921
+ Parser.prototype.bump = function () {
2922
+ if (this.isEOF()) {
2923
+ return;
2924
+ }
2925
+ var code = this.char();
2926
+ if (code === 10 /* '\n' */) {
2927
+ this.position.line += 1;
2928
+ this.position.column = 1;
2929
+ this.position.offset += 1;
2930
+ }
2931
+ else {
2932
+ this.position.column += 1;
2933
+ // 0 ~ 0x10000 -> unicode BMP, otherwise skip the surrogate pair.
2934
+ this.position.offset += code < 0x10000 ? 1 : 2;
2935
+ }
2936
+ };
2937
+ /**
2938
+ * If the substring starting at the current position of the parser has
2939
+ * the given prefix, then bump the parser to the character immediately
2940
+ * following the prefix and return true. Otherwise, don't bump the parser
2941
+ * and return false.
2942
+ */
2943
+ Parser.prototype.bumpIf = function (prefix) {
2944
+ if (startsWith(this.message, prefix, this.offset())) {
2945
+ for (var i = 0; i < prefix.length; i++) {
2946
+ this.bump();
2947
+ }
2948
+ return true;
2949
+ }
2950
+ return false;
2951
+ };
2952
+ /**
2953
+ * Bump the parser until the pattern character is found and return `true`.
2954
+ * Otherwise bump to the end of the file and return `false`.
2955
+ */
2956
+ Parser.prototype.bumpUntil = function (pattern) {
2957
+ var currentOffset = this.offset();
2958
+ var index = this.message.indexOf(pattern, currentOffset);
2959
+ if (index >= 0) {
2960
+ this.bumpTo(index);
2961
+ return true;
2962
+ }
2963
+ else {
2964
+ this.bumpTo(this.message.length);
2965
+ return false;
2966
+ }
2967
+ };
2968
+ /**
2969
+ * Bump the parser to the target offset.
2970
+ * If target offset is beyond the end of the input, bump the parser to the end of the input.
2971
+ */
2972
+ Parser.prototype.bumpTo = function (targetOffset) {
2973
+ if (this.offset() > targetOffset) {
2974
+ throw Error("targetOffset ".concat(targetOffset, " must be greater than or equal to the current offset ").concat(this.offset()));
2975
+ }
2976
+ targetOffset = Math.min(targetOffset, this.message.length);
2977
+ while (true) {
2978
+ var offset = this.offset();
2979
+ if (offset === targetOffset) {
2980
+ break;
2981
+ }
2982
+ if (offset > targetOffset) {
2983
+ throw Error("targetOffset ".concat(targetOffset, " is at invalid UTF-16 code unit boundary"));
2984
+ }
2985
+ this.bump();
2986
+ if (this.isEOF()) {
2987
+ break;
2988
+ }
2989
+ }
2990
+ };
2991
+ /** advance the parser through all whitespace to the next non-whitespace code unit. */
2992
+ Parser.prototype.bumpSpace = function () {
2993
+ while (!this.isEOF() && _isWhiteSpace(this.char())) {
2994
+ this.bump();
2995
+ }
2996
+ };
2997
+ /**
2998
+ * Peek at the *next* Unicode codepoint in the input without advancing the parser.
2999
+ * If the input has been exhausted, then this returns null.
3000
+ */
3001
+ Parser.prototype.peek = function () {
3002
+ if (this.isEOF()) {
3003
+ return null;
3004
+ }
3005
+ var code = this.char();
3006
+ var offset = this.offset();
3007
+ var nextCode = this.message.charCodeAt(offset + (code >= 0x10000 ? 2 : 1));
3008
+ return nextCode !== null && nextCode !== void 0 ? nextCode : null;
3009
+ };
3010
+ return Parser;
3011
+ }());
3012
+ /**
3013
+ * This check if codepoint is alphabet (lower & uppercase)
3014
+ * @param codepoint
3015
+ * @returns
3016
+ */
3017
+ function _isAlpha(codepoint) {
3018
+ return ((codepoint >= 97 && codepoint <= 122) ||
3019
+ (codepoint >= 65 && codepoint <= 90));
3020
+ }
3021
+ function _isAlphaOrSlash(codepoint) {
3022
+ return _isAlpha(codepoint) || codepoint === 47; /* '/' */
3023
+ }
3024
+ /** See `parseTag` function docs. */
3025
+ function _isPotentialElementNameChar(c) {
3026
+ return (c === 45 /* '-' */ ||
3027
+ c === 46 /* '.' */ ||
3028
+ (c >= 48 && c <= 57) /* 0..9 */ ||
3029
+ c === 95 /* '_' */ ||
3030
+ (c >= 97 && c <= 122) /** a..z */ ||
3031
+ (c >= 65 && c <= 90) /* A..Z */ ||
3032
+ c == 0xb7 ||
3033
+ (c >= 0xc0 && c <= 0xd6) ||
3034
+ (c >= 0xd8 && c <= 0xf6) ||
3035
+ (c >= 0xf8 && c <= 0x37d) ||
3036
+ (c >= 0x37f && c <= 0x1fff) ||
3037
+ (c >= 0x200c && c <= 0x200d) ||
3038
+ (c >= 0x203f && c <= 0x2040) ||
3039
+ (c >= 0x2070 && c <= 0x218f) ||
3040
+ (c >= 0x2c00 && c <= 0x2fef) ||
3041
+ (c >= 0x3001 && c <= 0xd7ff) ||
3042
+ (c >= 0xf900 && c <= 0xfdcf) ||
3043
+ (c >= 0xfdf0 && c <= 0xfffd) ||
3044
+ (c >= 0x10000 && c <= 0xeffff));
3045
+ }
3046
+ /**
3047
+ * Code point equivalent of regex `\p{White_Space}`.
3048
+ * From: https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
3049
+ */
3050
+ function _isWhiteSpace(c) {
3051
+ return ((c >= 0x0009 && c <= 0x000d) ||
3052
+ c === 0x0020 ||
3053
+ c === 0x0085 ||
3054
+ (c >= 0x200e && c <= 0x200f) ||
3055
+ c === 0x2028 ||
3056
+ c === 0x2029);
3057
+ }
3058
+ /**
3059
+ * Code point equivalent of regex `\p{Pattern_Syntax}`.
3060
+ * See https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
3061
+ */
3062
+ function _isPatternSyntax(c) {
3063
+ return ((c >= 0x0021 && c <= 0x0023) ||
3064
+ c === 0x0024 ||
3065
+ (c >= 0x0025 && c <= 0x0027) ||
3066
+ c === 0x0028 ||
3067
+ c === 0x0029 ||
3068
+ c === 0x002a ||
3069
+ c === 0x002b ||
3070
+ c === 0x002c ||
3071
+ c === 0x002d ||
3072
+ (c >= 0x002e && c <= 0x002f) ||
3073
+ (c >= 0x003a && c <= 0x003b) ||
3074
+ (c >= 0x003c && c <= 0x003e) ||
3075
+ (c >= 0x003f && c <= 0x0040) ||
3076
+ c === 0x005b ||
3077
+ c === 0x005c ||
3078
+ c === 0x005d ||
3079
+ c === 0x005e ||
3080
+ c === 0x0060 ||
3081
+ c === 0x007b ||
3082
+ c === 0x007c ||
3083
+ c === 0x007d ||
3084
+ c === 0x007e ||
3085
+ c === 0x00a1 ||
3086
+ (c >= 0x00a2 && c <= 0x00a5) ||
3087
+ c === 0x00a6 ||
3088
+ c === 0x00a7 ||
3089
+ c === 0x00a9 ||
3090
+ c === 0x00ab ||
3091
+ c === 0x00ac ||
3092
+ c === 0x00ae ||
3093
+ c === 0x00b0 ||
3094
+ c === 0x00b1 ||
3095
+ c === 0x00b6 ||
3096
+ c === 0x00bb ||
3097
+ c === 0x00bf ||
3098
+ c === 0x00d7 ||
3099
+ c === 0x00f7 ||
3100
+ (c >= 0x2010 && c <= 0x2015) ||
3101
+ (c >= 0x2016 && c <= 0x2017) ||
3102
+ c === 0x2018 ||
3103
+ c === 0x2019 ||
3104
+ c === 0x201a ||
3105
+ (c >= 0x201b && c <= 0x201c) ||
3106
+ c === 0x201d ||
3107
+ c === 0x201e ||
3108
+ c === 0x201f ||
3109
+ (c >= 0x2020 && c <= 0x2027) ||
3110
+ (c >= 0x2030 && c <= 0x2038) ||
3111
+ c === 0x2039 ||
3112
+ c === 0x203a ||
3113
+ (c >= 0x203b && c <= 0x203e) ||
3114
+ (c >= 0x2041 && c <= 0x2043) ||
3115
+ c === 0x2044 ||
3116
+ c === 0x2045 ||
3117
+ c === 0x2046 ||
3118
+ (c >= 0x2047 && c <= 0x2051) ||
3119
+ c === 0x2052 ||
3120
+ c === 0x2053 ||
3121
+ (c >= 0x2055 && c <= 0x205e) ||
3122
+ (c >= 0x2190 && c <= 0x2194) ||
3123
+ (c >= 0x2195 && c <= 0x2199) ||
3124
+ (c >= 0x219a && c <= 0x219b) ||
3125
+ (c >= 0x219c && c <= 0x219f) ||
3126
+ c === 0x21a0 ||
3127
+ (c >= 0x21a1 && c <= 0x21a2) ||
3128
+ c === 0x21a3 ||
3129
+ (c >= 0x21a4 && c <= 0x21a5) ||
3130
+ c === 0x21a6 ||
3131
+ (c >= 0x21a7 && c <= 0x21ad) ||
3132
+ c === 0x21ae ||
3133
+ (c >= 0x21af && c <= 0x21cd) ||
3134
+ (c >= 0x21ce && c <= 0x21cf) ||
3135
+ (c >= 0x21d0 && c <= 0x21d1) ||
3136
+ c === 0x21d2 ||
3137
+ c === 0x21d3 ||
3138
+ c === 0x21d4 ||
3139
+ (c >= 0x21d5 && c <= 0x21f3) ||
3140
+ (c >= 0x21f4 && c <= 0x22ff) ||
3141
+ (c >= 0x2300 && c <= 0x2307) ||
3142
+ c === 0x2308 ||
3143
+ c === 0x2309 ||
3144
+ c === 0x230a ||
3145
+ c === 0x230b ||
3146
+ (c >= 0x230c && c <= 0x231f) ||
3147
+ (c >= 0x2320 && c <= 0x2321) ||
3148
+ (c >= 0x2322 && c <= 0x2328) ||
3149
+ c === 0x2329 ||
3150
+ c === 0x232a ||
3151
+ (c >= 0x232b && c <= 0x237b) ||
3152
+ c === 0x237c ||
3153
+ (c >= 0x237d && c <= 0x239a) ||
3154
+ (c >= 0x239b && c <= 0x23b3) ||
3155
+ (c >= 0x23b4 && c <= 0x23db) ||
3156
+ (c >= 0x23dc && c <= 0x23e1) ||
3157
+ (c >= 0x23e2 && c <= 0x2426) ||
3158
+ (c >= 0x2427 && c <= 0x243f) ||
3159
+ (c >= 0x2440 && c <= 0x244a) ||
3160
+ (c >= 0x244b && c <= 0x245f) ||
3161
+ (c >= 0x2500 && c <= 0x25b6) ||
3162
+ c === 0x25b7 ||
3163
+ (c >= 0x25b8 && c <= 0x25c0) ||
3164
+ c === 0x25c1 ||
3165
+ (c >= 0x25c2 && c <= 0x25f7) ||
3166
+ (c >= 0x25f8 && c <= 0x25ff) ||
3167
+ (c >= 0x2600 && c <= 0x266e) ||
3168
+ c === 0x266f ||
3169
+ (c >= 0x2670 && c <= 0x2767) ||
3170
+ c === 0x2768 ||
3171
+ c === 0x2769 ||
3172
+ c === 0x276a ||
3173
+ c === 0x276b ||
3174
+ c === 0x276c ||
3175
+ c === 0x276d ||
3176
+ c === 0x276e ||
3177
+ c === 0x276f ||
3178
+ c === 0x2770 ||
3179
+ c === 0x2771 ||
3180
+ c === 0x2772 ||
3181
+ c === 0x2773 ||
3182
+ c === 0x2774 ||
3183
+ c === 0x2775 ||
3184
+ (c >= 0x2794 && c <= 0x27bf) ||
3185
+ (c >= 0x27c0 && c <= 0x27c4) ||
3186
+ c === 0x27c5 ||
3187
+ c === 0x27c6 ||
3188
+ (c >= 0x27c7 && c <= 0x27e5) ||
3189
+ c === 0x27e6 ||
3190
+ c === 0x27e7 ||
3191
+ c === 0x27e8 ||
3192
+ c === 0x27e9 ||
3193
+ c === 0x27ea ||
3194
+ c === 0x27eb ||
3195
+ c === 0x27ec ||
3196
+ c === 0x27ed ||
3197
+ c === 0x27ee ||
3198
+ c === 0x27ef ||
3199
+ (c >= 0x27f0 && c <= 0x27ff) ||
3200
+ (c >= 0x2800 && c <= 0x28ff) ||
3201
+ (c >= 0x2900 && c <= 0x2982) ||
3202
+ c === 0x2983 ||
3203
+ c === 0x2984 ||
3204
+ c === 0x2985 ||
3205
+ c === 0x2986 ||
3206
+ c === 0x2987 ||
3207
+ c === 0x2988 ||
3208
+ c === 0x2989 ||
3209
+ c === 0x298a ||
3210
+ c === 0x298b ||
3211
+ c === 0x298c ||
3212
+ c === 0x298d ||
3213
+ c === 0x298e ||
3214
+ c === 0x298f ||
3215
+ c === 0x2990 ||
3216
+ c === 0x2991 ||
3217
+ c === 0x2992 ||
3218
+ c === 0x2993 ||
3219
+ c === 0x2994 ||
3220
+ c === 0x2995 ||
3221
+ c === 0x2996 ||
3222
+ c === 0x2997 ||
3223
+ c === 0x2998 ||
3224
+ (c >= 0x2999 && c <= 0x29d7) ||
3225
+ c === 0x29d8 ||
3226
+ c === 0x29d9 ||
3227
+ c === 0x29da ||
3228
+ c === 0x29db ||
3229
+ (c >= 0x29dc && c <= 0x29fb) ||
3230
+ c === 0x29fc ||
3231
+ c === 0x29fd ||
3232
+ (c >= 0x29fe && c <= 0x2aff) ||
3233
+ (c >= 0x2b00 && c <= 0x2b2f) ||
3234
+ (c >= 0x2b30 && c <= 0x2b44) ||
3235
+ (c >= 0x2b45 && c <= 0x2b46) ||
3236
+ (c >= 0x2b47 && c <= 0x2b4c) ||
3237
+ (c >= 0x2b4d && c <= 0x2b73) ||
3238
+ (c >= 0x2b74 && c <= 0x2b75) ||
3239
+ (c >= 0x2b76 && c <= 0x2b95) ||
3240
+ c === 0x2b96 ||
3241
+ (c >= 0x2b97 && c <= 0x2bff) ||
3242
+ (c >= 0x2e00 && c <= 0x2e01) ||
3243
+ c === 0x2e02 ||
3244
+ c === 0x2e03 ||
3245
+ c === 0x2e04 ||
3246
+ c === 0x2e05 ||
3247
+ (c >= 0x2e06 && c <= 0x2e08) ||
3248
+ c === 0x2e09 ||
3249
+ c === 0x2e0a ||
3250
+ c === 0x2e0b ||
3251
+ c === 0x2e0c ||
3252
+ c === 0x2e0d ||
3253
+ (c >= 0x2e0e && c <= 0x2e16) ||
3254
+ c === 0x2e17 ||
3255
+ (c >= 0x2e18 && c <= 0x2e19) ||
3256
+ c === 0x2e1a ||
3257
+ c === 0x2e1b ||
3258
+ c === 0x2e1c ||
3259
+ c === 0x2e1d ||
3260
+ (c >= 0x2e1e && c <= 0x2e1f) ||
3261
+ c === 0x2e20 ||
3262
+ c === 0x2e21 ||
3263
+ c === 0x2e22 ||
3264
+ c === 0x2e23 ||
3265
+ c === 0x2e24 ||
3266
+ c === 0x2e25 ||
3267
+ c === 0x2e26 ||
3268
+ c === 0x2e27 ||
3269
+ c === 0x2e28 ||
3270
+ c === 0x2e29 ||
3271
+ (c >= 0x2e2a && c <= 0x2e2e) ||
3272
+ c === 0x2e2f ||
3273
+ (c >= 0x2e30 && c <= 0x2e39) ||
3274
+ (c >= 0x2e3a && c <= 0x2e3b) ||
3275
+ (c >= 0x2e3c && c <= 0x2e3f) ||
3276
+ c === 0x2e40 ||
3277
+ c === 0x2e41 ||
3278
+ c === 0x2e42 ||
3279
+ (c >= 0x2e43 && c <= 0x2e4f) ||
3280
+ (c >= 0x2e50 && c <= 0x2e51) ||
3281
+ c === 0x2e52 ||
3282
+ (c >= 0x2e53 && c <= 0x2e7f) ||
3283
+ (c >= 0x3001 && c <= 0x3003) ||
3284
+ c === 0x3008 ||
3285
+ c === 0x3009 ||
3286
+ c === 0x300a ||
3287
+ c === 0x300b ||
3288
+ c === 0x300c ||
3289
+ c === 0x300d ||
3290
+ c === 0x300e ||
3291
+ c === 0x300f ||
3292
+ c === 0x3010 ||
3293
+ c === 0x3011 ||
3294
+ (c >= 0x3012 && c <= 0x3013) ||
3295
+ c === 0x3014 ||
3296
+ c === 0x3015 ||
3297
+ c === 0x3016 ||
3298
+ c === 0x3017 ||
3299
+ c === 0x3018 ||
3300
+ c === 0x3019 ||
3301
+ c === 0x301a ||
3302
+ c === 0x301b ||
3303
+ c === 0x301c ||
3304
+ c === 0x301d ||
3305
+ (c >= 0x301e && c <= 0x301f) ||
3306
+ c === 0x3020 ||
3307
+ c === 0x3030 ||
3308
+ c === 0xfd3e ||
3309
+ c === 0xfd3f ||
3310
+ (c >= 0xfe45 && c <= 0xfe46));
3311
+ }
3312
+
3313
+ function pruneLocation(els) {
3314
+ els.forEach(function (el) {
3315
+ delete el.location;
3316
+ if (isSelectElement(el) || isPluralElement(el)) {
3317
+ for (var k in el.options) {
3318
+ delete el.options[k].location;
3319
+ pruneLocation(el.options[k].value);
3320
+ }
3321
+ }
3322
+ else if (isNumberElement(el) && isNumberSkeleton(el.style)) {
3323
+ delete el.style.location;
3324
+ }
3325
+ else if ((isDateElement(el) || isTimeElement(el)) &&
3326
+ isDateTimeSkeleton(el.style)) {
3327
+ delete el.style.location;
3328
+ }
3329
+ else if (isTagElement(el)) {
3330
+ pruneLocation(el.children);
3331
+ }
3332
+ });
3333
+ }
3334
+ function parse(message, opts) {
3335
+ if (opts === void 0) { opts = {}; }
3336
+ opts = __assign({ shouldParseSkeletons: true, requiresOtherClause: true }, opts);
3337
+ var result = new Parser(message, opts).parse();
3338
+ if (result.err) {
3339
+ var error = SyntaxError(ErrorKind[result.err.kind]);
3340
+ // @ts-expect-error Assign to error object
3341
+ error.location = result.err.location;
3342
+ // @ts-expect-error Assign to error object
3343
+ error.originalMessage = result.err.message;
3344
+ throw error;
3345
+ }
3346
+ if (!(opts === null || opts === void 0 ? void 0 : opts.captureLocation)) {
3347
+ pruneLocation(result.val);
3348
+ }
3349
+ return result.val;
3350
+ }
3351
+
3352
+ //
3353
+ // Main
3354
+ //
3355
+ function memoize(fn, options) {
3356
+ var cache = options && options.cache ? options.cache : cacheDefault;
3357
+ var serializer = options && options.serializer ? options.serializer : serializerDefault;
3358
+ var strategy = options && options.strategy ? options.strategy : strategyDefault;
3359
+ return strategy(fn, {
3360
+ cache: cache,
3361
+ serializer: serializer,
3362
+ });
3363
+ }
3364
+ //
3365
+ // Strategy
3366
+ //
3367
+ function isPrimitive(value) {
3368
+ return (value == null || typeof value === 'number' || typeof value === 'boolean'); // || typeof value === "string" 'unsafe' primitive for our needs
3369
+ }
3370
+ function monadic(fn, cache, serializer, arg) {
3371
+ var cacheKey = isPrimitive(arg) ? arg : serializer(arg);
3372
+ var computedValue = cache.get(cacheKey);
3373
+ if (typeof computedValue === 'undefined') {
3374
+ computedValue = fn.call(this, arg);
3375
+ cache.set(cacheKey, computedValue);
3376
+ }
3377
+ return computedValue;
3378
+ }
3379
+ function variadic(fn, cache, serializer) {
3380
+ var args = Array.prototype.slice.call(arguments, 3);
3381
+ var cacheKey = serializer(args);
3382
+ var computedValue = cache.get(cacheKey);
3383
+ if (typeof computedValue === 'undefined') {
3384
+ computedValue = fn.apply(this, args);
3385
+ cache.set(cacheKey, computedValue);
3386
+ }
3387
+ return computedValue;
3388
+ }
3389
+ function assemble(fn, context, strategy, cache, serialize) {
3390
+ return strategy.bind(context, fn, cache, serialize);
3391
+ }
3392
+ function strategyDefault(fn, options) {
3393
+ var strategy = fn.length === 1 ? monadic : variadic;
3394
+ return assemble(fn, this, strategy, options.cache.create(), options.serializer);
3395
+ }
3396
+ function strategyVariadic(fn, options) {
3397
+ return assemble(fn, this, variadic, options.cache.create(), options.serializer);
3398
+ }
3399
+ function strategyMonadic(fn, options) {
3400
+ return assemble(fn, this, monadic, options.cache.create(), options.serializer);
3401
+ }
3402
+ //
3403
+ // Serializer
3404
+ //
3405
+ var serializerDefault = function () {
3406
+ return JSON.stringify(arguments);
3407
+ };
3408
+ //
3409
+ // Cache
3410
+ //
3411
+ function ObjectWithoutPrototypeCache() {
3412
+ this.cache = Object.create(null);
3413
+ }
3414
+ ObjectWithoutPrototypeCache.prototype.get = function (key) {
3415
+ return this.cache[key];
3416
+ };
3417
+ ObjectWithoutPrototypeCache.prototype.set = function (key, value) {
3418
+ this.cache[key] = value;
3419
+ };
3420
+ var cacheDefault = {
3421
+ create: function create() {
3422
+ // @ts-ignore
3423
+ return new ObjectWithoutPrototypeCache();
3424
+ },
3425
+ };
3426
+ var strategies = {
3427
+ variadic: strategyVariadic,
3428
+ monadic: strategyMonadic,
3429
+ };
3430
+
3431
+ var ErrorCode;
3432
+ (function (ErrorCode) {
3433
+ // When we have a placeholder but no value to format
3434
+ ErrorCode["MISSING_VALUE"] = "MISSING_VALUE";
3435
+ // When value supplied is invalid
3436
+ ErrorCode["INVALID_VALUE"] = "INVALID_VALUE";
3437
+ // When we need specific Intl API but it's not available
3438
+ ErrorCode["MISSING_INTL_API"] = "MISSING_INTL_API";
3439
+ })(ErrorCode || (ErrorCode = {}));
3440
+ var FormatError = /** @class */ (function (_super) {
3441
+ __extends(FormatError, _super);
3442
+ function FormatError(msg, code, originalMessage) {
3443
+ var _this = _super.call(this, msg) || this;
3444
+ _this.code = code;
3445
+ _this.originalMessage = originalMessage;
3446
+ return _this;
3447
+ }
3448
+ FormatError.prototype.toString = function () {
3449
+ return "[formatjs Error: ".concat(this.code, "] ").concat(this.message);
3450
+ };
3451
+ return FormatError;
3452
+ }(Error));
3453
+ var InvalidValueError = /** @class */ (function (_super) {
3454
+ __extends(InvalidValueError, _super);
3455
+ function InvalidValueError(variableId, value, options, originalMessage) {
3456
+ return _super.call(this, "Invalid values for \"".concat(variableId, "\": \"").concat(value, "\". Options are \"").concat(Object.keys(options).join('", "'), "\""), ErrorCode.INVALID_VALUE, originalMessage) || this;
3457
+ }
3458
+ return InvalidValueError;
3459
+ }(FormatError));
3460
+ var InvalidValueTypeError = /** @class */ (function (_super) {
3461
+ __extends(InvalidValueTypeError, _super);
3462
+ function InvalidValueTypeError(value, type, originalMessage) {
3463
+ return _super.call(this, "Value for \"".concat(value, "\" must be of type ").concat(type), ErrorCode.INVALID_VALUE, originalMessage) || this;
3464
+ }
3465
+ return InvalidValueTypeError;
3466
+ }(FormatError));
3467
+ var MissingValueError = /** @class */ (function (_super) {
3468
+ __extends(MissingValueError, _super);
3469
+ function MissingValueError(variableId, originalMessage) {
3470
+ return _super.call(this, "The intl string context variable \"".concat(variableId, "\" was not provided to the string \"").concat(originalMessage, "\""), ErrorCode.MISSING_VALUE, originalMessage) || this;
3471
+ }
3472
+ return MissingValueError;
3473
+ }(FormatError));
3474
+
3475
+ var PART_TYPE;
3476
+ (function (PART_TYPE) {
3477
+ PART_TYPE[PART_TYPE["literal"] = 0] = "literal";
3478
+ PART_TYPE[PART_TYPE["object"] = 1] = "object";
3479
+ })(PART_TYPE || (PART_TYPE = {}));
3480
+ function mergeLiteral(parts) {
3481
+ if (parts.length < 2) {
3482
+ return parts;
3483
+ }
3484
+ return parts.reduce(function (all, part) {
3485
+ var lastPart = all[all.length - 1];
3486
+ if (!lastPart ||
3487
+ lastPart.type !== PART_TYPE.literal ||
3488
+ part.type !== PART_TYPE.literal) {
3489
+ all.push(part);
3490
+ }
3491
+ else {
3492
+ lastPart.value += part.value;
3493
+ }
3494
+ return all;
3495
+ }, []);
3496
+ }
3497
+ function isFormatXMLElementFn(el) {
3498
+ return typeof el === 'function';
3499
+ }
3500
+ // TODO(skeleton): add skeleton support
3501
+ function formatToParts(els, locales, formatters, formats, values, currentPluralValue,
3502
+ // For debugging
3503
+ originalMessage) {
3504
+ // Hot path for straight simple msg translations
3505
+ if (els.length === 1 && isLiteralElement(els[0])) {
3506
+ return [
3507
+ {
3508
+ type: PART_TYPE.literal,
3509
+ value: els[0].value,
3510
+ },
3511
+ ];
3512
+ }
3513
+ var result = [];
3514
+ for (var _i = 0, els_1 = els; _i < els_1.length; _i++) {
3515
+ var el = els_1[_i];
3516
+ // Exit early for string parts.
3517
+ if (isLiteralElement(el)) {
3518
+ result.push({
3519
+ type: PART_TYPE.literal,
3520
+ value: el.value,
3521
+ });
3522
+ continue;
3523
+ }
3524
+ // TODO: should this part be literal type?
3525
+ // Replace `#` in plural rules with the actual numeric value.
3526
+ if (isPoundElement(el)) {
3527
+ if (typeof currentPluralValue === 'number') {
3528
+ result.push({
3529
+ type: PART_TYPE.literal,
3530
+ value: formatters.getNumberFormat(locales).format(currentPluralValue),
3531
+ });
3532
+ }
3533
+ continue;
3534
+ }
3535
+ var varName = el.value;
3536
+ // Enforce that all required values are provided by the caller.
3537
+ if (!(values && varName in values)) {
3538
+ throw new MissingValueError(varName, originalMessage);
3539
+ }
3540
+ var value = values[varName];
3541
+ if (isArgumentElement(el)) {
3542
+ if (!value || typeof value === 'string' || typeof value === 'number') {
3543
+ value =
3544
+ typeof value === 'string' || typeof value === 'number'
3545
+ ? String(value)
3546
+ : '';
3547
+ }
3548
+ result.push({
3549
+ type: typeof value === 'string' ? PART_TYPE.literal : PART_TYPE.object,
3550
+ value: value,
3551
+ });
3552
+ continue;
3553
+ }
3554
+ // Recursively format plural and select parts' option — which can be a
3555
+ // nested pattern structure. The choosing of the option to use is
3556
+ // abstracted-by and delegated-to the part helper object.
3557
+ if (isDateElement(el)) {
3558
+ var style = typeof el.style === 'string'
3559
+ ? formats.date[el.style]
3560
+ : isDateTimeSkeleton(el.style)
3561
+ ? el.style.parsedOptions
3562
+ : undefined;
3563
+ result.push({
3564
+ type: PART_TYPE.literal,
3565
+ value: formatters
3566
+ .getDateTimeFormat(locales, style)
3567
+ .format(value),
3568
+ });
3569
+ continue;
3570
+ }
3571
+ if (isTimeElement(el)) {
3572
+ var style = typeof el.style === 'string'
3573
+ ? formats.time[el.style]
3574
+ : isDateTimeSkeleton(el.style)
3575
+ ? el.style.parsedOptions
3576
+ : formats.time.medium;
3577
+ result.push({
3578
+ type: PART_TYPE.literal,
3579
+ value: formatters
3580
+ .getDateTimeFormat(locales, style)
3581
+ .format(value),
3582
+ });
3583
+ continue;
3584
+ }
3585
+ if (isNumberElement(el)) {
3586
+ var style = typeof el.style === 'string'
3587
+ ? formats.number[el.style]
3588
+ : isNumberSkeleton(el.style)
3589
+ ? el.style.parsedOptions
3590
+ : undefined;
3591
+ if (style && style.scale) {
3592
+ value =
3593
+ value *
3594
+ (style.scale || 1);
3595
+ }
3596
+ result.push({
3597
+ type: PART_TYPE.literal,
3598
+ value: formatters
3599
+ .getNumberFormat(locales, style)
3600
+ .format(value),
3601
+ });
3602
+ continue;
3603
+ }
3604
+ if (isTagElement(el)) {
3605
+ var children = el.children, value_1 = el.value;
3606
+ var formatFn = values[value_1];
3607
+ if (!isFormatXMLElementFn(formatFn)) {
3608
+ throw new InvalidValueTypeError(value_1, 'function', originalMessage);
3609
+ }
3610
+ var parts = formatToParts(children, locales, formatters, formats, values, currentPluralValue);
3611
+ var chunks = formatFn(parts.map(function (p) { return p.value; }));
3612
+ if (!Array.isArray(chunks)) {
3613
+ chunks = [chunks];
3614
+ }
3615
+ result.push.apply(result, chunks.map(function (c) {
3616
+ return {
3617
+ type: typeof c === 'string' ? PART_TYPE.literal : PART_TYPE.object,
3618
+ value: c,
3619
+ };
3620
+ }));
3621
+ }
3622
+ if (isSelectElement(el)) {
3623
+ var opt = el.options[value] || el.options.other;
3624
+ if (!opt) {
3625
+ throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
3626
+ }
3627
+ result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values));
3628
+ continue;
3629
+ }
3630
+ if (isPluralElement(el)) {
3631
+ var opt = el.options["=".concat(value)];
3632
+ if (!opt) {
3633
+ if (!Intl.PluralRules) {
3634
+ throw new FormatError("Intl.PluralRules is not available in this environment.\nTry polyfilling it using \"@formatjs/intl-pluralrules\"\n", ErrorCode.MISSING_INTL_API, originalMessage);
3635
+ }
3636
+ var rule = formatters
3637
+ .getPluralRules(locales, { type: el.pluralType })
3638
+ .select(value - (el.offset || 0));
3639
+ opt = el.options[rule] || el.options.other;
3640
+ }
3641
+ if (!opt) {
3642
+ throw new InvalidValueError(el.value, value, Object.keys(el.options), originalMessage);
3643
+ }
3644
+ result.push.apply(result, formatToParts(opt.value, locales, formatters, formats, values, value - (el.offset || 0)));
3645
+ continue;
3646
+ }
3647
+ }
3648
+ return mergeLiteral(result);
3649
+ }
3650
+
3651
+ /*
3652
+ Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3653
+ Copyrights licensed under the New BSD License.
3654
+ See the accompanying LICENSE file for terms.
3655
+ */
3656
+ // -- MessageFormat --------------------------------------------------------
3657
+ function mergeConfig(c1, c2) {
3658
+ if (!c2) {
3659
+ return c1;
3660
+ }
3661
+ return __assign(__assign(__assign({}, (c1 || {})), (c2 || {})), Object.keys(c1).reduce(function (all, k) {
3662
+ all[k] = __assign(__assign({}, c1[k]), (c2[k] || {}));
3663
+ return all;
3664
+ }, {}));
3665
+ }
3666
+ function mergeConfigs(defaultConfig, configs) {
3667
+ if (!configs) {
3668
+ return defaultConfig;
3669
+ }
3670
+ return Object.keys(defaultConfig).reduce(function (all, k) {
3671
+ all[k] = mergeConfig(defaultConfig[k], configs[k]);
3672
+ return all;
3673
+ }, __assign({}, defaultConfig));
3674
+ }
3675
+ function createFastMemoizeCache(store) {
3676
+ return {
3677
+ create: function () {
3678
+ return {
3679
+ get: function (key) {
3680
+ return store[key];
3681
+ },
3682
+ set: function (key, value) {
3683
+ store[key] = value;
3684
+ },
3685
+ };
3686
+ },
3687
+ };
3688
+ }
3689
+ function createDefaultFormatters(cache) {
3690
+ if (cache === void 0) { cache = {
3691
+ number: {},
3692
+ dateTime: {},
3693
+ pluralRules: {},
3694
+ }; }
3695
+ return {
3696
+ getNumberFormat: memoize(function () {
3697
+ var _a;
3698
+ var args = [];
3699
+ for (var _i = 0; _i < arguments.length; _i++) {
3700
+ args[_i] = arguments[_i];
3701
+ }
3702
+ return new ((_a = Intl.NumberFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
3703
+ }, {
3704
+ cache: createFastMemoizeCache(cache.number),
3705
+ strategy: strategies.variadic,
3706
+ }),
3707
+ getDateTimeFormat: memoize(function () {
3708
+ var _a;
3709
+ var args = [];
3710
+ for (var _i = 0; _i < arguments.length; _i++) {
3711
+ args[_i] = arguments[_i];
3712
+ }
3713
+ return new ((_a = Intl.DateTimeFormat).bind.apply(_a, __spreadArray([void 0], args, false)))();
3714
+ }, {
3715
+ cache: createFastMemoizeCache(cache.dateTime),
3716
+ strategy: strategies.variadic,
3717
+ }),
3718
+ getPluralRules: memoize(function () {
3719
+ var _a;
3720
+ var args = [];
3721
+ for (var _i = 0; _i < arguments.length; _i++) {
3722
+ args[_i] = arguments[_i];
3723
+ }
3724
+ return new ((_a = Intl.PluralRules).bind.apply(_a, __spreadArray([void 0], args, false)))();
3725
+ }, {
3726
+ cache: createFastMemoizeCache(cache.pluralRules),
3727
+ strategy: strategies.variadic,
3728
+ }),
3729
+ };
3730
+ }
3731
+ var IntlMessageFormat$1 = /** @class */ (function () {
3732
+ function IntlMessageFormat(message, locales, overrideFormats, opts) {
3733
+ var _this = this;
3734
+ if (locales === void 0) { locales = IntlMessageFormat.defaultLocale; }
3735
+ this.formatterCache = {
3736
+ number: {},
3737
+ dateTime: {},
3738
+ pluralRules: {},
3739
+ };
3740
+ this.format = function (values) {
3741
+ var parts = _this.formatToParts(values);
3742
+ // Hot path for straight simple msg translations
3743
+ if (parts.length === 1) {
3744
+ return parts[0].value;
3745
+ }
3746
+ var result = parts.reduce(function (all, part) {
3747
+ if (!all.length ||
3748
+ part.type !== PART_TYPE.literal ||
3749
+ typeof all[all.length - 1] !== 'string') {
3750
+ all.push(part.value);
3751
+ }
3752
+ else {
3753
+ all[all.length - 1] += part.value;
3754
+ }
3755
+ return all;
3756
+ }, []);
3757
+ if (result.length <= 1) {
3758
+ return result[0] || '';
3759
+ }
3760
+ return result;
3761
+ };
3762
+ this.formatToParts = function (values) {
3763
+ return formatToParts(_this.ast, _this.locales, _this.formatters, _this.formats, values, undefined, _this.message);
3764
+ };
3765
+ this.resolvedOptions = function () { return ({
3766
+ locale: _this.resolvedLocale.toString(),
3767
+ }); };
3768
+ this.getAst = function () { return _this.ast; };
3769
+ // Defined first because it's used to build the format pattern.
3770
+ this.locales = locales;
3771
+ this.resolvedLocale = IntlMessageFormat.resolveLocale(locales);
3772
+ if (typeof message === 'string') {
3773
+ this.message = message;
3774
+ if (!IntlMessageFormat.__parse) {
3775
+ throw new TypeError('IntlMessageFormat.__parse must be set to process `message` of type `string`');
3776
+ }
3777
+ // Parse string messages into an AST.
3778
+ this.ast = IntlMessageFormat.__parse(message, {
3779
+ ignoreTag: opts === null || opts === void 0 ? void 0 : opts.ignoreTag,
3780
+ locale: this.resolvedLocale,
3781
+ });
3782
+ }
3783
+ else {
3784
+ this.ast = message;
3785
+ }
3786
+ if (!Array.isArray(this.ast)) {
3787
+ throw new TypeError('A message must be provided as a String or AST.');
3788
+ }
3789
+ // Creates a new object with the specified `formats` merged with the default
3790
+ // formats.
3791
+ this.formats = mergeConfigs(IntlMessageFormat.formats, overrideFormats);
3792
+ this.formatters =
3793
+ (opts && opts.formatters) || createDefaultFormatters(this.formatterCache);
3794
+ }
3795
+ Object.defineProperty(IntlMessageFormat, "defaultLocale", {
3796
+ get: function () {
3797
+ if (!IntlMessageFormat.memoizedDefaultLocale) {
3798
+ IntlMessageFormat.memoizedDefaultLocale =
3799
+ new Intl.NumberFormat().resolvedOptions().locale;
3800
+ }
3801
+ return IntlMessageFormat.memoizedDefaultLocale;
3802
+ },
3803
+ enumerable: false,
3804
+ configurable: true
3805
+ });
3806
+ IntlMessageFormat.memoizedDefaultLocale = null;
3807
+ IntlMessageFormat.resolveLocale = function (locales) {
3808
+ var supportedLocales = Intl.NumberFormat.supportedLocalesOf(locales);
3809
+ if (supportedLocales.length > 0) {
3810
+ return new Intl.Locale(supportedLocales[0]);
3811
+ }
3812
+ return new Intl.Locale(typeof locales === 'string' ? locales : locales[0]);
3813
+ };
3814
+ IntlMessageFormat.__parse = parse;
3815
+ // Default format options used as the prototype of the `formats` provided to the
3816
+ // constructor. These are used when constructing the internal Intl.NumberFormat
3817
+ // and Intl.DateTimeFormat instances.
3818
+ IntlMessageFormat.formats = {
3819
+ number: {
3820
+ integer: {
3821
+ maximumFractionDigits: 0,
3822
+ },
3823
+ currency: {
3824
+ style: 'currency',
3825
+ },
3826
+ percent: {
3827
+ style: 'percent',
3828
+ },
3829
+ },
3830
+ date: {
3831
+ short: {
3832
+ month: 'numeric',
3833
+ day: 'numeric',
3834
+ year: '2-digit',
3835
+ },
3836
+ medium: {
3837
+ month: 'short',
3838
+ day: 'numeric',
3839
+ year: 'numeric',
3840
+ },
3841
+ long: {
3842
+ month: 'long',
3843
+ day: 'numeric',
3844
+ year: 'numeric',
3845
+ },
3846
+ full: {
3847
+ weekday: 'long',
3848
+ month: 'long',
3849
+ day: 'numeric',
3850
+ year: 'numeric',
3851
+ },
3852
+ },
3853
+ time: {
3854
+ short: {
3855
+ hour: 'numeric',
3856
+ minute: 'numeric',
3857
+ },
3858
+ medium: {
3859
+ hour: 'numeric',
3860
+ minute: 'numeric',
3861
+ second: 'numeric',
3862
+ },
3863
+ long: {
3864
+ hour: 'numeric',
3865
+ minute: 'numeric',
3866
+ second: 'numeric',
3867
+ timeZoneName: 'short',
3868
+ },
3869
+ full: {
3870
+ hour: 'numeric',
3871
+ minute: 'numeric',
3872
+ second: 'numeric',
3873
+ timeZoneName: 'short',
3874
+ },
3875
+ },
3876
+ };
3877
+ return IntlMessageFormat;
3878
+ }());
3879
+
3880
+ /*
3881
+ Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3882
+ Copyrights licensed under the New BSD License.
3883
+ See the accompanying LICENSE file for terms.
3884
+ */
3885
+ var IntlMessageFormat = IntlMessageFormat$1;
3886
+
3887
+ const FormatIcuCreator = () => {
3888
+ const locales = new Map();
3889
+ function isLocaleValid(locale) {
3890
+ try {
3891
+ return Boolean(Intl.NumberFormat.supportedLocalesOf(locale).length);
3892
+ }
3893
+ catch (_a) {
3894
+ return false;
3895
+ }
3896
+ }
3897
+ function getLocale(language) {
3898
+ if (!locales.get(language)) {
3899
+ let localeCandidate = String(language).replace(/[^a-zA-Z]/g, '-');
3900
+ while (!isLocaleValid(localeCandidate)) {
3901
+ localeCandidate =
3902
+ localeCandidate.split('-').slice(0, -1).join('-') || 'en';
3903
+ }
3904
+ locales.set(language, localeCandidate);
3905
+ }
3906
+ return locales.get(language);
3907
+ }
3908
+ const format = ({ translation, language, params, }) => {
3909
+ const ignoreTag = !Object.values(params || {}).find((p) => typeof p === 'function');
3910
+ const locale = getLocale(language);
3911
+ return new IntlMessageFormat(translation, locale, undefined, {
3912
+ ignoreTag,
3913
+ }).format(params);
3914
+ };
3915
+ return Object.freeze({ getLocale, format });
3916
+ };
3917
+
3918
+ const FormatIcu = () => (tolgee, tools) => {
3919
+ tools.setFinalFormatter(FormatIcuCreator());
3920
+ return tolgee;
3921
+ };
3922
+
3923
+ export { FormatIcu };
3924
+ //# sourceMappingURL=tolgee-format-icu.esm.js.map