-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-data.js
83 lines (74 loc) · 1.91 KB
/
build-data.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
"use strict";
const fs = require("fs/promises");
const fetch = require("node-fetch");
const path = require("path");
const version = "v4.0.0.rc2";
const url = `https://raw.githubusercontent.com/github/gemoji/${version}/db/emoji.json`;
main();
function charAndDescription(emojiObj) {
return [emojiObj.emoji, emojiObj.description];
}
function categories(emojiObj) {
return emojiObj.categories;
}
// it appears aliases are unique
function aliases(emojiObj) {
return emojiObj.aliases;
}
function tags(emojiObj) {
return emojiObj.tags;
}
function writeData(obj, fileName) {
return fs.writeFile(
path.join(".", "data", fileName + ".json"),
JSON.stringify(obj),
"utf-8"
);
}
function main() {
return fetch(url)
.then((res) => res.json())
.then((json) => {
const data = json.map((r) => {
const { unicode_version, ios_version, ...rest } = r;
return rest;
});
return writeData(data, "gemoji").then(() => json);
})
.then((json) => {
const aliasLookup = json.flatMap((r, i) =>
r.aliases.map((a) => [a, r.emoji, i])
);
return writeData(aliasLookup, "alias_lookup").then(() => json);
})
.then((json) => {
const categoryLookup = json.reduce((prev, cur, index) => {
const category = cur.category;
if (!prev[category]) {
prev[category] = [];
}
prev[category].push(index);
return prev;
}, {});
return writeData(categoryLookup, "category_lookup").then(() => json);
})
.then((json) => {
const keys = [
"emoji",
"description",
"category",
"aliases",
"tags",
"unicode_version",
"ios_version",
"skin_tones",
];
let indices = {};
return Promise.all(
keys.map((key) => {
const data = json.map((r) => r[key]);
return writeData(data, key);
})
);
});
}