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