@formatjs/icu-messageformat-parser 3.2.1 → 3.4.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.
@@ -1,8 +1,8 @@
1
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
- */
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
8
  export declare function getBestPattern(skeleton: string, locale: Intl.Locale): string;
@@ -1,83 +1,68 @@
1
- import { timeData } from './time-data.generated.js';
1
+ import { timeData } from "./time-data.generated.js";
2
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
- */
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
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;
10
+ let skeletonCopy = "";
11
+ for (let patternPos = 0; patternPos < skeleton.length; patternPos++) {
12
+ const patternChar = skeleton.charAt(patternPos);
13
+ if (patternChar === "j") {
14
+ let extraLength = 0;
15
+ while (patternPos + 1 < skeleton.length && skeleton.charAt(patternPos + 1) === patternChar) {
16
+ extraLength++;
17
+ patternPos++;
18
+ }
19
+ let hourLen = 1 + (extraLength & 1);
20
+ let dayPeriodLen = extraLength < 2 ? 1 : 3 + (extraLength >> 1);
21
+ let dayPeriodChar = "a";
22
+ let hourChar = getDefaultHourSymbolFromLocale(locale);
23
+ if (hourChar == "H" || hourChar == "k") {
24
+ dayPeriodLen = 0;
25
+ }
26
+ while (dayPeriodLen-- > 0) {
27
+ skeletonCopy += dayPeriodChar;
28
+ }
29
+ while (hourLen-- > 0) {
30
+ skeletonCopy = hourChar + skeletonCopy;
31
+ }
32
+ } else if (patternChar === "J") {
33
+ skeletonCopy += "H";
34
+ } else {
35
+ skeletonCopy += patternChar;
36
+ }
37
+ }
38
+ return skeletonCopy;
42
39
  }
43
40
  /**
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
- */
41
+ * Maps the [hour cycle type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle)
42
+ * of the given `locale` to the corresponding time pattern.
43
+ * @param locale
44
+ */
48
45
  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];
46
+ let hourCycle = locale.hourCycle;
47
+ if (hourCycle === undefined && locale.hourCycles && locale.hourCycles.length) {
48
+ // @ts-ignore
49
+ hourCycle = locale.hourCycles[0];
50
+ }
51
+ if (hourCycle) {
52
+ switch (hourCycle) {
53
+ case "h24": return "k";
54
+ case "h23": return "H";
55
+ case "h12": return "h";
56
+ case "h11": return "K";
57
+ default: throw new Error("Invalid hourCycle");
58
+ }
59
+ }
60
+ // TODO: Once hourCycle is fully supported remove the following with data generation
61
+ const languageTag = locale.language;
62
+ let regionTag;
63
+ if (languageTag !== "root") {
64
+ regionTag = locale.maximize().region;
65
+ }
66
+ const hourCycles = timeData[regionTag || ""] || timeData[languageTag || ""] || timeData[`${languageTag}-001`] || timeData["001"];
67
+ return hourCycles[0];
83
68
  }
package/error.d.ts CHANGED
@@ -1,68 +1,68 @@
1
- import { Location } from './types.js';
1
+ import { type Location } from "./types.js";
2
2
  export interface ParserError {
3
- kind: ErrorKind;
4
- message: string;
5
- location: Location;
3
+ kind: ErrorKind;
4
+ message: string;
5
+ location: Location;
6
6
  }
