Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const parse = (locales: string | string[] = []): Entry[] => {
let raw: string[];
if (typeof locales === 'string') {
raw = locales.split(TAG_SEP);
} else {
raw = locales.reduce((a: string[], e: string): string[] => {
const tmp = e.split(TAG_SEP);
return a.concat(tmp);
}, []);
}
const result: Entry[] = [];
const len = raw.length;
for (let i = 0; i < len; i++) {
const id = raw[i].trim();
const tag = parseLanguageTag(id);
// Preserve 'und' undefined locale. If we resolve it, adding
// likely subtags will expand it to 'en-Latn-US'.
if (tag.hasLanguage() || tag.hasScript() || tag.hasRegion()) {
result.push(new Entry(id, LanguageResolver.resolve(tag)));
} else {
// Preserve undefined core fields, but include input's extensions
result.push(new Entry(id, new LanguageTag(
undefined, undefined, undefined, undefined, tag.extensions(), tag.privateUse()
)));
}
}
return result;
};
static removeLikelySubtags(real: string | LanguageTag): LanguageTag {
const tag = typeof real === 'string' ? parseLanguageTag(real) : real;
const max = fastTag(tag);
if (max[Tag.LANGUAGE] === Tag.LANGUAGE || max[Tag.SCRIPT] === Tag.SCRIPT || max[Tag.REGION] === Tag.REGION) {
addLikelySubtags(max);
}
const tmp = UNDEFINED.slice(0);
// Using "en-Latn-US" as an example...
// 1. Match "en-Zzzz-ZZ"
tmp[Tag.LANGUAGE] = max[Tag.LANGUAGE];
let match = tmp.slice(0);
addLikelySubtags(match);
if (fastTagEquals(match, max)) {
return returnTag(tag, tmp);
}
// 2. Match "en-Zzzz-US"
const parseFastTag = (raw: string): FastTag => fastTag(parseLanguageTag(raw));
static resolve(id: string): Locale {
let tag = parseLanguageTag(id);
if (tag.hasLanguage() || tag.hasScript() || tag.hasRegion()) {
tag = LanguageResolver.resolve(tag);
} else {
tag = UNDEFINED;
}
return { id, tag };
}
}
static resolve(real: string | LanguageTag): LanguageTag {
const tag = typeof real === 'string' ? parseLanguageTag(real) : real;
const fast = fastTag(tag);
substituteLanguageAliases(fast);
substituteRegionAliases(fast);
addLikelySubtags(fast);
return returnTag(tag, fast);
}
static addLikelySubtags(real: string | LanguageTag): LanguageTag {
const tag = typeof real === 'string' ? parseLanguageTag(real) : real;
const fast = fastTag(tag);
addLikelySubtags(fast);
return returnTag(tag, fast);
}