forked from adrium/easypass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateVowelsRegexp.js
executable file
·99 lines (86 loc) · 2.36 KB
/
generateVowelsRegexp.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
#!/usr/bin/env node
/*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
"use strict";
const https = require("https");
// Only Latin, Greek and Cyrillic vowels for now, derivations of those will
// be determined below.
const baseVowels = "AEIOUÆ" + "ΑΕΗΙΟΥΩ" + "АЕЄИІОУЫЭЮЯ";
function download(url)
{
return new Promise((resolve, reject) =>
{
let request = https.get(url, response =>
{
if (response.statusCode != 200)
{
reject(new Error("Unexpected status code: " + response.statusCode));
response.resume();
return;
}
let data = "";
response.on("data", chunk =>
{
data += chunk;
});
response.on("end", () =>
{
resolve(data);
});
});
request.on("error", error => reject(new Error(error.message)));
});
}
function formatRange([start, end])
{
if (start == end)
return String.fromCharCode(start);
else if (start + 1 == end)
return String.fromCharCode(start, end);
else
return String.fromCharCode(start) + "-" + String.fromCharCode(end);
}
download("https://unicode.org/Public/UNIDATA/UnicodeData.txt").then(data =>
{
data = data.trim().split(/[\r\n]+/).map(line => line.split(";"));
let vowels = new Set();
for (let i = 0; i < baseVowels.length; i++)
{
vowels.add(baseVowels.charCodeAt(i));
vowels.add(baseVowels.charAt(i).toLowerCase().charCodeAt(0));
}
let changed;
do
{
changed = false;
for (let [code, name, category, , , decomposition] of data)
{
if (!category.startsWith("L") || code.length > 4 || vowels.has(parseInt(code, 16)) || !decomposition)
continue;
if (decomposition.split(/\s+/).some(code => vowels.has(parseInt(code, 16))))
{
vowels.add(parseInt(code, 16));
changed = true;
}
}
} while (changed);
vowels = [...vowels.keys()];
vowels.sort((a, b) => a - b);
let result = "/[";
let currentRange = [vowels[0], vowels[0] - 1];
for (let vowel of vowels)
{
if (vowel == currentRange[1] + 1)
currentRange[1] = vowel;
else
{
result += formatRange(currentRange);
currentRange = [vowel, vowel];
}
}
result += "]/";
console.log(result);
}).catch(e => console.error(e));