-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathutils.js
163 lines (145 loc) · 4.6 KB
/
utils.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
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
163
const fs = require('fs');
const jsesc = require('jsesc');
const pkg = require('../../package.json');
const dependencies = Object.keys(pkg.devDependencies);
const unicodeVersion = dependencies.find((name) => /^@unicode\/unicode-\d/.test(name));
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
const highSurrogate = (codePoint) => Math.floor((codePoint - 0x10000) / 0x400) + 0xD800;
const lowSurrogate = (codePoint) => ((codePoint - 0x10000) % 0x400) + 0xDC00;
const codePointToString = (codePoint) => {
const string = String.fromCodePoint(codePoint);
// Important: escape RegExp meta-characters.
if (/[$()*+\-\./?\[\]^{|}]/.test(string)) {
return `\\${string}`;
}
return string;
};
const createRange = (codePoints) => {
// Does the range contain lone high surrogates?
let isBmpLast = false;
// Does the range contain astral code points?
let hasAstralCodePoints = false;
const bmp = [];
const supplementary = new Map();
for (const codePoint of codePoints) {
if (codePoint >= 0xD800 && codePoint <= 0xDBFF) {
isBmpLast = true;
bmp.push(codePoint);
} else if (codePoint <= 0xFFFF) {
bmp.push(codePoint);
} else { // It’s a supplementary code point.
const hi = highSurrogate(codePoint);
const lo = lowSurrogate(codePoint);
if (supplementary.has(hi)) {
supplementary.get(hi).push(lo);
} else {
supplementary.set(hi, [lo]);
}
hasAstralCodePoints = true;
}
}
const supplementaryByLowRanges = new Map();
for (const [hi, lo] of supplementary) {
const key = createBmpRange(lo);
if (supplementaryByLowRanges.has(key)) {
supplementaryByLowRanges.get(key).push(hi);
} else {
supplementaryByLowRanges.set(key, [hi]);
}
}
// `supplementaryDictByLowRanges` looks like this:
// { 'low surrogate range': [list of high surrogates that have this exact low surrogate range] })
const bmpRange = createBmpRange(bmp, {addBrackets: false});
const buf = [];
let astralRange = '';
// [bmpRange (including orphaned high surrogates), astralRange, isBmpLast]
if (hasAstralCodePoints) {
for (const [lo, hi] of supplementaryByLowRanges) {
buf.push(createBmpRange(hi) + lo);
}
astralRange = buf.join('|');
}
return {
bmp: bmpRange,
astral: astralRange,
isBmpLast: isBmpLast && hasAstralCodePoints
};
};
const createBmpRange = (r, {addBrackets} = {addBrackets: true}) => {
if (r.length === 0) {return '';}
const buf = [];
let [start] = r;
let [end] = r;
let predict = start + 1;
r = r.slice(1);
let counter = 0;
for (const code of r) {
if (predict == code) {
end = code;
predict = code + 1;
continue;
} else {
if (start == end) {
buf.push(codePointToString(start));
counter++;
} else if (end == start + 1) {
buf.push(`${codePointToString(start)}${codePointToString(end)}`);
counter += 2;
} else {
buf.push(`${codePointToString(start)}-${codePointToString(end)}`);
counter += 2;
}
start = code;
end = code;
predict = code + 1;
}
}
if (start == end) {
buf.push(codePointToString(start));
counter++;
} else if (end == start + 1) {
buf.push(`${codePointToString(start)}${codePointToString(end)}`);
counter += 2;
} else {
buf.push(`${codePointToString(start)}-${codePointToString(end)}`);
counter += 2;
}
const output = buf.join('');
if (!addBrackets || counter == 1) {
return output;
}
return `[${output}]`;
};
const assemble = ({name, alias, codePoints}) => {
const {bmp, astral, isBmpLast} = createRange(codePoints);
const result = {name};
if (alias) {
result.alias = alias;
}
if (isBmpLast) {
result.isBmpLast = true;
}
if (bmp) {
result.bmp = bmp;
}
if (astral) {
result.astral = astral;
}
return result;
};
const writeFile = (name, object) => {
console.log(`Saving ${name}…`);
const output = jsesc(object, {
compact: false,
indent: ' '
});
fs.writeFileSync(
`${__dirname}/../output/${name}`,
`module.exports = ${output};\n`
);
};
module.exports = {
assemble,
writeFile,
unicodeVersion
};