-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_idna2008_map.ts
40 lines (35 loc) · 1.17 KB
/
create_idna2008_map.ts
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
import fetch from 'node-fetch';
import * as fs from 'fs';
function parseCodePoints(str: string) {
return str.split('..').map(codePointStr => parseInt(codePointStr, 16));
}
function parseMapping(str: string) {
return str.split(' ').map(codePointStr => parseInt(codePointStr, 16));
}
function parseLine(line: string, mappings: { [key: number]: number[] }) {
const elems: string[] = line
.replace(/#.*$/, '')
.split(';')
.map(elem => elem.trim());
if (elems[1] !== 'mapped') return null;
const codePoints = parseCodePoints(elems[0]);
const mapping = parseMapping(elems[2]);
for (let codePoint of codePoints) {
mappings[codePoint] = mapping;
}
return mapping;
}
(async function() {
const text = await fetch(
'https://www.unicode.org/Public/idna/latest/IdnaMappingTable.txt'
).then(res => res.text());
const lines = text
.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#'));
const mappings = {};
for (let line of lines) {
parseLine(line, mappings);
}
fs.writeFileSync('./data/idna2008_map.json', JSON.stringify(mappings));
})();