-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.js
More file actions
240 lines (209 loc) · 6.49 KB
/
generate.js
File metadata and controls
240 lines (209 loc) · 6.49 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"
/*eslint-disable no-console */
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const Buffer = require("buffer").Buffer
const fs = require("fs")
const http = require("http")
const path = require("path")
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const UCD_URL = "http://www.unicode.org/Public/UCD/latest/ucd/EastAsianWidth.txt"
const FILENAME = path.resolve(__dirname, "../lib/is-narrow-character.js")
const ROW_PATTERN = /^(\w+)(?:..(\w+))?;(\w+)\s*(?:#.*)?$/gm
/**
* It checks whether the given type is narrow or not.
*
* @param {string} type - The type of characters.
* @returns {boolean} `true` if the type is narrow.
*/
function isNarrow(type) {
return type === "H" || type === "Na"
}
/**
* It converts the number to upper case hex string.
*
* @param {number} n - The number to convert.
* @returns {string} The hex string of the number.
*/
function toHex(n) {
return `0x${n.toString(16).toUpperCase()}`
}
/**
* It fetches characters information from Unicode Character Database.
*
* @returns {Promise<string>} The content of database.
*/
function fetchUnicodeCharacterData() {
return new Promise((resolve, reject) => {
console.log("Fetching:", UCD_URL)
http.get(UCD_URL, (res) => {
const chunks = []
res.on("data", (chunk) => {
console.log(" Received", chunk.length, "bytes.")
chunks.push(chunk)
})
res.on("end", () => {
console.log(" Done.")
const text = Buffer.concat(chunks).toString("utf8")
if (res.statusCode === 200) {
resolve(text)
}
else {
reject(new Error(text || "Unknown Error"))
}
})
res.on("error", reject)
}).on("error", reject)
})
}
/**
* It parses the content of Unicode Character Database.
*
* @param {string} content - The content to parse.
* @returns {IterableIterator<{first: number, last: number, type: string}>}
* The result of parsing.
*/
function* parseUnicodeCharacterData(content) {
ROW_PATTERN.lastIndex = 0
let m = null
while ((m = ROW_PATTERN.exec(content)) != null) {
const first = parseInt(m[1], 16)
const last = m[2] ? parseInt(m[2], 16) : first
const type = m[3]
yield {first, last, type}
}
}
/**
* It extracts the range of narrow characters.
*
* @param {Iterable<{first: number, last: number, type: string}>} ranges -
* The range of all characters.
* @returns {IterableIterator<{first: number, last: number}>}
* The range of narrow characters.
*/
function* extractNarrowRange(ranges) {
let prevRange = null
for (const range of ranges) {
if (!isNarrow(range.type)) {
continue
}
const first = range.first
const last = range.last
if (prevRange == null) {
// 1st range.
prevRange = {first, last}
}
else if (prevRange.last === first - 1) {
// if the range is consecutive with the last range,
// it merges those.
prevRange.last = last
}
else {
console.log("Range:", prevRange.first, prevRange.last)
yield prevRange
prevRange = {first, last}
}
}
if (prevRange != null) {
console.log("Range:", prevRange.first, prevRange.last)
yield prevRange
}
}
/**
* It converts the given ranges to comparison code list.
*
* The comparison code list can be concatenated by `||` operators.
*
* @param {Iterable<{first: number, last: number}>} ranges -
* The ranges to convert.
* @returns {IterableIterator<string>} The generated code.
*/
function* toComparisonCode(ranges) {
for (const range of ranges) {
const first = range.first
const last = range.last
if (first === last) {
yield `cp === ${toHex(first)}`
}
else if (first + 1 === last) {
yield `cp === ${toHex(first)}`
yield `cp === ${toHex(last)}`
}
else {
yield `(cp >= ${toHex(first)} && cp <= ${toHex(last)})`
}
}
}
/**
* It generates the code which includes `isNarrowCharacter` function.
*
* @param {Iterable<string>} comparisonCodes - The comparison codes.
* @returns {string} The generated code.
*/
function generateCode(comparisonCodes) {
return `/**
* @author Toru Nagashima <https://github.com/mysticatea>
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"
//------------------------------------------------------------------------------
// Exports
//------------------------------------------------------------------------------
/*eslint-disable complexity */
/**
* It checks whether the given character is a narrow character or not.
*
* @param {string} character - The character to check.
* @returns {boolean} \`true\` if the character is a narrow character.
*/
module.exports = function isNarrowCharacter(character) {
const cp = character.codePointAt(0)
return (
${Array.from(comparisonCodes).join(" ||\n ")}
)
}
/*eslint-enable */
`
}
/**
* It writes the code to `lib/is-narrow-character.js` file.
*
* @param {string} code - The code to write.
* @returns {Promise<void>} -
*/
function writeFile(code) {
return new Promise((resolve, reject) => {
console.log("Writing:", FILENAME)
fs.writeFile(FILENAME, code, (err) => {
if (err == null) {
console.log(" Done.")
resolve()
}
else {
reject(err)
}
})
})
}
//------------------------------------------------------------------------------
// Main
//------------------------------------------------------------------------------
fetchUnicodeCharacterData()
.then(parseUnicodeCharacterData)
.then(extractNarrowRange)
.then(toComparisonCode)
.then(generateCode)
.then(writeFile)
.catch(err => {
console.error(err.stack)
})
/*eslint-enable */