Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getPluralFunction(locale, withTypes = true) {
let fn = cldr.extractPluralRuleFunction(locale).toString();
if (fn === EMPTY_RULE) {
fn = DEFAULT_RULE;
}
const numberType = withTypes ? ': number' : '';
fn = fn.replace(/function anonymous\(n[^}]+{/g, `function plural(n${numberType})${numberType} {`)
.replace(toRegExp('var'), 'let')
.replace(toRegExp('if(typeof n==="string")n=parseInt(n,10);'), '')
.replace(toRegExp('\n}'), ';\n}');
// The replacement values must match the `Plural` enum from common.
// We do not use the enum directly to avoid depending on that package.
return fn.replace(toRegExp('"zero"'), ' 0')
.replace(toRegExp('"one"'), ' 1')
.replace(toRegExp('"two"'), ' 2')
function extractPluralRuleFunction(locale) {
assert(isValidLocale(locale), 'Invalid locale');
var fn = cldr.extractPluralRuleFunction(normalize(locale)).toString();
// Remove unnecessary function name.
fn = fn.replace('function anonymous(', 'function (');
// ParseInt() is expensive given that we already know the input is a number.
fn = fn.replace('if(typeof n==="string")n=parseInt(n,10);', 'n=Math.floor(n);');
// js-hint asi.
fn = fn.replace('"\n}', '";\n}');
// js-hint W018 "Confusing use of '!'" caused by stuff like "!(n===11)".
fn = fn.replace(/!\((\w+)===(\d+)\)/g, '($1!==$2)');
// js-hint W018 "Confusing use of '!'" caused by stuff like "!(n%100===11)".
fn = fn.replace(/!\((\w+)%(\d+)===(\d+)\)/g, '($1%$2!==$3)');
// Keep it neat.
fn = fn.replace(/\n/g, '');
extractPluralRuleFunction: function(locale) {
var fn = cldr.extractPluralRuleFunction(locale).toString();
// Remove unnecessary function name.
fn = fn.replace('function anonymous(', 'function (');
// ParseInt() is expensive given that we already know the input is a number.
fn = fn.replace('if(typeof n==="string")n=parseInt(n,10);', 'n=Math.floor(n);');
// js-hint asi.
fn = fn.replace('"\n}', '";\n}');
// js-hint W018 "Confusing use of '!'" caused by stuff like "!(n===11)".
fn = fn.replace(/!\((\w+)===(\d+)\)/g, '($1!==$2)');
// js-hint W018 "Confusing use of '!'" caused by stuff like "!(n%100===11)".
fn = fn.replace(/!\((\w+)%(\d+)===(\d+)\)/g, '($1%$2!==$3)');
// Keep it neat.
fn = fn.replace(/\n/g, '');
extractPluralRuleFunction: function(locale) {
var fn = cldr.extractPluralRuleFunction(locale).toString();
// Remove unnecessary function name.
fn = fn.replace('function anonymous(', 'function (');
// ParseInt() is expensive given that we already know the input is a number.
fn = fn.replace('if(typeof n==="string")n=parseInt(n,10);', 'n=Math.floor(n);');
// js-hint asi.
fn = fn.replace('"\n}', '";\n}');
// js-hint W018 "Confusing use of '!'" caused by stuff like "!(n===11)".
fn = fn.replace(/!\((\w+)===(\d+)\)/g, '($1!==$2)');
// js-hint W018 "Confusing use of '!'" caused by stuff like "!(n%100===11)".
fn = fn.replace(/!\((\w+)%(\d+)===(\d+)\)/g, '($1%$2!==$3)');
// Keep it neat.
fn = fn.replace(/\n/g, '');
locales.forEach(locale => {
const rule = normalizeRule(cldr.extractPluralRuleFunction(locale).toString());
const lang = getVariantLang(locale, rule);
if (!lang || !rule) {
return;
}
if (!ruleToLang[rule]) {
ruleToLang[rule] = [];
} else if (ruleToLang[rule].indexOf(lang) > -1) {
return;
}
ruleToLang[rule].push(lang);
});