@formatjs/icu-messageformat-parser 2.11.4 → 3.0.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.
package/parser.js CHANGED
@@ -1,15 +1,12 @@
1
- "use strict";
2
1
  var _a;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.Parser = void 0;
5
- var tslib_1 = require("tslib");
6
- var error_1 = require("./error");
7
- var types_1 = require("./types");
8
- var regex_generated_1 = require("./regex.generated");
9
- var icu_skeleton_parser_1 = require("@formatjs/icu-skeleton-parser");
10
- var date_time_pattern_generator_1 = require("./date-time-pattern-generator");
11
- var SPACE_SEPARATOR_START_REGEX = new RegExp("^".concat(regex_generated_1.SPACE_SEPARATOR_REGEX.source, "*"));
12
- var SPACE_SEPARATOR_END_REGEX = new RegExp("".concat(regex_generated_1.SPACE_SEPARATOR_REGEX.source, "*$"));
2
+ import { __assign } from "tslib";
3
+ import { ErrorKind } from './error.js';
4
+ import { SKELETON_TYPE, TYPE, } from './types.js';
5
+ import { SPACE_SEPARATOR_REGEX } from './regex.generated.js';
6
+ import { parseNumberSkeleton, parseNumberSkeletonFromString, parseDateTimeSkeleton, } from '@formatjs/icu-skeleton-parser';
7
+ import { getBestPattern } from './date-time-pattern-generator.js';
8
+ var SPACE_SEPARATOR_START_REGEX = new RegExp("^".concat(SPACE_SEPARATOR_REGEX.source, "*"));
9
+ var SPACE_SEPARATOR_END_REGEX = new RegExp("".concat(SPACE_SEPARATOR_REGEX.source, "*$"));
13
10
  function createLocation(start, end) {
14
11
  return { start: start, end: end };
15
12
  }
@@ -42,7 +39,7 @@ try {
42
39
  */
43
40
  REGEX_SUPPORTS_U_AND_Y = ((_a = re.exec('a')) === null || _a === void 0 ? void 0 : _a[0]) === 'a';
44
41
  }
