-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.js
131 lines (118 loc) · 2.96 KB
/
build.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
// To do: next major: generate one file.
import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
import {dsvFormat} from 'd3-dsv'
import {fetch} from 'undici'
const response = await fetch(
'https://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt'
)
let text = await response.text()
/** @type {Record<string, string>} */
const bTo1 = {}
/** @type {Record<string, string>} */
const tTo1 = {}
/** @type {Record<string, string>} */
const bTo2T = {}
/** @type {Record<string, string>} */
const tTo2B = {}
let index = -1
// BOM.
if (text.codePointAt(0) === 0xfeff) {
text = text.slice(1)
}
const data = dsvFormat('|')
.parse('b|t|i|n\n' + text)
.map(function (d) {
assert(d.n, 'expected name in all languages')
assert(d.b, 'expected bibliographic in all languages')
return {
name: d.n,
iso6392B: d.b,
iso6392T: d.t || undefined,
iso6391: d.i || undefined
}
})
while (++index < data.length) {
const d = data[index]
if (d.iso6391) {
bTo1[d.iso6392B] = d.iso6391
if (d.iso6392T) tTo1[d.iso6392T] = d.iso6391
}
if (d.iso6392T) {
bTo2T[d.iso6392B] = d.iso6392T
tTo2B[d.iso6392T] = d.iso6392B
}
}
await fs.writeFile(
'2.js',
[
'/**',
' * @typedef Language',
' * One language.',
' * @property {string} name',
' * Language name.',
' * @property {string} iso6392B',
' * Bibliographic ISO 639-2 code.',
' * @property {string | undefined} [iso6392T]',
' * Terminologic ISO 639-2 code (if different than `iso6392B`).',
' * @property {string | undefined} [iso6391]',
' * ISO 639-1 code (if available).',
' */',
'',
'/**',
' * List of languages.',
' *',
' * @type {Array<Language>}',
' */',
'export const iso6392 = ' + JSON.stringify(data, undefined, 2),
''
].join('\n')
)
await fs.writeFile(
'2b-to-1.js',
[
'/**',
' * Map of ISO 639-2 bibliographic codes (`dut`) to ISO 639-1 codes (`nl`).',
' *',
' * @type {Record<string, string>}',
' */',
'export const iso6392BTo1 = ' + JSON.stringify(bTo1, undefined, 2),
''
].join('\n')
)
await fs.writeFile(
'2t-to-1.js',
[
'/**',
' * Map of ISO 639-2 terminologic codes (`nld`) to ISO 639-1 codes (`nl`).',
' *',
' * @type {Record<string, string>}',
' */',
'export const iso6392TTo1 = ' + JSON.stringify(tTo1, undefined, 2),
''
].join('\n')
)
await fs.writeFile(
'2t-to-2b.js',
[
'/**',
' * Map of ISO 639-2 terminologic codes (`nld`) to bibliographic codes (`dut`).',
' *',
' * @type {Record<string, string>}',
' */',
'export const iso6392TTo2B = ' + JSON.stringify(tTo2B, undefined, 2),
''
].join('\n')
)
await fs.writeFile(
'2b-to-2t.js',
[
'/**',
' * Map of ISO 639-2 bibliographic codes (`dut`) to terminologic codes (`nld`).',
' *',
' * @type {Record<string, string>}',
' */',
'export const iso6392BTo2T = ' + JSON.stringify(bTo2T, undefined, 2),
''
].join('\n')
)