@formatjs/icu-messageformat-parser 2.9.8 → 2.10.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/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export declare function parse(message: string, opts?: ParserOptions): MessageFor
4
4
  export * from './types';
5
5
  export type { ParserOptions };
6
6
  export declare const _Parser: typeof Parser;
7
+ export { isStructurallySame } from './manipulator';
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._Parser = void 0;
3
+ exports.isStructurallySame = exports._Parser = void 0;
4
4
  exports.parse = parse;
5
5
  var tslib_1 = require("tslib");
6
6
  var error_1 = require("./error");
@@ -47,3 +47,5 @@ function parse(message, opts) {
47
47
  tslib_1.__exportStar(require("./types"), exports);
48
48
  // only for testing
49
49
  exports._Parser = parser_1.Parser;
50
+ var manipulator_1 = require("./manipulator");
51
+ Object.defineProperty(exports, "isStructurallySame", { enumerable: true, get: function () { return manipulator_1.isStructurallySame; } });
package/lib/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export declare function parse(message: string, opts?: ParserOptions): MessageFor
4
4
  export * from './types';
5
5
  export type { ParserOptions };
6
6
  export declare const _Parser: typeof Parser;
7
+ export { isStructurallySame } from './manipulator';
package/lib/index.js CHANGED
@@ -43,3 +43,4 @@ export function parse(message, opts) {
43
43
  export * from './types';
44
44
  // only for testing
45
45
  export var _Parser = Parser;
46
+ export { isStructurallySame } from './manipulator';
@@ -11,3 +11,4 @@ import { MessageFormatElement } from './types';
11
11
  * @param ast AST
12
12
  */
13
13
  export declare function hoistSelectors(ast: MessageFormatElement[]): MessageFormatElement[];
14
+ export declare function isStructurallySame(a: MessageFormatElement[], b: MessageFormatElement[]): boolean;
@@ -1,5 +1,5 @@
1
1
  import { __spreadArray } from "tslib";
2
- import { isPluralElement, isSelectElement, isTagElement, } from './types';
2
+ import { isArgumentElement, isDateElement, isLiteralElement, isNumberElement, isPluralElement, isPoundElement, isSelectElement, isTagElement, isTimeElement, } from './types';
3
3
  function cloneDeep(obj) {
4
4
  if (Array.isArray(obj)) {
5
5
  // @ts-expect-error meh
@@ -65,3 +65,93 @@ export function hoistSelectors(ast) {
65
65
  }
66
66
  return ast;
67
67
  }
68
+ function isStructurallySamePluralOrSelect(el1, el2) {
69
+ var options1 = el1.options;
70
+ var options2 = el2.options;
71
+ if (Object.keys(options1).length !== Object.keys(options2).length) {
72
+ return false;
73
+ }
74
+ for (var key in options1) {
75
+ if (!options2[key]) {
76
+ return false;
77
+ }
78
+ if (!isStructurallySame(options1[key].value, options2[key].value)) {
79
+ return false;
80
+ }
81
+ }
82
+ return true;
83
+ }
84
+ export function isStructurallySame(a, b) {
85
+ var aWithoutLiteral = a.filter(function (el) { return !isLiteralElement(el); });
86
+ var bWithoutLiteral = b.filter(function (el) { return !isLiteralElement(el); });
87
+ if (aWithoutLiteral.length !== bWithoutLiteral.length) {
88
+ return false;
89
+ }
90
+ var elementsMapInA = aWithoutLiteral.reduce(function (all, el) {
91
+ if (isPoundElement(el)) {
92
+ all['#'] = el;
93
+ return all;
94
+ }
95
+ all[el.value] = el;
96
+ return all;
97
+ }, {});
98
+ var elementsMapInB = bWithoutLiteral.reduce(function (all, el) {
99
+ if (isPoundElement(el)) {
100
+ all['#'] = el;
101
+ return all;
102
+ }
103
+ all[el.value] = el;
104
+ return all;
105
+ }, {});
106
+ for (var _i = 0, _a = Object.keys(elementsMapInA); _i < _a.length; _i++) {
107
+ var varName = _a[_i];
108
+ var elA = elementsMapInA[varName];
109
+ var elB = elementsMapInB[varName];
110
+ if (!elB) {
111
+ return false;
112
+ }
113
+ if (elA.type !== elB.type) {
114
+ return false;
115
+ }
116
+ if (isLiteralElement(elA) || isLiteralElement(elB)) {
117
+ continue;
118
+ }
119
+ if (isArgumentElement(elA) &&
120
+ isArgumentElement(elB) &&
121
+ elA.value !== elB.value) {
122
+ return false;
123
+ }
124
+ if (isPoundElement(elA) || isPoundElement(elB)) {
125
+ continue;
126
+ }
127
+ if (isDateElement(elA) ||
128
+ isTimeElement(elA) ||
129
+ isNumberElement(elA) ||
130
+ isDateElement(elB) ||
131
+ isTimeElement(elB) ||
132
+ isNumberElement(elB)) {
133
+ if (elA.value !== elB.value) {
134
+ return false;
135
+ }
136
+ }
137
+ if (isPluralElement(elA) &&
138
+ isPluralElement(elB) &&
139
+ !isStructurallySamePluralOrSelect(elA, elB)) {
140
+ return false;
141
+ }
142
+ if (isSelectElement(elA) &&
143
+ isSelectElement(elB) &&
144
+ isStructurallySamePluralOrSelect(elA, elB)) {
145
+ return false;
146
+ }
147
+ if (isTagElement(elA) && isTagElement(elB)) {
148
+ if (elA.value !== elB.value) {
149
+ return false;
150
+ }
151
+ if (!isStructurallySame(elA.children, elB.children)) {
152
+ return false;
153
+ }
154
+ }
155
+ }
156
+ return true;
157
+ }
@@ -1,3 +1,4 @@
1
1
  export declare function parse(): void;
2
2
  export * from './types';
3
3
  export declare const _Parser: undefined;
4
+ export { isStructurallySame } from './manipulator';
package/lib/no-parser.js CHANGED
@@ -3,3 +3,4 @@ export function parse() {
3
3
  }
4
4
  export * from './types';
5
5
  export var _Parser = undefined;
6
+ export { isStructurallySame } from './manipulator';
package/lib/types.d.ts CHANGED
@@ -62,11 +62,8 @@ export interface BaseElement<T extends TYPE> {
62
62
  }
63
63
  export type LiteralElement = BaseElement<TYPE.literal>;
64
64
  export type ArgumentElement = BaseElement<TYPE.argument>;
65
- export interface TagElement {
66
- type: TYPE.tag;
67
- value: string;
65
+ export interface TagElement extends BaseElement<TYPE.tag> {
68
66
  children: MessageFormatElement[];
69
- location?: Location;
70
67
  }
71
68
  export interface SimpleFormatElement<T extends TYPE, S extends Skeleton> extends BaseElement<T> {
72
69
  style?: string | S | null;
package/manipulator.d.ts CHANGED
@@ -11,3 +11,4 @@ import { MessageFormatElement } from './types';
11
11
  * @param ast AST
12
12
  */
13
13
  export declare function hoistSelectors(ast: MessageFormatElement[]): MessageFormatElement[];
14
+ export declare function isStructurallySame(a: MessageFormatElement[], b: MessageFormatElement[]): boolean;
package/manipulator.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.hoistSelectors = hoistSelectors;
4
+ exports.isStructurallySame = isStructurallySame;
4
5
  var tslib_1 = require("tslib");
5
6
  var types_1 = require("./types");
6
7
  function cloneDeep(obj) {
@@ -68,3 +69,93 @@ function hoistSelectors(ast) {
68
69
  }
69
70
  return ast;
70
71
  }
72
+ function isStructurallySamePluralOrSelect(el1, el2) {
73
+ var options1 = el1.options;
74
+ var options2 = el2.options;
75
+ if (Object.keys(options1).length !== Object.keys(options2).length) {
76
+ return false;
77
+ }
78
+ for (var key in options1) {
79
+ if (!options2[key]) {
80
+ return false;
81
+ }
82
+ if (!isStructurallySame(options1[key].value, options2[key].value)) {
83
+ return false;
84
+ }
85
+ }
86
+ return true;
87
+ }
88
+ function isStructurallySame(a, b) {
89
+ var aWithoutLiteral = a.filter(function (el) { return !(0, types_1.isLiteralElement)(el); });
90
+ var bWithoutLiteral = b.filter(function (el) { return !(0, types_1.isLiteralElement)(el); });
91
+ if (aWithoutLiteral.length !== bWithoutLiteral.length) {
92
+ return false;
93
+ }
94
+ var elementsMapInA = aWithoutLiteral.reduce(function (all, el) {
95
+ if ((0, types_1.isPoundElement)(el)) {
96
+ all['#'] = el;
97
+ return all;
98
+ }
99
+ all[el.value] = el;
100
+ return all;
101
+ }, {});
102
+ var elementsMapInB = bWithoutLiteral.reduce(function (all, el) {
103
+ if ((0, types_1.isPoundElement)(el)) {
104
+ all['#'] = el;
105
+ return all;
106
+ }
107
+ all[el.value] = el;
108
+ return all;
109
+ }, {});
110
+ for (var _i = 0, _a = Object.keys(elementsMapInA); _i < _a.length; _i++) {
111
+ var varName = _a[_i];
112
+ var elA = elementsMapInA[varName];
113
+ var elB = elementsMapInB[varName];
114
+ if (!elB) {
115
+ return false;
116
+ }
117
+ if (elA.type !== elB.type) {
118
+ return false;
119
+ }
120
+ if ((0, types_1.isLiteralElement)(elA) || (0, types_1.isLiteralElement)(elB)) {
121
+ continue;
122
+ }
123
+ if ((0, types_1.isArgumentElement)(elA) &&
124
+ (0, types_1.isArgumentElement)(elB) &&
125
+ elA.value !== elB.value) {
126
+ return false;
127
+ }
128
+ if ((0, types_1.isPoundElement)(elA) || (0, types_1.isPoundElement)(elB)) {
129
+ continue;
130
+ }
131
+ if ((0, types_1.isDateElement)(elA) ||
132
+ (0, types_1.isTimeElement)(elA) ||
133
+ (0, types_1.isNumberElement)(elA) ||
134
+ (0, types_1.isDateElement)(elB) ||
135
+ (0, types_1.isTimeElement)(elB) ||
136
+ (0, types_1.isNumberElement)(elB)) {
137
+ if (elA.value !== elB.value) {
138
+ return false;
139
+ }
140
+ }
141
+ if ((0, types_1.isPluralElement)(elA) &&
142
+ (0, types_1.isPluralElement)(elB) &&
143
+ !isStructurallySamePluralOrSelect(elA, elB)) {
144
+ return false;
145
+ }
146
+ if ((0, types_1.isSelectElement)(elA) &&
147
+ (0, types_1.isSelectElement)(elB) &&
148
+ isStructurallySamePluralOrSelect(elA, elB)) {
149
+ return false;
150
+ }
151
+ if ((0, types_1.isTagElement)(elA) && (0, types_1.isTagElement)(elB)) {
152
+ if (elA.value !== elB.value) {
153
+ return false;
154
+ }
155
+ if (!isStructurallySame(elA.children, elB.children)) {
156
+ return false;
157
+ }
158
+ }
159
+ }
160
+ return true;
161
+ }
package/no-parser.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare function parse(): void;
2
2
  export * from './types';
3
3
  export declare const _Parser: undefined;
4
+ export { isStructurallySame } from './manipulator';
package/no-parser.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._Parser = void 0;
3
+ exports.isStructurallySame = exports._Parser = void 0;
4
4
  exports.parse = parse;
5
5
  var tslib_1 = require("tslib");
6
6
  function parse() {
@@ -8,3 +8,5 @@ function parse() {
8
8
  }
9
9
  tslib_1.__exportStar(require("./types"), exports);
10
10
  exports._Parser = undefined;
11
+ var manipulator_1 = require("./manipulator");
12
+ Object.defineProperty(exports, "isStructurallySame", { enumerable: true, get: function () { return manipulator_1.isStructurallySame; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formatjs/icu-messageformat-parser",
3
- "version": "2.9.8",
3
+ "version": "2.10.0",
4
4
  "main": "index.js",
5
5
  "module": "lib/index.js",
6
6
  "types": "index.d.ts",
package/types.d.ts CHANGED
@@ -62,11 +62,8 @@ export interface BaseElement<T extends TYPE> {
62
62
  }
63
63
  export type LiteralElement = BaseElement<TYPE.literal>;
64
64
  export type ArgumentElement = BaseElement<TYPE.argument>;
65
- export interface TagElement {
66
- type: TYPE.tag;
67
- value: string;
65
+ export interface TagElement extends BaseElement<TYPE.tag> {
68
66
  children: MessageFormatElement[];
69
- location?: Location;
70
67
  }
71
68
  export interface SimpleFormatElement<T extends TYPE, S extends Skeleton> extends BaseElement<T> {
72
69
  style?: string | S | null;