@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.
package/printer.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DateTimeSkeleton, MessageFormatElement } from './types.js';
1
+ import { type DateTimeSkeleton, type 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,101 +1,90 @@
1
- import { __spreadArray } from "tslib";
2
- import { isArgumentElement, isDateElement, isLiteralElement, isNumberElement, isPluralElement, isPoundElement, isSelectElement, isTagElement, isTimeElement, SKELETON_TYPE, TYPE, } from './types.js';
1
+ import "@formatjs/icu-skeleton-parser";
2
+ import { isArgumentElement, isDateElement, isLiteralElement, isNumberElement, isPluralElement, isPoundElement, isSelectElement, isTagElement, isTimeElement, SKELETON_TYPE, TYPE } from "./types.js";
3
3
  export function printAST(ast) {
4
- return doPrintAST(ast, false);
4
+ return doPrintAST(ast, false);
5
5
  }
6
6
  export function doPrintAST(ast, isInPlural) {
7
- var printedNodes = ast.map(function (el, i) {
8
- if (isLiteralElement(el)) {
9
- return printLiteralElement(el, isInPlural, i === 0, i === ast.length - 1);
10
- }
11
- if (isArgumentElement(el)) {
12
- return printArgumentElement(el);
13
- }
14
- if (isDateElement(el) || isTimeElement(el) || isNumberElement(el)) {
15
- return printSimpleFormatElement(el);
16
- }
17
- if (isPluralElement(el)) {
18
- return printPluralElement(el);
19
- }
20
- if (isSelectElement(el)) {
21
- return printSelectElement(el);
22
- }
23
- if (isPoundElement(el)) {
24
- return '#';
25
- }
26
- if (isTagElement(el)) {
27
- return printTagElement(el);
28
- }
29
- });
30
- return printedNodes.join('');
7
+ const printedNodes = ast.map((el, i) => {
8
+ if (isLiteralElement(el)) {
9
+ return printLiteralElement(el, isInPlural, i === 0, i === ast.length - 1);
10
+ }
11
+ if (isArgumentElement(el)) {
12
+ return printArgumentElement(el);
13
+ }
14
+ if (isDateElement(el) || isTimeElement(el) || isNumberElement(el)) {
15
+ return printSimpleFormatElement(el);
16
+ }
17
+ if (isPluralElement(el)) {
18
+ return printPluralElement(el);
19
+ }
20
+ if (isSelectElement(el)) {
21
+ return printSelectElement(el);
22
+ }
23
+ if (isPoundElement(el)) {
24
+ return "#";
25
+ }
26
+ if (isTagElement(el)) {
27
+ return printTagElement(el);
28
+ }
29
+ });
30
+ return printedNodes.join("");
31
31
  }
32
32
  function printTagElement(el) {
33
- return "<".concat(el.value, ">").concat(printAST(el.children), "</").concat(el.value, ">");
33
+ return `<${el.value}>${printAST(el.children)}</${el.value}>`;
34
34
  }
35
35
  function printEscapedMessage(message) {
36
- return message.replace(/([{}](?:[\s\S]*[{}])?)/, "'$1'");
36
+ return message.replace(/([{}](?:[\s\S]*[{}])?)/, `'$1'`);
37
37
  }
38
- function printLiteralElement(_a, isInPlural, isFirstEl, isLastEl) {
39
- var value = _a.value;
40
- var escaped = value;
41
- // If this literal starts with a ' and its not the 1st node, this means the node before it is non-literal
42
- // and the `'` needs to be unescaped
43
- if (!isFirstEl && escaped[0] === "'") {
44
- escaped = "''".concat(escaped.slice(1));
45
- }
46
- // Same logic but for last el
47
- if (!isLastEl && escaped[escaped.length - 1] === "'") {
48
- escaped = "".concat(escaped.slice(0, escaped.length - 1), "''");
49
- }
50
- escaped = printEscapedMessage(escaped);
51
- return isInPlural ? escaped.replace('#', "'#'") : escaped;
38
+ function printLiteralElement({ value }, isInPlural, isFirstEl, isLastEl) {
39
+ let escaped = value;
40
+ // If this literal starts with a ' and its not the 1st node, this means the node before it is non-literal
41
+ // and the `'` needs to be unescaped
42
+ if (!isFirstEl && escaped[0] === `'`) {
43
+ escaped = `''${escaped.slice(1)}`;
44
+ }
45
+ // Same logic but for last el
46
+ if (!isLastEl && escaped[escaped.length - 1] === `'`) {
47
+ escaped = `${escaped.slice(0, escaped.length - 1)}''`;
48
+ }
49
+ escaped = printEscapedMessage(escaped);
50
+ return isInPlural ? escaped.replace("#", "'#'") : escaped;
52
51
  }
53
- function printArgumentElement(_a) {
54
- var value = _a.value;
55
- return "{".concat(value, "}");
52
+ function printArgumentElement({ value }) {
53
+ return `{${value}}`;
56
54
  }
57
55
  function printSimpleFormatElement(el) {
58
- return "{".concat(el.value, ", ").concat(TYPE[el.type]).concat(el.style ? ", ".concat(printArgumentStyle(el.style)) : '', "}");
56
+ return `{${el.value}, ${TYPE[el.type]}${el.style ? `, ${printArgumentStyle(el.style)}` : ""}}`;
59
57
  }
60
58
  function printNumberSkeletonToken(token) {
61
- var stem = token.stem, options = token.options;
62
- return options.length === 0
63
- ? stem
64
- : "".concat(stem).concat(options.map(function (o) { return "/".concat(o); }).join(''));
59
+ const { stem, options } = token;
60
+ return options.length === 0 ? stem : `${stem}${options.map((o) => `/${o}`).join("")}`;
65
61
  }
66
62
  function printArgumentStyle(style) {
67
- if (typeof style === 'string') {
68
- return printEscapedMessage(style);
69
- }
70
- else if (style.type === SKELETON_TYPE.dateTime) {
71
- return "::".concat(printDateTimeSkeleton(style));
72
- }
73
- else {
74
- return "::".concat(style.tokens.map(printNumberSkeletonToken).join(' '));
75
- }
63
+ if (typeof style === "string") {
64
+ return printEscapedMessage(style);
65
+ } else if (style.type === SKELETON_TYPE.dateTime) {
66
+ return `::${printDateTimeSkeleton(style)}`;
67
+ } else {
68
+ return `::${style.tokens.map(printNumberSkeletonToken).join(" ")}`;
69
+ }
76
70
  }
77
71
  export function printDateTimeSkeleton(style) {
78
- return style.pattern;
72
+ return style.pattern;
79
73
  }
80
74
  function printSelectElement(el) {
81
- var msg = [
82
- el.value,
83
- 'select',
84
- Object.keys(el.options)
85
- .map(function (id) { return "".concat(id, "{").concat(doPrintAST(el.options[id].value, false), "}"); })
86
- .join(' '),
87
- ].join(',');
88
- return "{".concat(msg, "}");
75
+ const msg = [
76
+ el.value,
77
+ "select",
78
+ Object.keys(el.options).map((id) => `${id}{${doPrintAST(el.options[id].value, false)}}`).join(" ")
79
+ ].join(",");
80
+ return `{${msg}}`;
89
81
  }
90
82
  function printPluralElement(el) {
91
- var type = el.pluralType === 'cardinal' ? 'plural' : 'selectordinal';
92
- var msg = [
93
- el.value,
94
- type,
95
- __spreadArray([
96
- el.offset ? "offset:".concat(el.offset) : ''
97
- ], Object.keys(el.options).map(function (id) { return "".concat(id, "{").concat(doPrintAST(el.options[id].value, true), "}"); }), true).filter(Boolean)
98
- .join(' '),
99
- ].join(',');
100
- return "{".concat(msg, "}");
83
+ const type = el.pluralType === "cardinal" ? "plural" : "selectordinal";
84
+ const msg = [
85
+ el.value,
86
+ type,
87
+ [el.offset ? `offset:${el.offset}` : "", ...Object.keys(el.options).map((id) => `${id}{${doPrintAST(el.options[id].value, true)}}`)].filter(Boolean).join(" ")
88
+ ].join(",");
89
+ return `{${msg}}`;
101
90
  }
@@ -1,3 +1,3 @@
1
1
  // @generated from regex-gen.ts
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]/;
2
+ export const SPACE_SEPARATOR_REGEX = /[ \xA0\u1680\u2000-\u200A\u202F\u205F\u3000]/;
3
+ export const WHITE_SPACE_REGEX = /[\t-\r \x85\u200E\u200F\u2028\u2029]/;