Skip to content

Commit 38e0f31

Browse files
committed
Initial commit
0 parents  commit 38e0f31

17 files changed

+6192
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
yarn.lock

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage/

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- lts/dubnium
4+
- node
5+
after_success: bash <(curl -s https://codecov.io/bash)

build.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
'use strict'
2+
3+
var fs = require('fs')
4+
var path = require('path')
5+
var fetch = require('node-fetch')
6+
var fromXml = require('xast-util-from-xml')
7+
var visit = require('unist-util-visit')
8+
var normalize = require('bcp-47/lib/normalize')
9+
10+
var own = {}.hasOwnProperty
11+
12+
var endpoint =
13+
'https://raw.githubusercontent.com/unicode-org/cldr/master/common/supplemental/supplementalMetadata.xml'
14+
15+
fetch(endpoint)
16+
.then(res => res.text())
17+
.then(onbody, console.error)
18+
19+
function onbody(doc) {
20+
var fields = []
21+
var match = []
22+
var defaults = []
23+
var many = {}
24+
var suffix = 'Alias'
25+
var seenHeploc = false
26+
var ignore = [
27+
// Subdivisions (ISO 3166-2) are not used in BCP 47 tags.
28+
'subdivision',
29+
// Timezones.
30+
'zone'
31+
]
32+
33+
visit(fromXml(doc), 'element', onelement)
34+
35+
write('defaults', defaults)
36+
write('fields', fields)
37+
write('many', many)
38+
write('matches', match)
39+
40+
/* eslint-disable-next-line complexity */
41+
function onelement(node) {
42+
var name = node.name
43+
var pos = name.indexOf(suffix)
44+
var from
45+
var to
46+
47+
if (name === 'defaultContent') {
48+
defaults = defaults.concat(clean(node.attributes.locales))
49+
}
50+
51+
if (pos === -1) {
52+
return
53+
}
54+
55+
name = name.slice(0, pos)
56+
57+
if (name === 'territory') {
58+
name = 'region'
59+
}
60+
61+
if (name === 'variant') {
62+
name = 'variants'
63+
}
64+
65+
if (ignore.includes(name)) {
66+
return
67+
}
68+
69+
from = clean(node.attributes.type)
70+
to = clean(node.attributes.replacement)
71+
72+
if (from.length === 1) {
73+
from = from[0]
74+
} else {
75+
throw new Error('Cannot map from many: ' + from)
76+
}
77+
78+
if (to.length === 1) {
79+
to = to[0]
80+
} else {
81+
if (!many[name]) {
82+
many[name] = {}
83+
}
84+
85+
many[name][from] = to
86+
return
87+
}
88+
89+
if (name === 'region' && from.length === 3 && isNaN(from)) {
90+
console.log(
91+
'ISO 3166-1 alpha 3 codes cannot be represented in BCP 47: %s',
92+
from
93+
)
94+
return
95+
}
96+
97+
if (name === 'language') {
98+
if (own.call(normalize, from)) {
99+
console.warn('Ignoring normalized value: %s -> %s', from, to)
100+
return
101+
}
102+
103+
match.push({from, to})
104+
} else if (name === 'variants') {
105+
if (from === 'aaland' && to === 'ax') {
106+
fields.push({
107+
from: {field: name, value: from},
108+
to: {field: 'region', value: to}
109+
})
110+
} else if (from === 'heploc' && to === 'alalc97') {
111+
if (seenHeploc) {
112+
console.warn('Ignoring double heploc alias')
113+
return
114+
}
115+
116+
seenHeploc = true
117+
fields.push({
118+
from: {field: name, value: from},
119+
to: {field: name, value: to}
120+
})
121+
} else if (from === 'polytoni' && to === 'polyton') {
122+
fields.push({
123+
from: {field: name, value: from},
124+
to: {field: name, value: to}
125+
})
126+
} else if (
127+
(from === 'arevela' && to === 'hy') ||
128+
(from === 'arevmda' && to === 'hyw')
129+
) {
130+
fields.push({
131+
from: {field: name, value: from},
132+
to: {field: 'language', value: to}
133+
})
134+
} else {
135+
console.warn('Ignoring unknown variant alias', from, to)
136+
}
137+
} else {
138+
fields.push({
139+
from: {field: name, value: from},
140+
to: {field: name, value: to}
141+
})
142+
}
143+
}
144+
}
145+
146+
function clean(value) {
147+
return value
148+
.toLowerCase()
149+
.replace(/_/g, '-')
150+
.replace(/\s+/g, ' ')
151+
.trim()
152+
.split(' ')
153+
}
154+
155+
function write(name, values) {
156+
fs.writeFileSync(
157+
path.join('lib', name + '.json'),
158+
JSON.stringify(values, null, 2) + '\n'
159+
)
160+
}

funding.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: wooorm

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('./lib')

0 commit comments

Comments
 (0)