7
7
  export declare enum ErrorKind {
8
- /** Argument is unclosed (e.g. `{0`) */
9
- EXPECT_ARGUMENT_CLOSING_BRACE = 1,
10
- /** Argument is empty (e.g. `{}`). */
11
- EMPTY_ARGUMENT = 2,
12
- /** Argument is malformed (e.g. `{foo!}``) */
13
- MALFORMED_ARGUMENT = 3,
14
- /** Expect an argument type (e.g. `{foo,}`) */
15
- EXPECT_ARGUMENT_TYPE = 4,
16
- /** Unsupported argument type (e.g. `{foo,foo}`) */
17
- INVALID_ARGUMENT_TYPE = 5,
18
- /** Expect an argument style (e.g. `{foo, number, }`) */
19
- EXPECT_ARGUMENT_STYLE = 6,
20
- /** The number skeleton is invalid. */
21
- INVALID_NUMBER_SKELETON = 7,
22
- /** The date time skeleton is invalid. */
23
- INVALID_DATE_TIME_SKELETON = 8,
24
- /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
25
- EXPECT_NUMBER_SKELETON = 9,
26
- /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
27
- EXPECT_DATE_TIME_SKELETON = 10,
28
- /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
29
- UNCLOSED_QUOTE_IN_ARGUMENT_STYLE = 11,
30
- /** Missing select argument options (e.g. `{foo, select}`) */
31
- EXPECT_SELECT_ARGUMENT_OPTIONS = 12,
32
- /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
33
- EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE = 13,
34
- /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
35
- INVALID_PLURAL_ARGUMENT_OFFSET_VALUE = 14,
36
- /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
37
- EXPECT_SELECT_ARGUMENT_SELECTOR = 15,
38
- /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
39
- EXPECT_PLURAL_ARGUMENT_SELECTOR = 16,
40
- /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
41
- EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT = 17,
42
- /**
43
- * Expecting a message fragment after the `plural` or `selectordinal` selector
44
- * (e.g. `{foo, plural, one}`)
45
- */
46
- EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT = 18,
47
- /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
48
- INVALID_PLURAL_ARGUMENT_SELECTOR = 19,
49
- /**
50
- * Duplicate selectors in `plural` or `selectordinal` argument.
51
- * (e.g. {foo, plural, one {#} one {#}})
52
- */
53
- DUPLICATE_PLURAL_ARGUMENT_SELECTOR = 20,
54
- /** Duplicate selectors in `select` argument.
55
- * (e.g. {foo, select, apple {apple} apple {apple}})
56
- */
57
- DUPLICATE_SELECT_ARGUMENT_SELECTOR = 21,
58
- /** Plural or select argument option must have `other` clause. */
59
- MISSING_OTHER_CLAUSE = 22,
60
- /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
61
- INVALID_TAG = 23,
62
- /** The tag name is invalid. (e.g. `<123>foo</123>`) */
63
- INVALID_TAG_NAME = 25,
64
- /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
65
- UNMATCHED_CLOSING_TAG = 26,
66
- /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
67
- UNCLOSED_TAG = 27
8
+ /** Argument is unclosed (e.g. `{0`) */
9
+ EXPECT_ARGUMENT_CLOSING_BRACE = 1,
10
+ /** Argument is empty (e.g. `{}`). */
11
+ EMPTY_ARGUMENT = 2,
12
+ /** Argument is malformed (e.g. `{foo!}``) */
13
+ MALFORMED_ARGUMENT = 3,
14
+ /** Expect an argument type (e.g. `{foo,}`) */
15
+ EXPECT_ARGUMENT_TYPE = 4,
16
+ /** Unsupported argument type (e.g. `{foo,foo}`) */
17
+ INVALID_ARGUMENT_TYPE = 5,
18
+ /** Expect an argument style (e.g. `{foo, number, }`) */
19
+ EXPECT_ARGUMENT_STYLE = 6,
20
+ /** The number skeleton is invalid. */
21
+ INVALID_NUMBER_SKELETON = 7,
22
+ /** The date time skeleton is invalid. */
23
+ INVALID_DATE_TIME_SKELETON = 8,
24
+ /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
25
+ EXPECT_NUMBER_SKELETON = 9,
26
+ /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
27
+ EXPECT_DATE_TIME_SKELETON = 10,
28
+ /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
29
+ UNCLOSED_QUOTE_IN_ARGUMENT_STYLE = 11,
30
+ /** Missing select argument options (e.g. `{foo, select}`) */
31
+ EXPECT_SELECT_ARGUMENT_OPTIONS = 12,
32
+ /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
33
+ EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE = 13,
34
+ /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
35
+ INVALID_PLURAL_ARGUMENT_OFFSET_VALUE = 14,
36
+ /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
37
+ EXPECT_SELECT_ARGUMENT_SELECTOR = 15,
38
+ /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
39
+ EXPECT_PLURAL_ARGUMENT_SELECTOR = 16,
40
+ /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
41
+ EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT = 17,
42
+ /**
43
+ * Expecting a message fragment after the `plural` or `selectordinal` selector
44
+ * (e.g. `{foo, plural, one}`)
45
+ */
46
+ EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT = 18,
47
+ /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
48
+ INVALID_PLURAL_ARGUMENT_SELECTOR = 19,
49
+ /**
50
+ * Duplicate selectors in `plural` or `selectordinal` argument.
51
+ * (e.g. {foo, plural, one {#} one {#}})
52
+ */
53
+ DUPLICATE_PLURAL_ARGUMENT_SELECTOR = 20,
54
+ /** Duplicate selectors in `select` argument.
55
+ * (e.g. {foo, select, apple {apple} apple {apple}})
56
+ */
57
+ DUPLICATE_SELECT_ARGUMENT_SELECTOR = 21,
58
+ /** Plural or select argument option must have `other` clause. */
59
+ MISSING_OTHER_CLAUSE = 22,
60
+ /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
61
+ INVALID_TAG = 23,
62
+ /** The tag name is invalid. (e.g. `<123>foo</123>`) */
63
+ INVALID_TAG_NAME = 25,
64
+ /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
65
+ UNMATCHED_CLOSING_TAG = 26,
66
+ /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
67
+ UNCLOSED_TAG = 27
68
68
  }
