|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | // Which Unicode version should be used?
|
4 |
| -var pkg = require('../package.json'); |
5 |
| -var dependencies = Object.keys(pkg.devDependencies); |
6 |
| -var unicodeVersion = dependencies.find((name) => /^unicode-\d/.test(name)); |
| 4 | +let pkg = require('../package.json') |
| 5 | +let dependencies = Object.keys(pkg.devDependencies) |
| 6 | +let unicodeVersion = dependencies.find((name) => /^unicode-\d/.test(name)) |
7 | 7 |
|
8 |
| -var start = require(unicodeVersion + '/Binary_Property/ID_Start/code-points.js') |
9 |
| - .filter(function(ch) { return ch > 0x7f; }); |
10 |
| -var last = -1; |
11 |
| -var cont = [0x200c, 0x200d].concat(require(unicodeVersion + '/Binary_Property/ID_Continue/code-points.js') |
12 |
| - .filter(function(ch) { return ch > 0x7f && search(start, ch, last + 1) === -1; })); |
| 8 | +let start = require(unicodeVersion + '/Binary_Property/ID_Start/code-points.js').filter(ch => ch > 0x7f) |
| 9 | +let last = -1 |
| 10 | +let cont = [0x200c, 0x200d].concat(require(unicodeVersion + '/Binary_Property/ID_Continue/code-points.js') |
| 11 | + .filter(ch => ch > 0x7f && search(start, ch, last + 1) === -1)) |
13 | 12 |
|
14 | 13 | function search(arr, ch, starting) {
|
15 |
| - for (var i = starting; arr[i] <= ch && i < arr.length; last = i++) |
16 |
| - if (arr[i] === ch) |
17 |
| - return i; |
18 |
| - return -1; |
19 |
| -} |
20 |
| - |
21 |
| -function pad(str, width) { |
22 |
| - while (str.length < width) str = "0" + str; |
23 |
| - return str; |
| 14 | + for (let i = starting; arr[i] <= ch && i < arr.length; last = i++) |
| 15 | + if (arr[i] === ch) return i |
| 16 | + return -1 |
24 | 17 | }
|
25 | 18 |
|
26 | 19 | function esc(code) {
|
27 |
| - var hex = code.toString(16); |
28 |
| - if (hex.length <= 2) return "\\x" + pad(hex, 2); |
29 |
| - else return "\\u" + pad(hex, 4); |
| 20 | + let hex = code.toString(16) |
| 21 | + return hex.length <= 2 ? hex.padStart(2, "0") : "\\u" + hex.padStart(4, "0") |
30 | 22 | }
|
31 | 23 |
|
32 | 24 | function generate(chars) {
|
33 |
| - var astral = [], re = ""; |
34 |
| - for (var i = 0, at = 0x10000; i < chars.length; i++) { |
35 |
| - var from = chars[i], to = from; |
36 |
| - while (i < chars.length - 1 && chars[i + 1] === to + 1) { |
37 |
| - i++; |
38 |
| - to++; |
39 |
| - } |
| 25 | + let astral = [], re = "" |
| 26 | + for (let i = 0, at = 0x10000; i < chars.length; i++) { |
| 27 | + let from = chars[i], to = from |
| 28 | + while (i < chars.length - 1 && chars[i + 1] === to + 1) {i++; to++} |
40 | 29 | if (to <= 0xffff) {
|
41 |
| - if (from === to) re += esc(from); |
42 |
| - else if (from + 1 === to) re += esc(from) + esc(to); |
43 |
| - else re += esc(from) + "-" + esc(to); |
| 30 | + if (from === to) re += esc(from) |
| 31 | + else if (from + 1 === to) re += esc(from) + esc(to) |
| 32 | + else re += esc(from) + "-" + esc(to) |
44 | 33 | } else {
|
45 |
| - astral.push(from - at, to - from); |
46 |
| - at = to; |
| 34 | + astral.push(from - at, to - from) |
| 35 | + at = to |
47 | 36 | }
|
48 | 37 | }
|
49 |
| - return {nonASCII: re, astral: astral}; |
| 38 | + return {nonASCII: re, astral: astral} |
50 | 39 | }
|
51 | 40 |
|
52 |
| -var startData = generate(start), contData = generate(cont); |
53 |
| - |
54 |
| -console.log("let nonASCIIidentifierStartChars = \"" + startData.nonASCII + "\""); |
55 |
| -console.log("let nonASCIIidentifierChars = \"" + contData.nonASCII + "\""); |
56 |
| -console.log("const astralIdentifierStartCodes = " + JSON.stringify(startData.astral)); |
57 |
| -console.log("const astralIdentifierCodes = " + JSON.stringify(contData.astral)); |
| 41 | +let startData = generate(start), contData = generate(cont) |
| 42 | + |
| 43 | +let code = [ |
| 44 | + ` let nonASCIIidentifierStartChars = "${startData.nonASCII}"`, |
| 45 | + ` let nonASCIIidentifierChars = "${contData.nonASCII}"`, |
| 46 | + ` const astralIdentifierStartCodes = ${JSON.stringify(startData.astral)}`, |
| 47 | + ` const astralIdentifierCodes = ${JSON.stringify(contData.astral)}` |
| 48 | +] |
| 49 | + |
| 50 | +if (process.argv.length != 3) { |
| 51 | + console.log(code.join("\n")) |
| 52 | +} else { |
| 53 | + let {readFile} = require('fs') |
| 54 | + readFile(process.argv[2], "utf8", function(err, data) { |
| 55 | + if (err) throw err |
| 56 | + for (let line of code) |
| 57 | + data = data.replace(new RegExp(/.* = /.exec(line)[0] + ".*"), line) |
| 58 | + process.stdout.write(data) |
| 59 | + }) |
| 60 | +} |
0 commit comments