UNPKG

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