@formatjs/icu-messageformat-parser 2.2.0 → 2.3.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 +1 @@
1
- {"version":3,"file":"manipulator.d.ts","sourceRoot":"","sources":["../manipulator.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,oBAAoB,EAErB,MAAM,SAAS,CAAA;AAkBhB;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,oBAAoB,EAAE,GAC1B,oBAAoB,EAAE,CAyBxB"}
1
+ {"version":3,"file":"manipulator.d.ts","sourceRoot":"","sources":["../manipulator.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,oBAAoB,EAIrB,MAAM,SAAS,CAAA;AA6DhB;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,oBAAoB,EAAE,GAC1B,oBAAoB,EAAE,CAaxB"}
@@ -1,5 +1,5 @@
1
1
  import { __spreadArray } from "tslib";
2
- import { isPluralElement, isSelectElement, } from './types';
2
+ import { isPluralElement, isSelectElement, isTagElement, } from './types';
3
3
  function cloneDeep(obj) {
4
4
  if (Array.isArray(obj)) {
5
5
  // @ts-expect-error meh
@@ -15,6 +15,33 @@ function cloneDeep(obj) {
15
15
  }
16
16
  return obj;
17
17
  }
18
+ function hoistPluralOrSelectElement(ast, el, positionToInject) {
19
+ // pull this out of the ast and move it to the top
20
+ var cloned = cloneDeep(el);
21
+ var options = cloned.options;
22
+ cloned.options = Object.keys(options).reduce(function (all, k) {
23
+ var newValue = hoistSelectors(__spreadArray(__spreadArray(__spreadArray([], ast.slice(0, positionToInject), true), options[k].value, true), ast.slice(positionToInject + 1), true));
24
+ all[k] = {
25
+ value: newValue,
26
+ };
27
+ return all;
28
+ }, {});
29
+ return cloned;
30
+ }
31
+ function isPluralOrSelectElement(el) {
32
+ return isPluralElement(el) || isSelectElement(el);
33
+ }
34
+ function findPluralOrSelectElement(ast) {
35
+ return !!ast.find(function (el) {
36
+ if (isPluralOrSelectElement(el)) {
37
+ return true;
38
+ }
39
+ if (isTagElement(el)) {
40
+ return findPluralOrSelectElement(el.children);
41
+ }
42
+ return false;
43
+ });
44
+ }
18
45
  /**
19
46
  * Hoist all selectors to the beginning of the AST & flatten the
20
47
  * resulting options. E.g:
@@ -27,26 +54,14 @@ function cloneDeep(obj) {
27
54
  * @param ast AST
28
55
  */
29
56
  export function hoistSelectors(ast) {
30
- var _loop_1 = function (i) {
57
+ for (var i = 0; i < ast.length; i++) {
31
58
  var el = ast[i];
32
- if (isPluralElement(el) || isSelectElement(el)) {
33
- // pull this out of the ast and move it to the top
34
- var cloned = cloneDeep(el);
35
- var options_1 = cloned.options;
36
- cloned.options = Object.keys(options_1).reduce(function (all, k) {
37
- var newValue = hoistSelectors(__spreadArray(__spreadArray(__spreadArray([], ast.slice(0, i), true), options_1[k].value, true), ast.slice(i + 1), true));
38
- all[k] = {
39
- value: newValue,
40
- };
41
- return all;
42
- }, {});
43
- return { value: [cloned] };
59
+ if (isPluralOrSelectElement(el)) {
60
+ return [hoistPluralOrSelectElement(ast, el, i)];
61
+ }
62
+ if (isTagElement(el) && findPluralOrSelectElement([el])) {
63
+ throw new Error('Cannot hoist plural/select within a tag element. Please put the tag element inside each plural/select option');
44
64
  }
45
- };
46
- for (var i = 0; i < ast.length; i++) {
47
- var state_1 = _loop_1(i);
48
- if (typeof state_1 === "object")
49
- return state_1.value;
50
65
  }
51
66
  return ast;
52
67
  }
@@ -1 +1 @@
1
- {"version":3,"file":"manipulator.d.ts","sourceRoot":"","sources":["manipulator.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,oBAAoB,EAErB,MAAM,SAAS,CAAA;AAkBhB;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,oBAAoB,EAAE,GAC1B,oBAAoB,EAAE,CAyBxB"}
1
+ {"version":3,"file":"manipulator.d.ts","sourceRoot":"","sources":["manipulator.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,oBAAoB,EAIrB,MAAM,SAAS,CAAA;AA6DhB;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,oBAAoB,EAAE,GAC1B,oBAAoB,EAAE,CAaxB"}
package/manipulator.js CHANGED
@@ -18,6 +18,33 @@ function cloneDeep(obj) {
18
18
  }
19
19
  return obj;
20
20
  }
21
+ function hoistPluralOrSelectElement(ast, el, positionToInject) {
22
+ // pull this out of the ast and move it to the top
23
+ var cloned = cloneDeep(el);
24
+ var options = cloned.options;
25
+ cloned.options = Object.keys(options).reduce(function (all, k) {
26
+ var newValue = hoistSelectors(tslib_1.__spreadArray(tslib_1.__spreadArray(tslib_1.__spreadArray([], ast.slice(0, positionToInject), true), options[k].value, true), ast.slice(positionToInject + 1), true));
27
+ all[k] = {
28
+ value: newValue,
29
+ };
30
+ return all;
31
+ }, {});
32
+ return cloned;
33
+ }
34
+ function isPluralOrSelectElement(el) {
35
+ return (0, types_1.isPluralElement)(el) || (0, types_1.isSelectElement)(el);
36
+ }
37
+ function findPluralOrSelectElement(ast) {
38
+ return !!ast.find(function (el) {
39
+ if (isPluralOrSelectElement(el)) {
40
+ return true;
41
+ }
42
+ if ((0, types_1.isTagElement)(el)) {
43
+ return findPluralOrSelectElement(el.children);
44
+ }
45
+ return false;
46
+ });
47
+ }
21
48
  /**
22
49
  * Hoist all selectors to the beginning of the AST & flatten the
23
50
  * resulting options. E.g:
@@ -30,26 +57,14 @@ function cloneDeep(obj) {
30
57
  * @param ast AST
31
58
  */
32
59
  function hoistSelectors(ast) {
33
- var _loop_1 = function (i) {
60
+ for (var i = 0; i < ast.length; i++) {
34
61
  var el = ast[i];
35
- if ((0, types_1.isPluralElement)(el) || (0, types_1.isSelectElement)(el)) {
36
- // pull this out of the ast and move it to the top
37
- var cloned = cloneDeep(el);
38
- var options_1 = cloned.options;
39
- cloned.options = Object.keys(options_1).reduce(function (all, k) {
40
- var newValue = hoistSelectors(tslib_1.__spreadArray(tslib_1.__spreadArray(tslib_1.__spreadArray([], ast.slice(0, i), true), options_1[k].value, true), ast.slice(i + 1), true));
41
- all[k] = {
42
- value: newValue,
43
- };
44
- return all;
45
- }, {});
46
- return { value: [cloned] };
62
+ if (isPluralOrSelectElement(el)) {
63
+ return [hoistPluralOrSelectElement(ast, el, i)];
64
+ }
65
+ if ((0, types_1.isTagElement)(el) && findPluralOrSelectElement([el])) {
66
+ throw new Error('Cannot hoist plural/select within a tag element. Please put the tag element inside each plural/select option');
47
67
  }
48
- };
49
- for (var i = 0; i < ast.length; i++) {
50
- var state_1 = _loop_1(i);
51
- if (typeof state_1 === "object")
52
- return state_1.value;
53
68
  }
54
69
  return ast;
55
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formatjs/icu-messageformat-parser",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "main": "index.js",
5
5
  "module": "lib/index.js",
6
6
  "types": "index.d.ts",