UNPKG

2.05 kBJavaScriptView Raw
1"use strict";
2var __spreadArrays = (this && this.__spreadArrays) || function () {
3 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
4 for (var r = Array(s), k = 0, i = 0; i < il; i++)
5 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
6 r[k] = a[j];
7 return r;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10var types_1 = require("./types");
11var parser_1 = require("./parser");
12var PLURAL_HASHTAG_REGEX = /(^|[^\\])#/g;
13/**
14 * Whether to convert `#` in plural rule options
15 * to `{var, number}`
16 * @param el AST Element
17 * @param pluralStack current plural stack
18 */
19function normalizeHashtagInPlural(els) {
20 els.forEach(function (el) {
21 // If we're encountering a plural el
22 if (!types_1.isPluralElement(el) && !types_1.isSelectElement(el)) {
23 return;
24 }
25 // Go down the options and search for # in any literal element
26 Object.keys(el.options).forEach(function (id) {
27 var _a;
28 var opt = el.options[id];
29 // If we got a match, we have to split this
30 // and inject a NumberElement in the middle
31 var matchingLiteralElIndex = -1;
32 var literalEl = undefined;
33 for (var i = 0; i < opt.value.length; i++) {
34 var el_1 = opt.value[i];
35 if (types_1.isLiteralElement(el_1) && PLURAL_HASHTAG_REGEX.test(el_1.value)) {
36 matchingLiteralElIndex = i;
37 literalEl = el_1;
38 break;
39 }
40 }
41 if (literalEl) {
42 var newValue = literalEl.value.replace(PLURAL_HASHTAG_REGEX, "$1{" + el.value + ", number}");
43 var newEls = parser_1.pegParse(newValue);
44 (_a = opt.value).splice.apply(_a, __spreadArrays([matchingLiteralElIndex, 1], newEls));
45 }
46 normalizeHashtagInPlural(opt.value);
47 });
48 });
49}
50exports.normalizeHashtagInPlural = normalizeHashtagInPlural;