generated from justjavac/deno_starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.js
executable file
·125 lines (108 loc) · 3.73 KB
/
generate.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
#!/usr/bin/env node
// @ts-nocheck
/// base on https://github.com/tdanecker/node-unicodetable/blob/be4f18ea740415309fbcb43d31a11332fed36530/generate.js
//
var fs = require('fs'),
path = require('path'),
http = require('http'),
BufferStream = require('bufferstream'),
// http://www.ksu.ru/eng/departments/ktk/test/perl/lib/unicode/UCDFF301.html
keys = ['value', 'name', 'category', 'class',
'bidirectional_category', 'mapping', 'decimal_digit_value', 'digit_value',
'numeric_value', 'mirrored', 'unicode_name', 'comment', 'uppercase_mapping',
'lowercase_mapping', 'titlecase_mapping'],
systemfiles = [
"UnicodeData.txt"
],
refs = 0;
// based on https://github.com/mathiasbynens/jsesc
function escape(charValue) {
var hexadecimal = charValue.replace(/^0*/, ''); // is already in hexadecimal
var longhand = hexadecimal.length > 2;
return '\\' + (longhand ? 'u' : 'x') +
('0000' + hexadecimal).slice(longhand ? -4 : -2);
}
function stringify(key, value) {
return key + ":" + JSON.stringify(value).replace(/\\\\(u|x)/, "\\$1");
}
function newFile(name, callback) {
var filename = path.join(__dirname, "category", name + ".ts"),
file = fs.createWriteStream(filename, {encoding:'utf8'});
file.once('close', function () {
if (!--refs) {
console.log("done.");
callback();
}
});
refs++;
return file;
}
function parser(callback) {
var data = {},
buffer = new BufferStream({encoding:'utf8', size:'flexible'}),
resume = buffer.resume.bind(buffer);
buffer.split('\n', function (line) {
var v, c, char = {},
values = line.toString().split(';');
for(var i = 0 ; i < 15 ; i++)
char[keys[i]] = values[i];
v = parseInt(char.value, 16);
char.symbol = escape(char.value);
c = char.category;
if (!data[c]) {
data[c] = newFile(c, callback)
.on('drain', resume)
.once('open', function () {
console.log("saving data as %s.ts …", c);
if (this.write('import type { UnicodeItem } from "../types.ts";const u: Record<number, UnicodeItem> ={' + stringify(v, char)))
buffer.resume();
});
buffer.pause();
} else if (!data[c].write("," + stringify(v, char))) {
buffer.pause();
}
});
buffer.on('end', function () {
var cat, categories = Object.keys(data),
len = categories.length;
for(var i = 0 ; i < len ; i++) {
cat = categories[i];
data[cat].end("};export default u;");
}
});
buffer.on('error', function (err) {
if (typeof err === 'string')
err = new Error(err);
throw err;
});
return buffer;
}
function read_file(success_cb, error_cb) {
var systemfile, sysfiles = systemfiles.slice(),
try_reading = function (success, error) {
systemfile = sysfiles.shift();
if (!systemfile) return error_cb();
console.log("try to read file %s …", systemfile);
fs.exists(systemfile, function (exists) {
if (!exists) {
console.error("%s not found.", systemfile);
return try_reading(success_cb, error_cb);
}
console.log("parsing …");
fs.createReadStream(systemfile, {encoding:'utf8'}).pipe(parser(success_cb));
});
};
try_reading(success_cb, error_cb);
}
// run
if (!module.parent) { // not required
read_file(process.exit, process.exit);
} else {
module.exports = {
escape:escape,
stringify:stringify,
newFile:newFile,
parser:parser,
read_file:read_file
};
}