Skip to content

Commit

Permalink
build: reformat (clang) extract.js (angular#33523)
Browse files Browse the repository at this point in the history
PR Close angular#33523
  • Loading branch information
petebacondarwin authored and mohaxspb committed Nov 7, 2019
1 parent ecda882 commit 320bc2c
Showing 1 changed file with 80 additions and 80 deletions.
160 changes: 80 additions & 80 deletions tools/gulp-tasks/cldr/extract.js
Expand Up @@ -23,7 +23,8 @@ const I18N_DATA_EXTRA_FOLDER = `${I18N_DATA_FOLDER}/extra`;
const RELATIVE_I18N_FOLDER = path.resolve(__dirname, `../../../${I18N_FOLDER}`);
const RELATIVE_I18N_CORE_FOLDER = path.resolve(__dirname, `../../../${I18N_CORE_FOLDER}`);
const RELATIVE_I18N_DATA_FOLDER = path.resolve(__dirname, `../../../${I18N_DATA_FOLDER}`);
const RELATIVE_I18N_DATA_EXTRA_FOLDER = path.resolve(__dirname, `../../../${I18N_DATA_EXTRA_FOLDER}`);
const RELATIVE_I18N_DATA_EXTRA_FOLDER =
path.resolve(__dirname, `../../../${I18N_DATA_EXTRA_FOLDER}`);
const DEFAULT_RULE = 'function anonymous(n\n/*``*/) {\nreturn"other"\n}';
const EMPTY_RULE = 'function anonymous(n\n/*``*/) {\n\n}';
const WEEK_DAYS = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
Expand Down Expand Up @@ -72,40 +73,44 @@ module.exports = (gulp, done) => {

console.log(`${index + 1}/${LOCALES.length}`);
console.log(`\t${I18N_DATA_FOLDER}/${locale}.ts`);
fs.writeFileSync(`${RELATIVE_I18N_DATA_FOLDER}/${locale}.ts`, locale === 'en'? localeEnFile : generateLocale(locale, localeData, baseCurrencies));
fs.writeFileSync(
`${RELATIVE_I18N_DATA_FOLDER}/${locale}.ts`,
locale === 'en' ? localeEnFile : generateLocale(locale, localeData, baseCurrencies));
console.log(`\t${I18N_DATA_EXTRA_FOLDER}/${locale}.ts`);
fs.writeFileSync(`${RELATIVE_I18N_DATA_EXTRA_FOLDER}/${locale}.ts`, generateLocaleExtra(locale, localeData));
fs.writeFileSync(
`${RELATIVE_I18N_DATA_EXTRA_FOLDER}/${locale}.ts`, generateLocaleExtra(locale, localeData));
});
console.log(`${LOCALES.length} locale files generated.`);

console.log(`All i18n cldr files have been generated, formatting files..."`);
const format = require('gulp-clang-format');
const clangFormat = require('clang-format');
return gulp
.src([
`${I18N_DATA_FOLDER}/**/*.ts`,
`${I18N_FOLDER}/currencies.ts`,
`${I18N_CORE_FOLDER}/locale_en.ts`
], {base: '.'})
.pipe(format.format('file', clangFormat))
.pipe(gulp.dest('.'));
.src(
[
`${I18N_DATA_FOLDER}/**/*.ts`, `${I18N_FOLDER}/currencies.ts`,
`${I18N_CORE_FOLDER}/locale_en.ts`
],
{base: '.'})
.pipe(format.format('file', clangFormat))
.pipe(gulp.dest('.'));
};

