Repository navigation
Expand file tree
/
Copy pathgenerate-emoji-data.js
More file actions
227 lines (185 loc) · 6.9 KB
/
Copy pathgenerate-emoji-data.js
File metadata and controls
227 lines (185 loc) · 6.9 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
const fs = require('fs');
const path = require('path');
const { execFileSync } = require('child_process');
const data = require('emojibase-data/en/data.json');
const emojibaseShortcodes = require('emojibase-data/en/shortcodes/emojibase.json');
const joypixelsShortcodes = require('emojibase-data/en/shortcodes/joypixels.json');
const pinnedShortnames = require('./pinned-shortnames');
const legacyShortnames = require('../app/lib/constants/emojis/legacyShortnames.json');
const OUTPUT = path.join(__dirname, '..', 'app', 'lib', 'constants', 'emojis', 'data.ts');
const groupToCategory = {
0: 'people',
1: 'people',
3: 'nature',
4: 'food',
5: 'travel',
6: 'activity',
7: 'objects',
8: 'symbols',
9: 'flags'
};
const CATEGORIES = ['people', 'nature', 'food', 'activity', 'travel', 'objects', 'symbols', 'flags'];
const bare = unicode => unicode.replace(/️/g, '');
const isRegionalIndicator = hexcode => {
if (hexcode.includes('-')) {
return false;
}
const cp = parseInt(hexcode, 16);
return cp >= 0x1f1e6 && cp <= 0x1f1ff;
};
const getShortcodes = hexcode => {
const entry = joypixelsShortcodes[hexcode] ?? emojibaseShortcodes[hexcode];
if (!entry) {
return [];
}
return Array.isArray(entry) ? entry : [entry];
};
const build = () => {
const emojisByCategory = Object.fromEntries(CATEGORIES.map(key => [key, []]));
const shortnameToUnicode = {};
const aliasesByName = {};
const add = (shortname, unicode) => {
const key = `:${shortname}:`;
if (!(key in shortnameToUnicode)) {
shortnameToUnicode[key] = unicode;
}
};
data.forEach(emoji => {
if (emoji.group === 2) {
return;
}
const isRegional = isRegionalIndicator(emoji.hexcode);
const category = groupToCategory[emoji.group] ?? (isRegional ? 'flags' : undefined);
if (!category) {
return;
}
const codes = getShortcodes(emoji.hexcode);
if (codes.length === 0) {
return;
}
const [name, ...aliases] = codes;
add(name, emoji.emoji);
aliases.forEach(alias => add(alias, emoji.emoji));
if (aliases.length) {
aliasesByName[name] = aliases;
}
if (!emoji.tone && !isRegional) {
emojisByCategory[category].push(name);
}
(emoji.skins ?? []).forEach(skin => {
if (!skin.tone) {
return;
}
const tones = Array.isArray(skin.tone) ? skin.tone : [skin.tone];
getShortcodes(skin.hexcode).forEach(code => add(code, skin.emoji));
add(`${name}_tone${tones.join('-')}`, skin.emoji);
});
});
return { emojisByCategory, shortnameToUnicode, aliasesByName };
};
const applyPins = shortnameToUnicode => {
const applied = [];
const stale = [];
const gone = [];
const displaced = [];
Object.keys(pinnedShortnames).forEach(shortname => {
const upstream = shortnameToUnicode[shortname];
if (!upstream) {
gone.push(shortname);
return;
}
if (bare(upstream) === bare(pinnedShortnames[shortname])) {
stale.push(shortname);
return;
}
shortnameToUnicode[shortname] = pinnedShortnames[shortname];
applied.push(shortname);
displaced.push(upstream);
});
const resolvable = new Set(Object.keys(shortnameToUnicode).map(key => bare(shortnameToUnicode[key])));
const orphaned = displaced.filter(unicode => !resolvable.has(bare(unicode)));
return { applied, stale, gone, orphaned };
};
const header = filename =>
`// Generated by scripts/generate-emoji-data.js — do not edit by hand.\n// Run \`pnpm generate-emoji-data\` after bumping the emojibase-data dependency.\n// See docs/emojis.md.\n/* eslint-disable */\n${filename}`;
const stringifyList = (name, list, indent = '\t') =>
`${name}: [\n${list.map(item => `${indent}\t'${item}'`).join(',\n')}\n${indent}]`;
const writeData = ({ emojisByCategory, shortnameToUnicode, aliasesByName }) => {
const categories = CATEGORIES.map(key => `\t${stringifyList(key, emojisByCategory[key])}`).join(',\n');
const map = Object.keys(shortnameToUnicode)
.map(key => `\t'${key}': '${shortnameToUnicode[key]}'`)
.join(',\n');
const aliases = Object.keys(aliasesByName)
.map(name => `\t${stringifyList(`'${name}'`, aliasesByName[name])}`)
.join(',\n');
const contents = header(`
export const emojisByCategory = {
${categories}
};
// Every generated shortname, aliases and skin tone variants included. Not everything the app can
// resolve: useShortnameToUnicode falls back to legacyShortnames.json, which is kept out of this map.
// Values come from emojibase, except the ones held back by scripts/pinned-shortnames.js.
export const shortnameToUnicodeMap: { [key: string]: string } = {
${map}
};
// Alternate shortnames for an emoji, keyed by the name listed in \`emojisByCategory\`.
// The picker lists one name per emoji, so searching matches every alias but returns the listed name.
export const aliasesByEmojiName: { [key: string]: string[] } = {
${aliases}
};
`);
fs.writeFileSync(OUTPUT, contents, 'utf8');
execFileSync('pnpm', ['exec', 'oxfmt', OUTPUT], { stdio: 'ignore' });
};
const foldLegacyIntoAliases = ({ emojisByCategory, shortnameToUnicode, aliasesByName }, legacy) => {
const nameByUnicode = {};
CATEGORIES.forEach(category =>
emojisByCategory[category].forEach(name => {
const unicode = shortnameToUnicode[`:${name}:`];
if (unicode && !(bare(unicode) in nameByUnicode)) {
nameByUnicode[bare(unicode)] = name;
}
})
);
let folded = 0;
Object.keys(legacy).forEach(shortname => {
const name = nameByUnicode[bare(legacy[shortname])];
const alias = shortname.slice(1, -1);
if (!name || alias === name) {
return;
}
if (!aliasesByName[name]) {
aliasesByName[name] = [];
}
if (!aliasesByName[name].includes(alias)) {
aliasesByName[name].push(alias);
folded += 1;
}
});
return folded;
};
const emojiData = build();
const pins = applyPins(emojiData.shortnameToUnicode);
const folded = foldLegacyIntoAliases(emojiData, legacyShortnames);
writeData(emojiData);
const total = CATEGORIES.reduce((acc, key) => acc + emojiData.emojisByCategory[key].length, 0);
console.log(`🚀 Emoji data generated: ${total} emojis, ${Object.keys(emojiData.shortnameToUnicode).length} shortnames.`);
console.log(`ℹ️ ${Object.keys(legacyShortnames).length} legacy shortnames read, ${folded} kept searchable as aliases.`);
console.log(`📌 ${pins.applied.length} shortnames pinned to the glyph a previous release resolved.`);
const stale = Object.keys(legacyShortnames).filter(key => key in emojiData.shortnameToUnicode);
if (stale.length) {
console.log(
`⚠️ ${stale.length} entries in legacyShortnames.json are back in emojibase and can be removed: ${stale.join(', ')}`
);
}
if (pins.stale.length) {
console.log(`⚠️ ${pins.stale.length} pins now match emojibase and can be removed: ${pins.stale.join(', ')}`);
}
if (pins.gone.length) {
console.log(
`⚠️ ${pins.gone.length} pins no longer resolve — emojibase dropped the name, so it belongs in legacyShortnames.json: ${pins.gone.join(', ')}`
);
}
if (pins.orphaned.length) {
console.log(`⚠️ a pin took the last shortname of ${pins.orphaned.join(' ')} — nothing resolves to it any more.`);
}