package/error.js CHANGED
@@ -1,63 +1,64 @@
1
- export var ErrorKind;
2
- (function (ErrorKind) {
3
- /** Argument is unclosed (e.g. `{0`) */
4
- ErrorKind[ErrorKind["EXPECT_ARGUMENT_CLOSING_BRACE"] = 1] = "EXPECT_ARGUMENT_CLOSING_BRACE";
5
- /** Argument is empty (e.g. `{}`). */
6
- ErrorKind[ErrorKind["EMPTY_ARGUMENT"] = 2] = "EMPTY_ARGUMENT";
7
- /** Argument is malformed (e.g. `{foo!}``) */
8
- ErrorKind[ErrorKind["MALFORMED_ARGUMENT"] = 3] = "MALFORMED_ARGUMENT";
9
- /** Expect an argument type (e.g. `{foo,}`) */
10
- ErrorKind[ErrorKind["EXPECT_ARGUMENT_TYPE"] = 4] = "EXPECT_ARGUMENT_TYPE";
11
- /** Unsupported argument type (e.g. `{foo,foo}`) */
12
- ErrorKind[ErrorKind["INVALID_ARGUMENT_TYPE"] = 5] = "INVALID_ARGUMENT_TYPE";
13
- /** Expect an argument style (e.g. `{foo, number, }`) */
14
- ErrorKind[ErrorKind["EXPECT_ARGUMENT_STYLE"] = 6] = "EXPECT_ARGUMENT_STYLE";
15
- /** The number skeleton is invalid. */
16
- ErrorKind[ErrorKind["INVALID_NUMBER_SKELETON"] = 7] = "INVALID_NUMBER_SKELETON";
17
- /** The date time skeleton is invalid. */
18
- ErrorKind[ErrorKind["INVALID_DATE_TIME_SKELETON"] = 8] = "INVALID_DATE_TIME_SKELETON";
19
- /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
20
- ErrorKind[ErrorKind["EXPECT_NUMBER_SKELETON"] = 9] = "EXPECT_NUMBER_SKELETON";
21
- /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
22
- ErrorKind[ErrorKind["EXPECT_DATE_TIME_SKELETON"] = 10] = "EXPECT_DATE_TIME_SKELETON";
23
- /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
24
- ErrorKind[ErrorKind["UNCLOSED_QUOTE_IN_ARGUMENT_STYLE"] = 11] = "UNCLOSED_QUOTE_IN_ARGUMENT_STYLE";
25
- /** Missing select argument options (e.g. `{foo, select}`) */
26
- ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_OPTIONS"] = 12] = "EXPECT_SELECT_ARGUMENT_OPTIONS";
27
- /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
28
- ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE"] = 13] = "EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE";
29
- /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
30
- ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_OFFSET_VALUE"] = 14] = "INVALID_PLURAL_ARGUMENT_OFFSET_VALUE";
31
- /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
32
- ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR"] = 15] = "EXPECT_SELECT_ARGUMENT_SELECTOR";
33
- /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
34
- ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR"] = 16] = "EXPECT_PLURAL_ARGUMENT_SELECTOR";
35
- /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
36
- ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT"] = 17] = "EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT";
37
- /**
38
- * Expecting a message fragment after the `plural` or `selectordinal` selector
39
- * (e.g. `{foo, plural, one}`)
40
- */
41
- ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT"] = 18] = "EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT";
42
- /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
43
- ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_SELECTOR"] = 19] = "INVALID_PLURAL_ARGUMENT_SELECTOR";
44
- /**
45
- * Duplicate selectors in `plural` or `selectordinal` argument.
46
- * (e.g. {foo, plural, one {#} one {#}})
47
- */
48
- ErrorKind[ErrorKind["DUPLICATE_PLURAL_ARGUMENT_SELECTOR"] = 20] = "DUPLICATE_PLURAL_ARGUMENT_SELECTOR";
49
- /** Duplicate selectors in `select` argument.
50
- * (e.g. {foo, select, apple {apple} apple {apple}})
51
- */
52
- ErrorKind[ErrorKind["DUPLICATE_SELECT_ARGUMENT_SELECTOR"] = 21] = "DUPLICATE_SELECT_ARGUMENT_SELECTOR";
53
- /** Plural or select argument option must have `other` clause. */
54
- ErrorKind[ErrorKind["MISSING_OTHER_CLAUSE"] = 22] = "MISSING_OTHER_CLAUSE";
55
- /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
56
- ErrorKind[ErrorKind["INVALID_TAG"] = 23] = "INVALID_TAG";
57
- /** The tag name is invalid. (e.g. `<123>foo</123>`) */
58
- ErrorKind[ErrorKind["INVALID_TAG_NAME"] = 25] = "INVALID_TAG_NAME";
59
- /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
60
- ErrorKind[ErrorKind["UNMATCHED_CLOSING_TAG"] = 26] = "UNMATCHED_CLOSING_TAG";
61
- /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
62
- ErrorKind[ErrorKind["UNCLOSED_TAG"] = 27] = "UNCLOSED_TAG";
63
- })(ErrorKind || (ErrorKind = {}));
1
+ import "./types.js";
2
+ export let ErrorKind = /* @__PURE__ */ function(ErrorKind) {
3
+ /** Argument is unclosed (e.g. `{0`) */
4
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_CLOSING_BRACE"] = 1] = "EXPECT_ARGUMENT_CLOSING_BRACE";
5
+ /** Argument is empty (e.g. `{}`). */
6
+ ErrorKind[ErrorKind["EMPTY_ARGUMENT"] = 2] = "EMPTY_ARGUMENT";
7
+ /** Argument is malformed (e.g. `{foo!}``) */
8
+ ErrorKind[ErrorKind["MALFORMED_ARGUMENT"] = 3] = "MALFORMED_ARGUMENT";
9
+ /** Expect an argument type (e.g. `{foo,}`) */
10
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_TYPE"] = 4] = "EXPECT_ARGUMENT_TYPE";
11
+ /** Unsupported argument type (e.g. `{foo,foo}`) */
12
+ ErrorKind[ErrorKind["INVALID_ARGUMENT_TYPE"] = 5] = "INVALID_ARGUMENT_TYPE";
13
+ /** Expect an argument style (e.g. `{foo, number, }`) */
14
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_STYLE"] = 6] = "EXPECT_ARGUMENT_STYLE";
15
+ /** The number skeleton is invalid. */
16
+ ErrorKind[ErrorKind["INVALID_NUMBER_SKELETON"] = 7] = "INVALID_NUMBER_SKELETON";
17
+ /** The date time skeleton is invalid. */
18
+ ErrorKind[ErrorKind["INVALID_DATE_TIME_SKELETON"] = 8] = "INVALID_DATE_TIME_SKELETON";
19
+ /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
20
+ ErrorKind[ErrorKind["EXPECT_NUMBER_SKELETON"] = 9] = "EXPECT_NUMBER_SKELETON";
21
+ /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
22
+ ErrorKind[ErrorKind["EXPECT_DATE_TIME_SKELETON"] = 10] = "EXPECT_DATE_TIME_SKELETON";
23
+ /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
24
+ ErrorKind[ErrorKind["UNCLOSED_QUOTE_IN_ARGUMENT_STYLE"] = 11] = "UNCLOSED_QUOTE_IN_ARGUMENT_STYLE";
25
+ /** Missing select argument options (e.g. `{foo, select}`) */
26
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_OPTIONS"] = 12] = "EXPECT_SELECT_ARGUMENT_OPTIONS";
27
+ /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
28
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE"] = 13] = "EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE";
29
+ /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
30
+ ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_OFFSET_VALUE"] = 14] = "INVALID_PLURAL_ARGUMENT_OFFSET_VALUE";
31
+ /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
32
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR"] = 15] = "EXPECT_SELECT_ARGUMENT_SELECTOR";
33
+ /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
34
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR"] = 16] = "EXPECT_PLURAL_ARGUMENT_SELECTOR";
35
+ /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
36
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT"] = 17] = "EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT";
37
+ /**
38
+ * Expecting a message fragment after the `plural` or `selectordinal` selector
39
+ * (e.g. `{foo, plural, one}`)
40
+ */
41
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT"] = 18] = "EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT";
42
+ /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
43
+ ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_SELECTOR"] = 19] = "INVALID_PLURAL_ARGUMENT_SELECTOR";
44
+ /**
45
+ * Duplicate selectors in `plural` or `selectordinal` argument.
46
+ * (e.g. {foo, plural, one {#} one {#}})
47
+ */
48
+ ErrorKind[ErrorKind["DUPLICATE_PLURAL_ARGUMENT_SELECTOR"] = 20] = "DUPLICATE_PLURAL_ARGUMENT_SELECTOR";
49
+ /** Duplicate selectors in `select` argument.
50
+ * (e.g. {foo, select, apple {apple} apple {apple}})
51
+ */
52
+ ErrorKind[ErrorKind["DUPLICATE_SELECT_ARGUMENT_SELECTOR"] = 21] = "DUPLICATE_SELECT_ARGUMENT_SELECTOR";
53
+ /** Plural or select argument option must have `other` clause. */
54
+ ErrorKind[ErrorKind["MISSING_OTHER_CLAUSE"] = 22] = "MISSING_OTHER_CLAUSE";
55
+ /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
56
+ ErrorKind[ErrorKind["INVALID_TAG"] = 23] = "INVALID_TAG";
57
+ /** The tag name is invalid. (e.g. `<123>foo</123>`) */
58
+ ErrorKind[ErrorKind["INVALID_TAG_NAME"] = 25] = "INVALID_TAG_NAME";
59
+ /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
60
+ ErrorKind[ErrorKind["UNMATCHED_CLOSING_TAG"] = 26] = "UNMATCHED_CLOSING_TAG";
61
+ /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
62
+ ErrorKind[ErrorKind["UNCLOSED_TAG"] = 27] = "UNCLOSED_TAG";
63
+ return ErrorKind;
64
+ }({});
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Parser, ParserOptions } from './parser.js';
2
- import { MessageFormatElement } from './types.js';
1
+ import { Parser, type ParserOptions } from "./parser.js";
2
+ import { type MessageFormatElement } from "./types.js";
3
3
  export declare function parse(message: string, opts?: ParserOptions): MessageFormatElement[];
