forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_plural_rules.js
174 lines (141 loc) · 3.91 KB
/
gen_plural_rules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const cldr = require('cldr');
// locale list
const locales = cldr.localeIds;
const langToRule = {};
const ruleToLang = {};
const variants = [];
const localeToVariant = {};
const DEFAULT_RULE = `function anonymous(n\n/**/) {\nreturn"other"\n}`;
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);
});
var nextVariantCode = 'a'.charCodeAt(0);
variants.forEach(locale => {
const rule = normalizeRule(cldr.extractPluralRuleFunction(locale).toString());
if (!rule) {
return;
}
var mapTo = null;
if (ruleToLang[rule]) {
mapTo = ruleToLang[rule][0];
localeToVariant[locale] = mapTo;
return;
}
if (!mapTo) {
mapTo = '_' + String.fromCharCode(nextVariantCode++);
langToRule[mapTo] = rule;
ruleToLang[rule] = [mapTo];
localeToVariant[locale] = mapTo;
}
});
console.log(generateCode());
function generateCode() {
checkMapping();
return `
// This is generated code DO NOT MODIFY
// see angular2/script/cldr/gen_plural_rules.js
enum Plural {
Zero,
One,
Two,
Few,
Many,
Other
}
function getPluralCase(locale: string, n: number|string): Plural {
` + generateVars() +
generateRules() + `
}`;
}
function generateRules() {
const codeParts = [`
const lang = locale.split('_')[0].toLowerCase();
switch (lang) {`];
Object.keys(ruleToLang).forEach(rule => {
const langs = ruleToLang[rule];
codeParts.push(...langs.map(l => ` case '${l}': `));
codeParts.push(` ${rule}`);
});
codeParts.push(` default:
return Plural.Other;
}`);
return codeParts.join('\n');
}
function generateVars(){
return `
function getPluralCase(locale: string, nLike: number | string): Plural {
// TODO(vicb): lazy compute
if (typeof nLike === 'string') {
nLike = parseInt(<string>nLike, 10);
}
const n: number = nLike as number;
const nDecimal = n.toString().replace(/^[^.]*\\.?/, "");
const i = Math.floor(Math.abs(n));
const v = nDecimal.length;
const f = parseInt(nDecimal, 10);
const t = parseInt(n.toString().replace(/^[^.]*\\.?|0+$/g,""), 10) || 0;
`;
}
function checkMapping() {
if (localeToVariant.length) {
console.log(`Mapping required:`);
console.log(localeToVariant);
throw new Error('not implemented');
}
}
/**
* If the language rule do not match an existing language rule, flag it as variant and handle it at the end
*/
function getVariantLang(locale, rule) {
var lang = locale.split('_')[0];
if (!langToRule[lang]) {
langToRule[lang] = rule;
return lang;
}
if (langToRule[lang] === rule) {
return lang;
}
variants.push(locale);
return null;
}
function normalizeRule(fn) {
if (fn === DEFAULT_RULE) return;
return fn
.replace(toRegExp('function anonymous(n\n/**/) {\n'), '')
.replace(toRegExp('var'), 'let')
.replace(toRegExp('"zero"'), ' Plural.Zero')
.replace(toRegExp('"one"'), ' Plural.One')
.replace(toRegExp('"two"'), ' Plural.Two')
.replace(toRegExp('"few"'), ' Plural.Few')
.replace(toRegExp('"many"'), ' Plural.Many')
.replace(toRegExp('"other"'), ' Plural.Other')
.replace(toRegExp('\n}'), '')
.replace(toRegExp('let'), '')
.replace(toRegExp('if(typeof n==="string")n=parseInt(n,10);'), '')
.replace(toRegExp('i=Math.floor(Math.abs(n))'), '')
.replace(/v=n.toString.*?.length/g, '')
.replace(/f=parseInt.*?\|\|0/g, '')
.replace(/t=parseInt.*?\|\|0/g, '')
.replace(/^[ ,;]*/, '')
+ ';';
}
function toRegExp(s) {
return new RegExp(s.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'), 'g');
}