Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
// Note: run `npm install unicode-7.0.0` first.
2
3
// Which Unicode version should be used?
4
var version = '7.0.0';
5
6
var start = require('unicode-' + version + '/properties/ID_Start/code-points')
7
.filter(function(ch) { return ch > 127; });
8
var cont = [0x200c, 0x200d].concat(require('unicode-' + version + '/properties/ID_Continue/code-points')
9
.filter(function(ch) { return ch > 127 && start.indexOf(ch) == -1; }));
10
11
function pad(str, width) {
12
while (str.length < width) str = "0" + str;
13
return str;
14
}
15
16
function esc(code) {
17
var hex = code.toString(16);
18
if (hex.length <= 2) return "\\x" + pad(hex, 2);
19
else return "\\u" + pad(hex, 4);
20
}
21
22
function generate(chars) {
23
var astral = [], re = "";
24
for (var i = 0, at = 0x10000; i < chars.length; i++) {
25
var from = chars[i], to = from;
26
while (i < chars.length - 1 && chars[i + 1] == to + 1) {
27
i++;
28
to++;
29
}
30
if (to <= 0xffff) {
31
if (from == to) re += esc(from);
32
else if (from + 1 == to) re += esc(from) + esc(to);
33
else re += esc(from) + "-" + esc(to);
34
} else {
35
astral.push(from - at, to - from);
36
at = to;
37
}
38
}
39
return {nonASCII: re, astral: astral};
40
}
41
42
var startData = generate(start), contData = generate(cont);
43
44
console.log(" var nonASCIIidentifierStartChars = \"" + startData.nonASCII + "\";");
45
console.log(" var nonASCIIidentifierChars = \"" + contData.nonASCII + "\";");
46
console.log(" var astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";");
47
console.log(" var astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";");
48
49