4
- export * from './types.js';
4
+ export * from "./types.js";
5
5
  export type { ParserOptions };
6
6
  export declare const _Parser: typeof Parser;
7
- export { isStructurallySame } from './manipulator.js';
7
+ export { isStructurallySame } from "./manipulator.js";
package/index.js CHANGED
@@ -1,46 +1,44 @@
1
- import { __assign } from "tslib";
2
- import { ErrorKind } from './error.js';
3
- import { Parser } from './parser.js';
4
- import { isDateElement, isDateTimeSkeleton, isNumberElement, isNumberSkeleton, isPluralElement, isSelectElement, isTagElement, isTimeElement, } from './types.js';
1
+ import { ErrorKind } from "./error.js";
2
+ import { Parser } from "./parser.js";
3
+ import { isDateElement, isDateTimeSkeleton, isNumberElement, isNumberSkeleton, isPluralElement, isSelectElement, isTagElement, isTimeElement } from "./types.js";
5
4
  function pruneLocation(els) {
6
- els.forEach(function (el) {
7
- delete el.location;
8
- if (isSelectElement(el) || isPluralElement(el)) {
9
- for (var k in el.options) {
10
- delete el.options[k].location;
11
- pruneLocation(el.options[k].value);
12
- }
13
- }
14
- else if (isNumberElement(el) && isNumberSkeleton(el.style)) {
15
- delete el.style.location;
16
- }
17
- else if ((isDateElement(el) || isTimeElement(el)) &&
18
- isDateTimeSkeleton(el.style)) {
19
- delete el.style.location;
20
- }
21
- else if (isTagElement(el)) {
22
- pruneLocation(el.children);
23
- }
24
- });
5
+ els.forEach((el) => {
6
+ delete el.location;
7
+ if (isSelectElement(el) || isPluralElement(el)) {
8
+ for (const k in el.options) {
9
+ delete el.options[k].location;
10
+ pruneLocation(el.options[k].value);
11
+ }
12
+ } else if (isNumberElement(el) && isNumberSkeleton(el.style)) {
13
+ delete el.style.location;
14
+ } else if ((isDateElement(el) || isTimeElement(el)) && isDateTimeSkeleton(el.style)) {
15
+ delete el.style.location;
16
+ } else if (isTagElement(el)) {
17
+ pruneLocation(el.children);
18
+ }
19
+ });
25
20
  }
