-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.js
90 lines (82 loc) · 2.28 KB
/
generate.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
const fs = require("fs");
const https = require("https");
const stream = require("stream");
const linesStream = require("@orisano/lines-stream");
const UnicodeTrieBuilder = require("unicode-trie/builder");
const types = require("./types");
function parseLine() {
return new stream.Transform({
decodeStrings: false,
readableObjectMode: true,
transform(line, encoding, callback) {
const body = line.split("#")[0];
if (body.trim().length === 0) {
callback();
return;
}
const [rawRange, type] = body.split(";").map((x) => x.trim());
const range = rawRange.split("..").map((x) => parseInt(x, 16));
if (range.length > 1) {
this.push({ start: range[0], end: range[1], type });
} else {
this.push({ start: range[0], end: range[0], type });
}
callback();
},
});
}
https.get(
"https://www.unicode.org/Public/16.0.0/ucd/auxiliary/GraphemeBreakProperty.txt",
(res) => {
const { statusCode } = res;
if (statusCode !== 200) {
console.error(`failed to request: ${statusCode}`);
res.resume();
return;
}
const trie = new UnicodeTrieBuilder(types.Other);
res
.setEncoding("utf8")
.pipe(linesStream())
.pipe(parseLine())
.on("data", ({ start, end, type }) => {
trie.setRange(start, end, types[type]);
})
.on("end", () => {
fs.writeFileSync(
"./typeTrie.json",
JSON.stringify({
data: trie.toBuffer().toString("base64"),
})
);
});
}
);
https.get(
"https://www.unicode.org/Public/16.0.0/ucd/emoji/emoji-data.txt",
(res) => {
const { statusCode } = res;
if (statusCode !== 200) {
console.error(`failed to request: ${statusCode}`);
res.resume();
return;
}
const trie = new UnicodeTrieBuilder();
res
.setEncoding("utf8")
.pipe(linesStream())
.pipe(parseLine())
.on("data", ({ start, end, type }) => {
if (type === "Extended_Pictographic")
trie.setRange(start, end, types.Extended_Pictographic);
})
.on("end", () => {
fs.writeFileSync(
"./extPict.json",
JSON.stringify({
data: trie.toBuffer().toString("base64"),
})
);
});
}
);