UNPKG

intl-messageformat-parser

Version:

Parses ICU Message strings into an AST via JavaScript.

46 lines (45 loc) 1.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeHashtagInPlural = void 0; var tslib_1 = require("tslib"); var types_1 = require("./types"); var parser_1 = require("./parser"); var PLURAL_HASHTAG_REGEX = /(^|[^\\])#/g; /** * Whether to convert `#` in plural rule options * to `{var, number}` * @param el AST Element * @param pluralStack current plural stack */ function normalizeHashtagInPlural(els) { els.forEach(function (el) { // If we're encountering a plural el if (!types_1.isPluralElement(el) && !types_1.isSelectElement(el)) { return; } // Go down the options and search for # in any literal element Object.keys(el.options).forEach(function (id) { var _a; var opt = el.options[id]; // If we got a match, we have to split this // and inject a NumberElement in the middle var matchingLiteralElIndex = -1; var literalEl = undefined; for (var i = 0; i < opt.value.length; i++) { var el_1 = opt.value[i]; if (types_1.isLiteralElement(el_1) && PLURAL_HASHTAG_REGEX.test(el_1.value)) { matchingLiteralElIndex = i; literalEl = el_1; break; } } if (literalEl) { var newValue = literalEl.value.replace(PLURAL_HASHTAG_REGEX, "$1{" + el.value + ", number}"); var newEls = parser_1.pegParse(newValue); (_a = opt.value).splice.apply(_a, tslib_1.__spreadArray([matchingLiteralElIndex, 1], newEls)); } normalizeHashtagInPlural(opt.value); }); }); } exports.normalizeHashtagInPlural = normalizeHashtagInPlural;