26
- export function parse(message, opts) {
27
- if (opts === void 0) { opts = {}; }
28
- opts = __assign({ shouldParseSkeletons: true, requiresOtherClause: true }, opts);
29
- var result = new Parser(message, opts).parse();
30
- if (result.err) {
31
- var error = SyntaxError(ErrorKind[result.err.kind]);
32
- // @ts-expect-error Assign to error object
33
- error.location = result.err.location;
34
- // @ts-expect-error Assign to error object
35
- error.originalMessage = result.err.message;
36
- throw error;
37
- }
38
- if (!(opts === null || opts === void 0 ? void 0 : opts.captureLocation)) {
39
- pruneLocation(result.val);
40
- }
41
- return result.val;
21
+ export function parse(message, opts = {}) {
22
+ opts = {
23
+ shouldParseSkeletons: true,
24
+ requiresOtherClause: true,
25
+ ...opts
26
+ };
27
+ const result = new Parser(message, opts).parse();
28
+ if (result.err) {
29
+ const error = SyntaxError(ErrorKind[result.err.kind]);
30
+ // @ts-expect-error Assign to error object
31
+ error.location = result.err.location;
32
+ // @ts-expect-error Assign to error object
33
+ error.originalMessage = result.err.message;
34
+ throw error;
35
+ }
36
+ if (!opts?.captureLocation) {
37
+ pruneLocation(result.val);
38
+ }
39
+ return result.val;
42
40
  }
43
- export * from './types.js';
41
+ export * from "./types.js";
44
42
  // only for testing
45
- export var _Parser = Parser;
46
- export { isStructurallySame } from './manipulator.js';
43
+ export const _Parser = Parser;
44
+ export { isStructurallySame } from "./manipulator.js";
package/manipulator.d.ts CHANGED
@@ -1,26 +1,26 @@
1
- import { MessageFormatElement } from './types.js';
1
+ import { type MessageFormatElement } from "./types.js";
2
2
  /**
3
- * Hoist all selectors to the beginning of the AST & flatten the
4
- * resulting options. E.g:
5
- * "I have {count, plural, one{a dog} other{many dogs}}"
6
- * becomes "{count, plural, one{I have a dog} other{I have many dogs}}".
7
- * If there are multiple selectors, the order of which one is hoisted 1st
8
- * is non-deterministic.
9
- * The goal is to provide as many full sentences as possible since fragmented
10
- * sentences are not translator-friendly
11
- * @param ast AST
12
- */
3
+ * Hoist all selectors to the beginning of the AST & flatten the
4
+ * resulting options. E.g:
5
+ * "I have {count, plural, one{a dog} other{many dogs}}"
6
+ * becomes "{count, plural, one{I have a dog} other{I have many dogs}}".
7
+ * If there are multiple selectors, the order of which one is hoisted 1st
8
+ * is non-deterministic.
9
+ * The goal is to provide as many full sentences as possible since fragmented
10
+ * sentences are not translator-friendly
11
+ * @param ast AST
12
+ */
13
13
  export declare function hoistSelectors(ast: MessageFormatElement[]): MessageFormatElement[];
14
14
  interface IsStructurallySameResult {
15
- error?: Error;
16
- success: boolean;
15
+ error?: Error;
16
+ success: boolean;
17
17
  }
18
18
  /**
19
- * Check if 2 ASTs are structurally the same. This primarily means that
20
- * they have the same variables with the same type
21
- * @param a
22
- * @param b
23
- * @returns
24
- */
19
+ * Check if 2 ASTs are structurally the same. This primarily means that
20
+ * they have the same variables with the same type
21
+ * @param a
22
+ * @param b
23
+ * @returns
24
+ */
25
25
  export declare function isStructurallySame(a: MessageFormatElement[], b: MessageFormatElement[]): IsStructurallySameResult;
26
26
  export {};