-
Notifications
You must be signed in to change notification settings - Fork 0
/
emojis.js
91 lines (78 loc) · 2.71 KB
/
emojis.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
const fs = require('fs/promises');
const fs_simple = require('fs');
const path = require('path');
const emojisList = async () => {
return await fs.readFile('emoji-test.txt', 'utf-8');
};
function grouper(array, toFind) {
return array.reduce((previous, current, index) => {
if (current.includes(toFind)) {
previous.push({ name: current, start: index });
}
return previous
}, []);
}
const map = {};
(async function() {
const list = await emojisList();
let lines = list.split('\n');
const indexes = grouper(lines, '# group:');
indexes.forEach((group, index) => {
map[`group${index}`] = {
name: group.name.slice(9),
start: group.start,
subgroups: []
}
})
for (let i = 0; i < indexes.length; i++) {
let currentGroup = [];
if (i + 1 < indexes.length) {
currentGroup = lines.slice(indexes[i].start, indexes[i + 1].start);
} else {
currentGroup = lines.slice(indexes[i].start);
}
map[`group${i}`].subgroups = [...grouper(currentGroup, '# subgroup:')];
}
Object.entries(map).forEach(([key, value]) => {
const subgroups = value.subgroups;
const groupStart = value.start;
for(let j = 0; j < subgroups.length; j++) {
let emojis = null;
if (j + 1 < subgroups.length) {
emojis = lines.slice(groupStart + subgroups[j].start + 1, groupStart + subgroups[j + 1].start - 1);
} else {
emojis = lines.slice(groupStart + subgroups[j].start + 1);
}
emojis = emojis.map((emoji) => {
const a = emoji.replace(';fully-qualified', '');
const emojiName = a.slice(a.search(/[0-9]\s[a-z]/) + 2).replace(/ +/g, '_').replace(/-/g, '_')
const emojiCode = a.replace(/ +/g, '').slice(0, a.replace(/ +/g, '').indexOf(';'))
return {
emojiName,
emojiCode
}
});
map[key].emojis = emojis;
delete map[key].start;
delete map[key].subgroups;
}
});
const filePath = path.join(__dirname, 'src/emojis.rs');
const writableStream = fs_simple.createWriteStream(filePath);
writableStream.on('error', (error) => console.error(error));
writableStream.on('finish', () => console.log(`File created to ${filePath}`))
writableStream.write(`use std::collections::BTreeMap;
pub fn get_map() -> BTreeMap<&'static str, &'static str> {
let mut map = BTreeMap::new();
`);
Object.values(map).forEach(value => {
value.emojis.forEach(emoji => {
if (emoji.emojiName !== '' && emoji.emojiName !== "EOF" && !emoji.emojiName.includes("_subgroup")) {
writableStream.write(` map.insert("${emoji.emojiName}", "${emoji.emojiCode}");\n`);
}
});
});
writableStream.write(' map\n');
writableStream.write('}');
writableStream.end();
})();