45
- catch (_) {
42
+ catch (_b) {
46
43
  REGEX_SUPPORTS_U_AND_Y = false;
47
44
  }
48
45
  var startsWith = hasNativeStartsWith
@@ -195,7 +192,7 @@ var Parser = /** @class */ (function () {
195
192
  var position = this.clonePosition();
196
193
  this.bump();
197
194
  elements.push({
198
- type: types_1.TYPE.pound,
195
+ type: TYPE.pound,
199
196
  location: createLocation(position, this.clonePosition()),
200
197
  });
201
198
  }
@@ -207,7 +204,7 @@ var Parser = /** @class */ (function () {
207
204
  break;
208
205
  }
209
206
  else {
210
- return this.error(error_1.ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(this.clonePosition(), this.clonePosition()));
207
+ return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(this.clonePosition(), this.clonePosition()));
211
208
  }
212
209
  }
213
210
  else if (char === 60 /* `<` */ &&
@@ -256,7 +253,7 @@ var Parser = /** @class */ (function () {
256
253
  // Self closing tag
257
254
  return {
258
255
  val: {
259
- type: types_1.TYPE.literal,
256
+ type: TYPE.literal,
260
257
  value: "<".concat(tagName, "/>"),
261
258
  location: createLocation(startPosition, this.clonePosition()),
262
259
  },
@@ -273,20 +270,20 @@ var Parser = /** @class */ (function () {
273
270
  var endTagStartPosition = this.clonePosition();
274
271
  if (this.bumpIf('</')) {
275
272
  if (this.isEOF() || !_isAlpha(this.char())) {
276
- return this.error(error_1.ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
273
+ return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
277
274
  }
278
275
  var closingTagNameStartPosition = this.clonePosition();
279
276
  var closingTagName = this.parseTagName();
280
277
  if (tagName !== closingTagName) {
281
- return this.error(error_1.ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(closingTagNameStartPosition, this.clonePosition()));
278
+ return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(closingTagNameStartPosition, this.clonePosition()));
282
279
  }
283
280
  this.bumpSpace();
284
281
  if (!this.bumpIf('>')) {
285
- return this.error(error_1.ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
282
+ return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
286
283
  }
287
284
  return {
288
285
  val: {
289
- type: types_1.TYPE.tag,
286
+ type: TYPE.tag,
290
287
  value: tagName,
291
288
  children: children,
292
289
  location: createLocation(startPosition, this.clonePosition()),
@@ -295,11 +292,11 @@ var Parser = /** @class */ (function () {
295
292
  };
296
293
  }
297
294
  else {
298
- return this.error(error_1.ErrorKind.UNCLOSED_TAG, createLocation(startPosition, this.clonePosition()));
295
+ return this.error(ErrorKind.UNCLOSED_TAG, createLocation(startPosition, this.clonePosition()));
299
296
  }
300
297
  }
301
298
  else {
302
- return this.error(error_1.ErrorKind.INVALID_TAG, createLocation(startPosition, this.clonePosition()));
299
+ return this.error(ErrorKind.INVALID_TAG, createLocation(startPosition, this.clonePosition()));
303
300
  }
304
301
  };
305
302
  /**
@@ -336,7 +333,7 @@ var Parser = /** @class */ (function () {
336
333
  }
337
334
  var location = createLocation(start, this.clonePosition());
338
335
  return {
339
- val: { type: types_1.TYPE.literal, value: value, location: location },
336
+ val: { type: TYPE.literal, value: value, location: location },
340
337
  err: null,
341
338
  };
342
339
  };
@@ -429,20 +426,20 @@ var Parser = /** @class */ (function () {
429
426
  this.bump(); // `{`
430
427
  this.bumpSpace();
431
428
  if (this.isEOF()) {
432
- return this.error(error_1.ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
429
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
433
430
  }
434
431
  if (this.char() === 125 /* `}` */) {
435
432
  this.bump();
436
- return this.error(error_1.ErrorKind.EMPTY_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
433
+ return this.error(ErrorKind.EMPTY_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
437
434
  }
438
435
  // argument name
439
436
  var value = this.parseIdentifierIfPossible().value;
440
437
  if (!value) {
441
- return this.error(error_1.ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
438
+ return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
442
439
  }
443
440
  this.bumpSpace();
444
441
  if (this.isEOF()) {
445
- return this.error(error_1.ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
442
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
446
443
  }
447
444
  switch (this.char()) {
448
445
  // Simple argument: `{name}`
@@ -450,7 +447,7 @@ var Parser = /** @class */ (function () {
450
447
  this.bump(); // `}`
451
448
  return {
452
449
  val: {
453
- type: types_1.TYPE.argument,
450
+ type: TYPE.argument,
454
451
  // value does not include the opening and closing braces.
455
452
  value: value,
456
453
  location: createLocation(openingBracePosition, this.clonePosition()),
@@ -463,12 +460,12 @@ var Parser = /** @class */ (function () {
463
460
  this.bump(); // `,`
464
461
  this.bumpSpace();
465
462
  if (this.isEOF()) {
466
- return this.error(error_1.ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
463
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
467
464
  }
468
465
  return this.parseArgumentOptions(nestingLevel, expectingCloseTag, value, openingBracePosition);
469
466
  }
470
467
  default:
471
- return this.error(error_1.ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
468
+ return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
472
469
  }
473
470
  };
474
471
  /**
@@ -496,7 +493,7 @@ var Parser = /** @class */ (function () {
496
493
  switch (argType) {
497
494
  case '':
498
495
  // Expecting a style string number, date, time, plural, selectordinal, or select.
499
- return this.error(error_1.ErrorKind.EXPECT_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
496
+ return this.error(ErrorKind.EXPECT_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
500
497
  case 'number':
501
498
  case 'date':
502
499
  case 'time': {
@@ -514,7 +511,7 @@ var Parser = /** @class */ (function () {
514
511
  }
515
512
  var style = trimEnd(result.val);
516
513
  if (style.length === 0) {
517
- return this.error(error_1.ErrorKind.EXPECT_ARGUMENT_STYLE, createLocation(this.clonePosition(), this.clonePosition()));
514
+ return this.error(ErrorKind.EXPECT_ARGUMENT_STYLE, createLocation(this.clonePosition(), this.clonePosition()));
518
515
  }
519
516
  var styleLocation = createLocation(styleStartPosition, this.clonePosition());
520
517
  styleAndLocation = { style: style, styleLocation: styleLocation };
@@ -534,30 +531,30 @@ var Parser = /** @class */ (function () {
534
531
  return result;
535
532
  }
536
533
  return {
537
- val: { type: types_1.TYPE.number, value: value, location: location_1, style: result.val },
534
+ val: { type: TYPE.number, value: value, location: location_1, style: result.val },
538
535
  err: null,
539
536
  };
540
537
  }
541
538
  else {
542
539
  if (skeleton.length === 0) {
543
- return this.error(error_1.ErrorKind.EXPECT_DATE_TIME_SKELETON, location_1);
540
+ return this.error(ErrorKind.EXPECT_DATE_TIME_SKELETON, location_1);
544
541
  }
545
542
  var dateTimePattern = skeleton;
546
543
  // Get "best match" pattern only if locale is passed, if not, let it
547
544
  // pass as-is where `parseDateTimeSkeleton()` will throw an error
548
545
  // for unsupported patterns.
549
546
  if (this.locale) {
550
- dateTimePattern = (0, date_time_pattern_generator_1.getBestPattern)(skeleton, this.locale);
547
+ dateTimePattern = getBestPattern(skeleton, this.locale);
551
548
  }
552
549
  var style = {
553
- type: types_1.SKELETON_TYPE.dateTime,
550
+ type: SKELETON_TYPE.dateTime,
554
551
  pattern: dateTimePattern,
555
552
  location: styleAndLocation.styleLocation,
556
553
  parsedOptions: this.shouldParseSkeletons
557
- ? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(dateTimePattern)
554
+ ? parseDateTimeSkeleton(dateTimePattern)
558
555
  : {},
559
556
  };
560
- var type = argType === 'date' ? types_1.TYPE.date : types_1.TYPE.time;
557
+ var type = argType === 'date' ? TYPE.date : TYPE.time;
561
558
  return {
562
559
  val: { type: type, value: value, location: location_1, style: style },
563
560
  err: null,
@@ -568,10 +565,10 @@ var Parser = /** @class */ (function () {
568
565
  return {
569
566
  val: {
570
567
  type: argType === 'number'
571
- ? types_1.TYPE.number
568
+ ? TYPE.number
572
569
  : argType === 'date'
573
- ? types_1.TYPE.date
574
- : types_1.TYPE.time,
570
+ ? TYPE.date
571
+ : TYPE.time,
575
572
  value: value,
576
573
  location: location_1,
577
574
  style: (_a = styleAndLocation === null || styleAndLocation === void 0 ? void 0 : styleAndLocation.style) !== null && _a !== void 0 ? _a : null,
@@ -588,7 +585,7 @@ var Parser = /** @class */ (function () {
588
585
  var typeEndPosition_1 = this.clonePosition();
589
586
  this.bumpSpace();
590
587
  if (!this.bumpIf(',')) {
591
- return this.error(error_1.ErrorKind.EXPECT_SELECT_ARGUMENT_OPTIONS, createLocation(typeEndPosition_1, tslib_1.__assign({}, typeEndPosition_1)));
588
+ return this.error(ErrorKind.EXPECT_SELECT_ARGUMENT_OPTIONS, createLocation(typeEndPosition_1, __assign({}, typeEndPosition_1)));
592
589
  }
593
590
  this.bumpSpace();
594
591
  // Parse offset:
@@ -603,10 +600,10 @@ var Parser = /** @class */ (function () {
603
600
  var pluralOffset = 0;
604
601
  if (argType !== 'select' && identifierAndLocation.value === 'offset') {
605
602
  if (!this.bumpIf(':')) {
606
- return this.error(error_1.ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, createLocation(this.clonePosition(), this.clonePosition()));
603
+ return this.error(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, createLocation(this.clonePosition(), this.clonePosition()));
607
604
  }
608
605
  this.bumpSpace();
609
- var result = this.tryParseDecimalInteger(error_1.ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, error_1.ErrorKind.INVALID_PLURAL_ARGUMENT_OFFSET_VALUE);
606
+ var result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, ErrorKind.INVALID_PLURAL_ARGUMENT_OFFSET_VALUE);
610
607
  if (result.err) {
611
608
  return result;
612
609
  }
@@ -627,7 +624,7 @@ var Parser = /** @class */ (function () {
627
624
  if (argType === 'select') {
628
625
  return {
629
626
  val: {
630
- type: types_1.TYPE.select,
627
+ type: TYPE.select,
631
628
  value: value,
632
629
  options: fromEntries(optionsResult.val),
633
630
  location: location_2,
@@ -638,7 +635,7 @@ var Parser = /** @class */ (function () {
638
635
  else {
639
636
  return {
640
637
  val: {
641
- type: types_1.TYPE.plural,
638
+ type: TYPE.plural,
642
639
  value: value,
643
640
  options: fromEntries(optionsResult.val),
644
641
  offset: pluralOffset,
@@ -650,14 +647,14 @@ var Parser = /** @class */ (function () {
650
647
  }
651
648
  }
652
649
  default:
653
- return this.error(error_1.ErrorKind.INVALID_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
650
+ return this.error(ErrorKind.INVALID_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
654
651
  }
655
652
  };
656
653
  Parser.prototype.tryParseArgumentClose = function (openingBracePosition) {
657
654
  // Parse: {value, number, ::currency/GBP }
658
655
  //
659
656
  if (this.isEOF() || this.char() !== 125 /* `}` */) {
660
- return this.error(error_1.ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
657
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
661
658
  }
662
659
  this.bump(); // `}`
663
660
  return { val: true, err: null };
@@ -677,7 +674,7 @@ var Parser = /** @class */ (function () {
677
674
  this.bump();
678
675
  var apostrophePosition = this.clonePosition();
679
676
  if (!this.bumpUntil("'")) {
680
- return this.error(error_1.ErrorKind.UNCLOSED_QUOTE_IN_ARGUMENT_STYLE, createLocation(apostrophePosition, this.clonePosition()));
677
+ return this.error(ErrorKind.UNCLOSED_QUOTE_IN_ARGUMENT_STYLE, createLocation(apostrophePosition, this.clonePosition()));
681
678
  }
682
679
  this.bump();
683
680
  break;
@@ -712,18 +709,18 @@ var Parser = /** @class */ (function () {
712
709
  Parser.prototype.parseNumberSkeletonFromString = function (skeleton, location) {
713
710
  var tokens = [];
714
711
  try {
715
- tokens = (0, icu_skeleton_parser_1.parseNumberSkeletonFromString)(skeleton);
712
+ tokens = parseNumberSkeletonFromString(skeleton);
716
713
  }
717
- catch (e) {
718
- return this.error(error_1.ErrorKind.INVALID_NUMBER_SKELETON, location);
714
+ catch (_a) {
715
+ return this.error(ErrorKind.INVALID_NUMBER_SKELETON, location);
719
716
  }
720
717
  return {
721
718
  val: {
722
- type: types_1.SKELETON_TYPE.number,
719
+ type: SKELETON_TYPE.number,
723
720
  tokens: tokens,
724
721
  location: location,
725
722
  parsedOptions: this.shouldParseSkeletons
726
- ? (0, icu_skeleton_parser_1.parseNumberSkeleton)(tokens)
723
+ ? parseNumberSkeleton(tokens)
727
724
  : {},
728
725
  },
729
726
  err: null,
@@ -753,7 +750,7 @@ var Parser = /** @class */ (function () {
753
750
  var startPosition = this.clonePosition();
754
751
  if (parentArgType !== 'select' && this.bumpIf('=')) {
755
752
  // Try parse `={number}` selector
756
- var result = this.tryParseDecimalInteger(error_1.ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, error_1.ErrorKind.INVALID_PLURAL_ARGUMENT_SELECTOR);
753
+ var result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, ErrorKind.INVALID_PLURAL_ARGUMENT_SELECTOR);
757
754
  if (result.err) {
758
755
  return result;
759
756
  }
@@ -767,8 +764,8 @@ var Parser = /** @class */ (function () {
767
764
  // Duplicate selector clauses
768
765
  if (parsedSelectors.has(selector)) {
769
766
  return this.error(parentArgType === 'select'
770
- ? error_1.ErrorKind.DUPLICATE_SELECT_ARGUMENT_SELECTOR
771
- : error_1.ErrorKind.DUPLICATE_PLURAL_ARGUMENT_SELECTOR, selectorLocation);
767
+ ? ErrorKind.DUPLICATE_SELECT_ARGUMENT_SELECTOR
768
+ : ErrorKind.DUPLICATE_PLURAL_ARGUMENT_SELECTOR, selectorLocation);
772
769
  }
773
770
  if (selector === 'other') {
774
771
  hasOtherClause = true;
@@ -780,8 +777,8 @@ var Parser = /** @class */ (function () {
780
777
  var openingBracePosition = this.clonePosition();
781
778
  if (!this.bumpIf('{')) {
782
779
  return this.error(parentArgType === 'select'
783
- ? error_1.ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT
784
- : error_1.ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT, createLocation(this.clonePosition(), this.clonePosition()));
780
+ ? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT
781
+ : ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT, createLocation(this.clonePosition(), this.clonePosition()));
785
782
  }
786
783
  var fragmentResult = this.parseMessage(nestingLevel + 1, parentArgType, expectCloseTag);
787
784
  if (fragmentResult.err) {
@@ -806,11 +803,11 @@ var Parser = /** @class */ (function () {
806
803
  }
807
804
  if (options.length === 0) {
808
805
  return this.error(parentArgType === 'select'
809
- ? error_1.ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR
810
- : error_1.ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, createLocation(this.clonePosition(), this.clonePosition()));
806
+ ? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR
807
+ : ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, createLocation(this.clonePosition(), this.clonePosition()));
811
808
  }
812
809
  if (this.requiresOtherClause && !hasOtherClause) {
813
- return this.error(error_1.ErrorKind.MISSING_OTHER_CLAUSE, createLocation(this.clonePosition(), this.clonePosition()));
810
+ return this.error(ErrorKind.MISSING_OTHER_CLAUSE, createLocation(this.clonePosition(), this.clonePosition()));
814
811
  }
815
812
  return { val: options, err: null };
816
813
  };
@@ -976,7 +973,7 @@ var Parser = /** @class */ (function () {
976
973
  };
977
974
  return Parser;
978
975
  }());
979
- exports.Parser = Parser;
976
+ export { Parser };
980
977
  /**
981
978
  * This check if codepoint is alphabet (lower & uppercase)
982
979
  * @param codepoint
package/printer.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DateTimeSkeleton, MessageFormatElement } from './types';
1
+ import { DateTimeSkeleton, MessageFormatElement } from './types.js';
2
2
  export declare function printAST(ast: MessageFormatElement[]): string;
3
3
  export declare function doPrintAST(ast: MessageFormatElement[], isInPlural: boolean): string;
4
4
  export declare function printDateTimeSkeleton(style: DateTimeSkeleton): string;
package/printer.js CHANGED
@@ -1,34 +1,29 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.printAST = printAST;
4
- exports.doPrintAST = doPrintAST;
5
- exports.printDateTimeSkeleton = printDateTimeSkeleton;
6
- var tslib_1 = require("tslib");
7
- var types_1 = require("./types");
8
- function printAST(ast) {
1
+ import { __spreadArray } from "tslib";
2
+ import { isArgumentElement, isDateElement, isLiteralElement, isNumberElement, isPluralElement, isPoundElement, isSelectElement, isTagElement, isTimeElement, SKELETON_TYPE, TYPE, } from './types.js';
3
+ export function printAST(ast) {
9
4
  return doPrintAST(ast, false);
10
5
  }
11
- function doPrintAST(ast, isInPlural) {
6
+ export function doPrintAST(ast, isInPlural) {
12
7
  var printedNodes = ast.map(function (el, i) {
13
- if ((0, types_1.isLiteralElement)(el)) {
8
+ if (isLiteralElement(el)) {
14
9
  return printLiteralElement(el, isInPlural, i === 0, i === ast.length - 1);
15
10
  }
16
- if ((0, types_1.isArgumentElement)(el)) {
11
+ if (isArgumentElement(el)) {
17
12
  return printArgumentElement(el);
18
13
  }
19
- if ((0, types_1.isDateElement)(el) || (0, types_1.isTimeElement)(el) || (0, types_1.isNumberElement)(el)) {
14
+ if (isDateElement(el) || isTimeElement(el) || isNumberElement(el)) {
20
15
  return printSimpleFormatElement(el);
21
16
  }
22
- if ((0, types_1.isPluralElement)(el)) {
17
+ if (isPluralElement(el)) {
23
18
  return printPluralElement(el);
24
19
  }
25
- if ((0, types_1.isSelectElement)(el)) {
20
+ if (isSelectElement(el)) {
26
21
  return printSelectElement(el);
27
22
  }
28
- if ((0, types_1.isPoundElement)(el)) {
23
+ if (isPoundElement(el)) {
29
24
  return '#';
30
25
  }
31
- if ((0, types_1.isTagElement)(el)) {
26
+ if (isTagElement(el)) {
32
27
  return printTagElement(el);
33
28
  }
34
29
  });
@@ -60,7 +55,7 @@ function printArgumentElement(_a) {
60
55
  return "{".concat(value, "}");
61
56
  }
62
57
  function printSimpleFormatElement(el) {
63
- return "{".concat(el.value, ", ").concat(types_1.TYPE[el.type]).concat(el.style ? ", ".concat(printArgumentStyle(el.style)) : '', "}");
58
+ return "{".concat(el.value, ", ").concat(TYPE[el.type]).concat(el.style ? ", ".concat(printArgumentStyle(el.style)) : '', "}");
64
59
  }
65
60
  function printNumberSkeletonToken(token) {
66
61
  var stem = token.stem, options = token.options;
@@ -72,14 +67,14 @@ function printArgumentStyle(style) {
72
67
  if (typeof style === 'string') {
73
68
  return printEscapedMessage(style);
74
69
  }
75
- else if (style.type === types_1.SKELETON_TYPE.dateTime) {
70
+ else if (style.type === SKELETON_TYPE.dateTime) {
76
71
  return "::".concat(printDateTimeSkeleton(style));
77
72
  }
78
73
  else {
79
74
  return "::".concat(style.tokens.map(printNumberSkeletonToken).join(' '));
80
75
  }
81
76
  }
82
- function printDateTimeSkeleton(style) {
77
+ export function printDateTimeSkeleton(style) {
83
78
  return style.pattern;
84
79
  }
85
80
  function printSelectElement(el) {
@@ -97,7 +92,7 @@ function printPluralElement(el) {
97
92
  var msg = [
98
93
  el.value,
99
94
  type,
100
- tslib_1.__spreadArray([
95
+ __spreadArray([
101
96
  el.offset ? "offset:".concat(el.offset) : ''
102
97
  ], Object.keys(el.options).map(function (id) { return "".concat(id, "{").concat(doPrintAST(el.options[id].value, true), "}"); }), true).filter(Boolean)
103
98
  .join(' '),
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WHITE_SPACE_REGEX = exports.SPACE_SEPARATOR_REGEX = void 0;
4
1
  // @generated from regex-gen.ts
5
- exports.SPACE_SEPARATOR_REGEX = /[ \xA0\u1680\u2000-\u200A\u202F\u205F\u3000]/;
6
- exports.WHITE_SPACE_REGEX = /[\t-\r \x85\u200E\u200F\u2028\u2029]/;
2
+ export var SPACE_SEPARATOR_REGEX = /[ \xA0\u1680\u2000-\u200A\u202F\u205F\u3000]/;
3
+ export var WHITE_SPACE_REGEX = /[\t-\r \x85\u200E\u200F\u2028\u2029]/;
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.timeData = void 0;
4
1
  // @generated from time-data-gen.ts
5
2
  // prettier-ignore
6
- exports.timeData = {
3
+ export var timeData = {
7
4
  "001": [
8
5
  "H",
9
6
  "h"
package/types.js CHANGED
@@ -1,20 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SKELETON_TYPE = exports.TYPE = void 0;
4
- exports.isLiteralElement = isLiteralElement;
5
- exports.isArgumentElement = isArgumentElement;
6
- exports.isNumberElement = isNumberElement;
7
- exports.isDateElement = isDateElement;
8
- exports.isTimeElement = isTimeElement;
9
- exports.isSelectElement = isSelectElement;
10
- exports.isPluralElement = isPluralElement;
11
- exports.isPoundElement = isPoundElement;
12
- exports.isTagElement = isTagElement;
13
- exports.isNumberSkeleton = isNumberSkeleton;
14
- exports.isDateTimeSkeleton = isDateTimeSkeleton;
15
- exports.createLiteralElement = createLiteralElement;
16
- exports.createNumberElement = createNumberElement;
17
- var TYPE;
1
+ export var TYPE;
18
2
  (function (TYPE) {
19
3
  /**
20
4
  * Raw text
@@ -53,55 +37,55 @@ var TYPE;
53
37
  * XML-like tag
54
38
  */
55
39
  TYPE[TYPE["tag"] = 8] = "tag";
56
- })(TYPE || (exports.TYPE = TYPE = {}));
57
- var SKELETON_TYPE;
40
+ })(TYPE || (TYPE = {}));
41
+ export var SKELETON_TYPE;
58
42
  (function (SKELETON_TYPE) {
59
43
  SKELETON_TYPE[SKELETON_TYPE["number"] = 0] = "number";
60
44
  SKELETON_TYPE[SKELETON_TYPE["dateTime"] = 1] = "dateTime";
61
- })(SKELETON_TYPE || (exports.SKELETON_TYPE = SKELETON_TYPE = {}));
45
+ })(SKELETON_TYPE || (SKELETON_TYPE = {}));
62
46
  /**
63
47
  * Type Guards
64
48
  */
65
- function isLiteralElement(el) {
49
+ export function isLiteralElement(el) {
66
50
  return el.type === TYPE.literal;
67
51
  }
68
- function isArgumentElement(el) {
52
+ export function isArgumentElement(el) {
69
53
  return el.type === TYPE.argument;
70
54
  }
71
- function isNumberElement(el) {
55
+ export function isNumberElement(el) {
72
56
  return el.type === TYPE.number;
73
57
  }
74
- function isDateElement(el) {
58
+ export function isDateElement(el) {
75
59
  return el.type === TYPE.date;
76
60
  }
77
- function isTimeElement(el) {
61
+ export function isTimeElement(el) {
78
62
  return el.type === TYPE.time;
79
63
  }
80
- function isSelectElement(el) {
64
+ export function isSelectElement(el) {
81
65
  return el.type === TYPE.select;
82
66
  }
83
- function isPluralElement(el) {
67
+ export function isPluralElement(el) {
84
68
  return el.type === TYPE.plural;
85
69
  }
86
- function isPoundElement(el) {
70
+ export function isPoundElement(el) {
87
71
  return el.type === TYPE.pound;
88
72
  }
89
- function isTagElement(el) {
73
+ export function isTagElement(el) {
90
74
  return el.type === TYPE.tag;
91
75
  }
92
- function isNumberSkeleton(el) {
76
+ export function isNumberSkeleton(el) {
93
77
  return !!(el && typeof el === 'object' && el.type === SKELETON_TYPE.number);
94
78
  }
95
- function isDateTimeSkeleton(el) {
79
+ export function isDateTimeSkeleton(el) {
96
80
  return !!(el && typeof el === 'object' && el.type === SKELETON_TYPE.dateTime);
97
81
  }
98
- function createLiteralElement(value) {
82
+ export function createLiteralElement(value) {
99
83
  return {
100
84
  type: TYPE.literal,
101
85
  value: value,
102
86
  };
103
87
  }
104
- function createNumberElement(value, style) {
88
+ export function createNumberElement(value, style) {
105
89
  return {
106
90
  type: TYPE.number,
107
91
  value: value,
@@ -1,8 +0,0 @@
1
- /**
2
- * Returns the best matching date time pattern if a date time skeleton
3
- * pattern is provided with a locale. Follows the Unicode specification:
4
- * https://www.unicode.org/reports/tr35/tr35-dates.html#table-mapping-requested-time-skeletons-to-patterns
5
- * @param skeleton date time skeleton pattern that possibly includes j, J or C
6
- * @param locale
7
- */
8
- export declare function getBestPattern(skeleton: string, locale: Intl.Locale): string;
@@ -1,83 +0,0 @@
1
- import { timeData } from './time-data.generated';
2
- /**
3
- * Returns the best matching date time pattern if a date time skeleton
4
- * pattern is provided with a locale. Follows the Unicode specification:
5
- * https://www.unicode.org/reports/tr35/tr35-dates.html#table-mapping-requested-time-skeletons-to-patterns
6
- * @param skeleton date time skeleton pattern that possibly includes j, J or C
7
- * @param locale
8
- */
9
- export function getBestPattern(skeleton, locale) {
10
- var skeletonCopy = '';
11
- for (var patternPos = 0; patternPos < skeleton.length; patternPos++) {
12
- var patternChar = skeleton.charAt(patternPos);
13
- if (patternChar === 'j') {
14
- var extraLength = 0;
15
- while (patternPos + 1 < skeleton.length &&
16
- skeleton.charAt(patternPos + 1) === patternChar) {
17
- extraLength++;
18
- patternPos++;
19
- }
20
- var hourLen = 1 + (extraLength & 1);
21
- var dayPeriodLen = extraLength < 2 ? 1 : 3 + (extraLength >> 1);
22
- var dayPeriodChar = 'a';
23
- var hourChar = getDefaultHourSymbolFromLocale(locale);
24
- if (hourChar == 'H' || hourChar == 'k') {
25
- dayPeriodLen = 0;
26
- }
27
- while (dayPeriodLen-- > 0) {
28
- skeletonCopy += dayPeriodChar;
29
- }
30
- while (hourLen-- > 0) {
31
- skeletonCopy = hourChar + skeletonCopy;
32
- }
33
- }
34
- else if (patternChar === 'J') {
35
- skeletonCopy += 'H';
36
- }
37
- else {
38
- skeletonCopy += patternChar;
39
- }
40
- }
41
- return skeletonCopy;
42
- }
43
- /**
44
- * Maps the [hour cycle type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle)
45
- * of the given `locale` to the corresponding time pattern.
46
- * @param locale
47
- */
48
- function getDefaultHourSymbolFromLocale(locale) {
49
- var hourCycle = locale.hourCycle;
50
- if (hourCycle === undefined &&
51
- // @ts-ignore hourCycle(s) is not identified yet
52
- locale.hourCycles &&
53
- // @ts-ignore
54
- locale.hourCycles.length) {
55
- // @ts-ignore
56
- hourCycle = locale.hourCycles[0];
57
- }
58
- if (hourCycle) {
59
- switch (hourCycle) {
60
- case 'h24':
61
- return 'k';
62
- case 'h23':
63
- return 'H';
64
- case 'h12':
65
- return 'h';
66
- case 'h11':
67
- return 'K';
68
- default:
69
- throw new Error('Invalid hourCycle');
70
- }
71
- }
72
- // TODO: Once hourCycle is fully supported remove the following with data generation
73
- var languageTag = locale.language;
74
- var regionTag;
75
- if (languageTag !== 'root') {
76
- regionTag = locale.maximize().region;
77
- }
78
- var hourCycles = timeData[regionTag || ''] ||
79
- timeData[languageTag || ''] ||
80
- timeData["".concat(languageTag, "-001")] ||
81
- timeData['001'];
82
- return hourCycles[0];
83
- }