UNPKG

1.8 kBJavaScriptView Raw
1"use strict";
2
3// Which Unicode version should be used?
4const version = "9.0.0";
5
6const start = require("unicode-" + version + "/Binary_Property/ID_Start/code-points.js")
7 .filter(function(ch) { return ch > 0x7f; });
8let last = -1;
9const cont = [0x200c, 0x200d].concat(
10 require("unicode-" + version + "/Binary_Property/ID_Continue/code-points.js")
11 .filter(function(ch) {
12 return ch > 0x7f && search(start, ch, last + 1) == -1;
13 })
14 );
15
16function search(arr, ch, starting) {
17 for (let i = starting; arr[i] <= ch && i < arr.length; last = i++)
18 if (arr[i] === ch)
19 return i;
20 return -1;
21}
22
23function pad(str, width) {
24 while (str.length < width) str = "0" + str;
25 return str;
26}
27
28function esc(code) {
29 const hex = code.toString(16);
30 if (hex.length <= 2) return "\\x" + pad(hex, 2);
31 else return "\\u" + pad(hex, 4);
32}
33
34function generate(chars) {
35 const astral = [];
36 let re = "";
37 for (let i = 0, at = 0x10000; i < chars.length; i++) {
38 const from = chars[i];
39 let to = from;
40 while (i < chars.length - 1 && chars[i + 1] == to + 1) {
41 i++;
42 to++;
43 }
44 if (to <= 0xffff) {
45 if (from == to) re += esc(from);
46 else if (from + 1 == to) re += esc(from) + esc(to);
47 else re += esc(from) + "-" + esc(to);
48 } else {
49 astral.push(from - at, to - from);
50 at = to;
51 }
52 }
53 return { nonASCII: re, astral: astral };
54}
55
56const startData = generate(start);
57const contData = generate(cont);
58
59console.log("let nonASCIIidentifierStartChars = \"" + startData.nonASCII + "\";");
60console.log("let nonASCIIidentifierChars = \"" + contData.nonASCII + "\";");
61console.log("const astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";");
62console.log("const astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";");