forked from adobe/react-spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateAllPlurals.mjs
162 lines (126 loc) · 4.28 KB
/
generateAllPlurals.mjs
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
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* Scrapes data on CLDR https://www.unicode.org/cldr/charts/44/supplemental/language_plural_rules.html#comparison
* and generates a list of all possible values needed between all locales for plural rules.
* It is used by our NumberParser to generate all literal strings, ex units 1 foot, 2 feet, but other locales have more than 2 forms.
*/
import {JSDOM} from 'jsdom';
function getRange(row) {
let range = [];
let th = row.firstElementChild;
do {
const { textContent } = th;
let [start, end = start] = textContent.split('-');
if (start === '') {
range.push([]);
} else {
start = start.replace(/\.x$/, '.1');
end = end.replace(/\.x$/, '.1');
range.push([Number(start), Number(end)]);
}
} while ((th = th.nextElementSibling));
return range;
}
function getLocales(cell) {
let locales = [];
let element = cell.firstElementChild;
do {
if (element.tagName === 'SPAN') {
locales.push(element.title);
}
} while ((element = element.nextElementSibling));
return locales;
}
function getPlurals(tr, range) {
let columnTitles = [...tr.childNodes].map((td) => td.title);
let rules = Object.fromEntries(columnTitles.map((key) => [key, []]));
let td = tr.firstElementChild;
let index = 1;
do {
const category = td.title;
let columns = td.hasAttribute('colspan')
? Number(td.getAttribute('colspan'))
: 1;
do {
rules[category].push(range[index]);
index++;
} while (columns-- > 1);
} while ((td = td.nextElementSibling));
return rules;
}
function extractTable(dom, integerTable, fractionTable) {
function extract(table) {
const tbody = table.querySelector('tbody');
let tr = tbody.firstElementChild;
let current;
let results = {};
do {
if (tr.firstElementChild.tagName === 'TH') {
if (current) {
for (const language of current.languages) {
if (!results[language]) {
results[language] = Object.fromEntries(
Object.keys(current.rules).map((key) => [key, []])
);
}
for (let rule in current.rules) {
results[language][rule] = [].concat(
results[language][rule],
current.rules[rule]
);
}
}
}
current = {
range: [],
languages: [],
rules: {},
};
current.range = getRange(tr);
} else if (
tr.children[1] instanceof dom.window.HTMLTableCellElement &&
tr.children[1].classList.contains('l')
) {
current.languages = getLocales(tr.children[1]);
} else {
current.rules = getPlurals(tr, current.range);
}
} while ((tr = tr.nextElementSibling));
return results;
}
const integer = extract(integerTable);
const fraction = extract(fractionTable);
let values = new Set();
for (let language in integer) {
for (let rule in integer[language]) {
if (integer[language][rule].length > 1) {
values.add(integer[language][rule][0][0]);
}
}
}
for (let language in fraction) {
for (let rule in fraction[language]) {
if (fraction[language][rule].length > 1) {
values.add(fraction[language][rule][0][0]);
}
}
}
return Array.from(values);
}
fetch('https://www.unicode.org/cldr/charts/44/supplemental/language_plural_rules.html#comparison')
.then(async (response) => {
let data = await response.text();
const dom = new JSDOM(data);
const [integerTable, fractionTable] = dom.window.document.querySelectorAll('.pluralComp');
let values = extractTable(dom, integerTable, fractionTable);
console.log(values);
});