-
Notifications
You must be signed in to change notification settings - Fork 917
/
Copy pathgenerate-unicode-script-values.js
59 lines (51 loc) · 1.89 KB
/
generate-unicode-script-values.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
"use strict"
const fs = require("fs")
const path = require("path")
import("../acorn/src/unicode-property-data.js")
.then(m => {
return m.default[13].nonBinary.Script
})
.then(async(reScriptValuesAddedInES) => {
const scriptValues = new Set()
for await (const value of getLatestUnicodeScriptValues()) {
scriptValues.add(value)
}
const scriptValuesAddedInUnicode = "export default " +
JSON.stringify(
[...scriptValues]
// The unicode script values now follow the Unicode spec as of ES2023,
// but prior to ES2022 they were listed in the ES2022 spec.
// The generated file lists all the unicode script values except those listed before ES2022.
.filter(value => !reScriptValuesAddedInES.test(value))
.sort()
.join(" ")
)
writeGeneratedFile("scriptValuesAddedInUnicode", scriptValuesAddedInUnicode)
console.log("Done. The generated files must be committed.")
})
function writeGeneratedFile(filename, content) {
const comment = "// This file was generated by \"bin/" + path.basename(__filename) + "\". Do not modify manually!"
fs.writeFileSync(path.resolve("./acorn/src/generated", filename + ".js"), comment + "\n" + content + "\n", "utf8")
}
/**
* Gets the all unicode script values from the latest PropertyValueAliases.
*/
async function * getLatestUnicodeScriptValues() {
const response = await fetch("https://unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txt")
const lines = (await response.text()).split("\n")
for (const line of lines) {
if (!line || line.startsWith("#")) {
continue
}
const [propertyAlias, alias, canonical, ...remaining] = line
.split("#")[0] // strip comments
.split(";") // split by semicolon
.map((x) => x.trim()) // trim
if (propertyAlias !== "sc") {
continue
}
yield canonical
yield alias
yield * remaining
}
}