This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
85 lines (66 loc) · 1.9 KB
/
index.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
"use strict";
const path = require("path");
const alfy = require("alfy");
const search = require("@jukben/emoji-search").default;
const unicode = require("@jukben/unicode").default;
const emojis = require("emoji-datasource-apple");
const ALWAYS_OFFLINE = +process.env["always-offline"] || 0;
const emojiLookup = emojis.reduce((a, v, i, arr) => {
a[v.unified.toLowerCase()] = v;
if (v.non_qualified) {
a[v.non_qualified.toLowerCase()] = v;
}
return a;
}, {});
const getHex = char => unicode(char).hexCodeAt(0);
function getImagePath(char) {
return `${path.dirname(require.resolve("emoji-datasource-apple"))}/img/apple/64/${emojiLookup[getHex(char)].image}`;
}
function getName(char) {
return (emojiLookup[getHex(char)] && emojiLookup[getHex(char)].name) || char;
}
const emoji = () => {
const format = char => {
const name = getName(char).toLowerCase();
return {
uid: name,
title: "",
subtitle: name,
icon: { path: getImagePath(char) },
arg: char,
};
};
const nothingFound = [{ title: "Nothing found!", icon: { path: getImagePath("👀") } }];
const getOfflineEmoji = () => {
let results = search(alfy.input)
.map(({ char }) => char)
.filter(char => char)
.map(format)
if (!results.length) {
results = nothingFound;
}
return results;
}
if (ALWAYS_OFFLINE) {
return alfy.output(getOfflineEmoji());
}
return alfy
.fetch("emoji.getdango.com/api/emoji", {
maxAge: 8640000000, // cache for 24 hours
query: {
q: alfy.input,
syn: 1
}
})
.then(({ results, synonyms = [] } = {}) => {
if (!results) {
return alfy.output(nothingFound);
}
return alfy.output([...new Set([...synonyms, ...results.map(({ text }) => text)])].map(format));
})
.catch(() => {
alfy.output(getOfflineEmoji())
});
};
emoji();
module.exports = emoji;