@formatjs/icu-messageformat-parser
Advanced tools
Comparing version 2.2.0 to 2.3.0
import { __spreadArray } from "tslib"; | ||
import { isPluralElement, isSelectElement, } from './types'; | ||
import { isPluralElement, isSelectElement, isTagElement, } from './types'; | ||
function cloneDeep(obj) { | ||
@@ -18,2 +18,29 @@ if (Array.isArray(obj)) { | ||
} | ||
function hoistPluralOrSelectElement(ast, el, positionToInject) { | ||
// pull this out of the ast and move it to the top | ||
var cloned = cloneDeep(el); | ||
var options = cloned.options; | ||
cloned.options = Object.keys(options).reduce(function (all, k) { | ||
var newValue = hoistSelectors(__spreadArray(__spreadArray(__spreadArray([], ast.slice(0, positionToInject), true), options[k].value, true), ast.slice(positionToInject + 1), true)); | ||
all[k] = { | ||
value: newValue, | ||
}; | ||
return all; | ||
}, {}); | ||
return cloned; | ||
} | ||
function isPluralOrSelectElement(el) { | ||
return isPluralElement(el) || isSelectElement(el); | ||
} | ||
function findPluralOrSelectElement(ast) { | ||
return !!ast.find(function (el) { | ||
if (isPluralOrSelectElement(el)) { | ||
return true; | ||
} | ||
if (isTagElement(el)) { | ||
return findPluralOrSelectElement(el.children); | ||
} | ||
return false; | ||
}); | ||
} | ||
/** | ||
@@ -31,24 +58,12 @@ * Hoist all selectors to the beginning of the AST & flatten the | ||
export function hoistSelectors(ast) { | ||
var _loop_1 = function (i) { | ||
for (var i = 0; i < ast.length; i++) { | ||
var el = ast[i]; | ||
if (isPluralElement(el) || isSelectElement(el)) { | ||
// pull this out of the ast and move it to the top | ||
var cloned = cloneDeep(el); | ||
var options_1 = cloned.options; | ||
cloned.options = Object.keys(options_1).reduce(function (all, k) { | ||
var newValue = hoistSelectors(__spreadArray(__spreadArray(__spreadArray([], ast.slice(0, i), true), options_1[k].value, true), ast.slice(i + 1), true)); | ||
all[k] = { | ||
value: newValue, | ||
}; | ||
return all; | ||
}, {}); | ||
return { value: [cloned] }; | ||
if (isPluralOrSelectElement(el)) { | ||
return [hoistPluralOrSelectElement(ast, el, i)]; | ||
} | ||
}; | ||
for (var i = 0; i < ast.length; i++) { | ||
var state_1 = _loop_1(i); | ||
if (typeof state_1 === "object") | ||
return state_1.value; | ||
if (isTagElement(el) && findPluralOrSelectElement([el])) { | ||
throw new Error('Cannot hoist plural/select within a tag element. Please put the tag element inside each plural/select option'); | ||
} | ||
} | ||
return ast; | ||
} |
@@ -21,2 +21,29 @@ "use strict"; | ||
} | ||
function hoistPluralOrSelectElement(ast, el, positionToInject) { | ||
// pull this out of the ast and move it to the top | ||
var cloned = cloneDeep(el); | ||
var options = cloned.options; | ||
cloned.options = Object.keys(options).reduce(function (all, k) { | ||
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)); | ||
all[k] = { | ||
value: newValue, | ||
}; | ||
return all; | ||
}, {}); | ||
return cloned; | ||
} | ||
function isPluralOrSelectElement(el) { | ||
return (0, types_1.isPluralElement)(el) || (0, types_1.isSelectElement)(el); | ||
} | ||
function findPluralOrSelectElement(ast) { | ||
return !!ast.find(function (el) { | ||
if (isPluralOrSelectElement(el)) { | ||
return true; | ||
} | ||
if ((0, types_1.isTagElement)(el)) { | ||
return findPluralOrSelectElement(el.children); | ||
} | ||
return false; | ||
}); | ||
} | ||
/** | ||
@@ -34,22 +61,10 @@ * Hoist all selectors to the beginning of the AST & flatten the | ||
function hoistSelectors(ast) { | ||
var _loop_1 = function (i) { | ||
for (var i = 0; i < ast.length; i++) { | ||
var el = ast[i]; | ||
if ((0, types_1.isPluralElement)(el) || (0, types_1.isSelectElement)(el)) { | ||
// pull this out of the ast and move it to the top | ||
var cloned = cloneDeep(el); | ||
var options_1 = cloned.options; | ||
cloned.options = Object.keys(options_1).reduce(function (all, k) { | ||
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)); | ||
all[k] = { | ||
value: newValue, | ||
}; | ||
return all; | ||
}, {}); | ||
return { value: [cloned] }; | ||
if (isPluralOrSelectElement(el)) { | ||
return [hoistPluralOrSelectElement(ast, el, i)]; | ||
} | ||
}; | ||
for (var i = 0; i < ast.length; i++) { | ||
var state_1 = _loop_1(i); | ||
if (typeof state_1 === "object") | ||
return state_1.value; | ||
if ((0, types_1.isTagElement)(el) && findPluralOrSelectElement([el])) { | ||
throw new Error('Cannot hoist plural/select within a tag element. Please put the tag element inside each plural/select option'); | ||
} | ||
} | ||
@@ -56,0 +71,0 @@ return ast; |
{ | ||
"name": "@formatjs/icu-messageformat-parser", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "module": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
218533
6972