/**
* Generate file that contains basic locale data
*/
function generateLocale(locale, localeData, baseCurrencies) {
// [ localeId, dateTime, number, currency, pluralCase ]
let data = stringify([
locale,
...getDateTimeTranslations(localeData),
...getDateTimeSettings(localeData),
...getNumberSettings(localeData),
...getCurrencySettings(locale, localeData),
generateLocaleCurrencies(localeData, baseCurrencies)
], true)
// We remove "undefined" added by spreading arrays when there is no value
.replace(/undefined/g, 'u');
let data =
stringify(
[
locale, ...getDateTimeTranslations(localeData), ...getDateTimeSettings(localeData),
...getNumberSettings(localeData), ...getCurrencySettings(locale, localeData),
generateLocaleCurrencies(localeData, baseCurrencies)
],
true)
// We remove "undefined" added by spreading arrays when there is no value
.replace(/undefined/g, 'u');

// adding plural function after, because we don't want it as a string
data = data.substring(0, data.lastIndexOf(']')) + `, plural]`;
Expand All @@ -120,7 +125,8 @@ export default ${data};
}

/**
* Generate a file that contains extra data (for now: day period rules, and extended day period data)
* Generate a file that contains extra data
* (for now: day period rules, and extended day period data)
*/
function generateLocaleExtra(locale, localeData) {
const dayPeriods = getDayPeriodsNoAmPm(localeData);
Expand All @@ -136,8 +142,7 @@ function generateLocaleExtra(locale, localeData) {
}

const dayPeriodsFormat = removeDuplicates([
objectValues(dayPeriods.format.narrow),
objectValues(dayPeriods.format.abbreviated),
objectValues(dayPeriods.format.narrow), objectValues(dayPeriods.format.abbreviated),
objectValues(dayPeriods.format.wide)
]);

Expand Down Expand Up @@ -243,13 +248,16 @@ export const CURRENCIES_EN: {[code: string]: CurrenciesSymbols | [string | undef

/**
* Returns data for the chosen day periods
* @returns {format: {narrow / abbreviated / wide: [...]}, stand-alone: {narrow / abbreviated / wide: [...]}}
* @returns {
* format: {narrow / abbreviated / wide: [...]},
* stand-alone: {narrow / abbreviated / wide: [...]}
* }
*/
function getDayPeriods(localeData, dayPeriodsList) {
const dayPeriods = localeData.main(`dates/calendars/gregorian/dayPeriods`);
const result = {};
// cleaning up unused keys
Object.keys(dayPeriods).forEach(key1 => { // format / stand-alone
Object.keys(dayPeriods).forEach(key1 => { // format / stand-alone
result[key1] = {};
Object.keys(dayPeriods[key1]).forEach(key2 => { // narrow / abbreviated / wide
result[key1][key2] = {};
Expand All @@ -275,13 +283,16 @@ function getDayPeriodsAmPm(localeData) {
* Returns the extra day periods (without am/pm)
*/
function getDayPeriodsNoAmPm(localeData) {
return getDayPeriods(localeData, ['noon', 'midnight', 'morning1', 'morning2', 'afternoon1',
'afternoon2', 'evening1', 'evening2', 'night1', 'night2']);
return getDayPeriods(localeData, [
'noon', 'midnight', 'morning1', 'morning2', 'afternoon1', 'afternoon2', 'evening1', 'evening2',
'night1', 'night2'
]);
}

/**
* Returns date-related translations for a locale
* @returns [ dayPeriodsFormat, dayPeriodsStandalone, daysFormat, dayStandalone, monthsFormat, monthsStandalone, eras ]
* @returns [ dayPeriodsFormat, dayPeriodsStandalone, daysFormat, dayStandalone, monthsFormat,
* monthsStandalone, eras ]
* each value: [ narrow, abbreviated, wide, short? ]
*/
function getDateTimeTranslations(localeData) {
Expand All @@ -291,8 +302,7 @@ function getDateTimeTranslations(localeData) {
const dayPeriods = getDayPeriodsAmPm(localeData);

const dayPeriodsFormat = removeDuplicates([
objectValues(dayPeriods.format.narrow),
objectValues(dayPeriods.format.abbreviated),
objectValues(dayPeriods.format.narrow), objectValues(dayPeriods.format.abbreviated),
objectValues(dayPeriods.format.wide)
]);

Expand All @@ -303,22 +313,17 @@ function getDateTimeTranslations(localeData) {
]);

const daysFormat = removeDuplicates([
objectValues(dayNames.format.narrow),
objectValues(dayNames.format.abbreviated),
objectValues(dayNames.format.wide),
objectValues(dayNames.format.short)
objectValues(dayNames.format.narrow), objectValues(dayNames.format.abbreviated),
objectValues(dayNames.format.wide), objectValues(dayNames.format.short)
]);

const daysStandalone = removeDuplicates([
objectValues(dayNames['stand-alone'].narrow),
objectValues(dayNames['stand-alone'].abbreviated),
objectValues(dayNames['stand-alone'].wide),
objectValues(dayNames['stand-alone'].short)
objectValues(dayNames['stand-alone'].narrow), objectValues(dayNames['stand-alone'].abbreviated),
objectValues(dayNames['stand-alone'].wide), objectValues(dayNames['stand-alone'].short)
]);

const monthsFormat = removeDuplicates([
objectValues(monthNames.format.narrow),
objectValues(monthNames.format.abbreviated),
objectValues(monthNames.format.narrow), objectValues(monthNames.format.abbreviated),
objectValues(monthNames.format.wide)
]);

Expand All @@ -337,8 +342,7 @@ function getDateTimeTranslations(localeData) {
const dateTimeTranslations = [
...removeDuplicates([dayPeriodsFormat, dayPeriodsStandalone]),
...removeDuplicates([daysFormat, daysStandalone]),
...removeDuplicates([monthsFormat, monthsStandalone]),
eras
...removeDuplicates([monthsFormat, monthsStandalone]), eras
];

return dateTimeTranslations;
Expand All @@ -352,30 +356,25 @@ function getDateTimeTranslations(localeData) {
function getDateTimeFormats(localeData) {
function getFormats(data) {
return removeDuplicates([
data.short._value || data.short,
data.medium._value || data.medium,
data.long._value || data.long,
data.full._value || data.full
data.short._value || data.short, data.medium._value || data.medium,
data.long._value || data.long, data.full._value || data.full
]);
}

const dateFormats = localeData.main('dates/calendars/gregorian/dateFormats');
const timeFormats = localeData.main('dates/calendars/gregorian/timeFormats');
const dateTimeFormats = localeData.main('dates/calendars/gregorian/dateTimeFormats');

return [
getFormats(dateFormats),
getFormats(timeFormats),
getFormats(dateTimeFormats)
];
return [getFormats(dateFormats), getFormats(timeFormats), getFormats(dateTimeFormats)];
}

/**
* Returns day period rules for a locale
* @returns string[]
*/
function getDayPeriodRules(localeData) {
const dayPeriodRules = localeData.get(`supplemental/dayPeriodRuleSet/${localeData.attributes.language}`);
const dayPeriodRules =
localeData.get(`supplemental/dayPeriodRuleSet/${localeData.attributes.language}`);
const rules = {};
if (dayPeriodRules) {
Object.keys(dayPeriodRules).forEach(key => {
Expand Down Expand Up @@ -404,11 +403,11 @@ function getFirstDayOfWeek(localeData) {
*/
function getWeekendRange(localeData) {
const startDay =
localeData.get(`supplemental/weekData/weekendStart/${localeData.attributes.territory}`) ||
localeData.get('supplemental/weekData/weekendStart/001');
localeData.get(`supplemental/weekData/weekendStart/${localeData.attributes.territory}`) ||
localeData.get('supplemental/weekData/weekendStart/001');
const endDay =
localeData.get(`supplemental/weekData/weekendEnd/${localeData.attributes.territory}`) ||
localeData.get('supplemental/weekData/weekendEnd/001');
localeData.get(`supplemental/weekData/weekendEnd/${localeData.attributes.territory}`) ||
localeData.get('supplemental/weekData/weekendEnd/001');
return [WEEK_DAYS.indexOf(startDay), WEEK_DAYS.indexOf(endDay)];
}

Expand All @@ -417,13 +416,17 @@ function getWeekendRange(localeData) {
* @returns [ firstDayOfWeek, weekendRange, formats ]
*/
function getDateTimeSettings(localeData) {
return [getFirstDayOfWeek(localeData), getWeekendRange(localeData), ...getDateTimeFormats(localeData)];
return [
getFirstDayOfWeek(localeData), getWeekendRange(localeData), ...getDateTimeFormats(localeData)
];
}

/**
* Returns the number symbols and formats for a locale
* @returns [ symbols, formats ]
* symbols: [ decimal, group, list, percentSign, plusSign, minusSign, exponential, superscriptingExponent, perMille, infinity, nan, timeSeparator, currencyDecimal?, currencyGroup? ]
* symbols: [ decimal, group, list, percentSign, plusSign, minusSign, exponential,
* superscriptingExponent, perMille, infinity, nan, timeSeparator, currencyDecimal?, currencyGroup?
* ]
* formats: [ currency, decimal, percent, scientific ]
*/
function getNumberSettings(localeData) {
Expand Down Expand Up @@ -455,10 +458,7 @@ function getNumberSettings(localeData) {
symbolValues.push(symbols.currencyGroup);
}

return [
symbolValues,
[decimalFormat, percentFormat, currencyFormat, scientificFormat]
];
return [symbolValues, [decimalFormat, percentFormat, currencyFormat, scientificFormat]];
}

/**
Expand All @@ -471,8 +471,9 @@ function getCurrencySettings(locale, localeData) {

// find the currency currently used in this country
const currencies =
localeData.get(`supplemental/currencyData/region/${localeData.attributes.territory}`) ||
localeData.get(`supplemental/currencyData/region/${localeData.attributes.language.toUpperCase()}`);
localeData.get(`supplemental/currencyData/region/${localeData.attributes.territory}`) ||
localeData.get(
`supplemental/currencyData/region/${localeData.attributes.language.toUpperCase()}`);

if (currencies) {
currencies.some(currency => {
Expand All @@ -492,7 +493,8 @@ function getCurrencySettings(locale, localeData) {
let currencySettings = [undefined, undefined];

if (currentCurrency) {
currencySettings = [currencyInfo[currentCurrency].symbol, currencyInfo[currentCurrency].displayName];
currencySettings =
[currencyInfo[currentCurrency].symbol, currencyInfo[currentCurrency].displayName];
}

return currencySettings;
Expand All @@ -517,23 +519,21 @@ function getPluralFunction(locale) {
fn = DEFAULT_RULE;
}

fn = fn
.replace(
toRegExp('function anonymous(n\n/*``*/) {\n'),
'function plural(n: number): number {\n ')
.replace(toRegExp('var'), 'let')
.replace(toRegExp('if(typeof n==="string")n=parseInt(n,10);'), '')
.replace(toRegExp('\n}'), ';\n}');
fn = fn.replace(
toRegExp('function anonymous(n\n/*``*/) {\n'),
'function plural(n: number): number {\n ')
.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')
.replace(toRegExp('"few"'), ' 3')
.replace(toRegExp('"many"'), ' 4')
.replace(toRegExp('"other"'), ' 5');
return fn.replace(toRegExp('"zero"'), ' 0')
.replace(toRegExp('"one"'), ' 1')
.replace(toRegExp('"two"'), ' 2')
.replace(toRegExp('"few"'), ' 3')
.replace(toRegExp('"many"'), ' 4')
.replace(toRegExp('"other"'), ' 5');
}

/**
Expand Down Expand Up @@ -562,7 +562,7 @@ function objectValues(obj) {
*/
function removeDuplicates(data) {
const dedup = [data[0]];
for(let i = 1; i < data.length; i++) {
for (let i = 1; i < data.length; i++) {
if (stringify(data[i]) !== stringify(data[i - 1])) {
dedup.push(data[i]);
} else {
Expand Down

0 comments on commit 320bc2c

Please sign in to comment.