mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
refactor(common): minor refactoring/simplification in generate-locales-tool (#45714)
Make some minor changes in `generate-locales-tool` to make the code simpler and/or avoid unnecessary work. PR Close #45714
This commit is contained in:
committed by
Dylan Hunn
parent
521bfc127c
commit
61b6b4f710
@@ -24,12 +24,18 @@
|
||||
*/
|
||||
export function removeDuplicates(data: unknown[]) {
|
||||
const dedup = [data[0]];
|
||||
let prevStringified = JSON.stringify(data[0]);
|
||||
let nextStringified;
|
||||
|
||||
for (let i = 1; i < data.length; i++) {
|
||||
if (JSON.stringify(data[i]) !== JSON.stringify(data[i - 1])) {
|
||||
nextStringified = JSON.stringify(data[i]);
|
||||
if (nextStringified !== prevStringified) {
|
||||
prevStringified = nextStringified;
|
||||
dedup.push(data[i]);
|
||||
} else {
|
||||
dedup.push(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
return dedup;
|
||||
}
|
||||
|
||||
@@ -138,22 +138,12 @@ ${locale.closureLocaleNames.map(l => `case '${l}':`).join('\n')}
|
||||
}
|
||||
|
||||
function computeEquivalentLocaleNames(localeName: ClosureLibraryLocaleName): string[] {
|
||||
const equivalents = new Set<string>([localeName]);
|
||||
const equivalents = new Set<string>([localeName, formatLocale(localeName)]);
|
||||
|
||||
if (localeName.includes('-')) {
|
||||
equivalents.add(localeName.replace(/-/g, '_'));
|
||||
}
|
||||
|
||||
const aliases = closureLibraryAliases[localeName];
|
||||
if (aliases !== undefined) {
|
||||
aliases.forEach(aliasName => {
|
||||
equivalents.add(aliasName);
|
||||
|
||||
if (aliasName.includes('-')) {
|
||||
equivalents.add(aliasName.replace(/-/g, '_'));
|
||||
}
|
||||
});
|
||||
}
|
||||
closureLibraryAliases[localeName]?.forEach(aliasName => {
|
||||
equivalents.add(aliasName);
|
||||
equivalents.add(formatLocale(aliasName));
|
||||
});
|
||||
|
||||
return Array.from(equivalents);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ export function generateBaseCurrencies(localeData: CldrLocaleData) {
|
||||
const currencies: BaseCurrencies = {};
|
||||
|
||||
Object.keys(currenciesData).forEach(key => {
|
||||
let symbolsArray = [];
|
||||
const symbolsArray: any[] = [];
|
||||
const symbol = currenciesData[key].symbol;
|
||||
const symbolNarrow = currenciesData[key]['symbol-alt-narrow'];
|
||||
if (symbol && symbol !== key) {
|
||||
@@ -53,7 +53,7 @@ export function generateBaseCurrencies(localeData: CldrLocaleData) {
|
||||
if (symbolsArray.length > 0) {
|
||||
symbolsArray.push(symbolNarrow);
|
||||
} else {
|
||||
symbolsArray = [undefined, symbolNarrow];
|
||||
symbolsArray.push(undefined, symbolNarrow);
|
||||
}
|
||||
}
|
||||
if (fractions[key] && fractions[key]['_digits']) {
|
||||
@@ -61,9 +61,9 @@ export function generateBaseCurrencies(localeData: CldrLocaleData) {
|
||||
if (symbolsArray.length === 2) {
|
||||
symbolsArray.push(digits);
|
||||
} else if (symbolsArray.length === 1) {
|
||||
symbolsArray = [...symbolsArray, undefined, digits];
|
||||
symbolsArray.push(undefined, digits);
|
||||
} else {
|
||||
symbolsArray = [undefined, undefined, digits];
|
||||
symbolsArray.push(undefined, undefined, digits);
|
||||
}
|
||||
}
|
||||
if (symbolsArray.length > 0) {
|
||||
|
||||
@@ -25,7 +25,7 @@ export function getPluralFunction(localeData: CldrLocaleData, withTypes = true)
|
||||
|
||||
const numberType = withTypes ? ': number' : '';
|
||||
fn = fn.replace(
|
||||
/function anonymous\(val[^}]+{/g, `function plural(val${numberType})${numberType} {`)
|
||||
/function anonymous\(val[^)]+\)/g, `function plural(val${numberType})${numberType}`)
|
||||
// Since our generated plural functions only take numbers, we can eliminate some of
|
||||
// the logic generated by the `cldr` package (to reduce payload size).
|
||||
.replace(/n\s+=\s+Number\(val\)/, 'n = val')
|
||||
|
||||
Reference in New Issue
Block a user