forked from Julien-Marcou/Unicode-Emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (82 loc) · 2.85 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
86
87
88
89
90
import UNICODE_EMOJI from './unicode-emoji.js';
const emojiVariations = new Map();
function groupEmojis(emojis, groupBy) {
const groupedEmojis = {};
for (const emoji of emojis) {
if (!(emoji[groupBy] in groupedEmojis)) {
groupedEmojis[emoji[groupBy]] = [];
}
groupedEmojis[emoji[groupBy]].push(emoji);
}
return groupedEmojis;
}
function filterEmojis(emojis, omitWhere) {
const filteredEmojis = [];
for (const emoji of emojis) {
let omit = false;
for (const omitField in omitWhere) {
if (omitWhere[omitField].includes(emoji[omitField])) {
omit = true;
break;
}
}
if (omit) {
continue;
}
if (omitWhere.version) {
if (!emojiVariations.has(emoji) && emoji.variations) {
emojiVariations.set(emoji, emoji.variations);
}
if (emojiVariations.has(emoji)) {
const filteredVariations = [];
for (const variation of emojiVariations.get(emoji)) {
if (!omitWhere.version.includes(variation.version)) {
filteredVariations.push(variation);
}
}
emoji.variations = filteredVariations.length ? filteredVariations : undefined;
}
}
filteredEmojis.push(emoji);
}
return filteredEmojis;
}
function safeCheckGroupBy(groupBy) {
const allowedFields = ['category', 'group', 'subgroup', 'version'];
if (!groupBy || typeof groupBy !== 'string' || !allowedFields.includes(groupBy)) {
throw new Error("You can't group emojis with \"" + groupBy + "\" - Try \"category\", \"group\", \"subgroup\" or \"version\" instead");
}
}
function safeCheckOmitBy(omitWhere) {
const allowedFields = ['category', 'group', 'subgroup', 'version'];
if (!omitWhere || typeof omitWhere !== 'object') {
throw new Error("You can't filter emojis with \"" + omitWhere + "\" - Use an object, like \"{category: ['flags', 'symbols']}\" instead")
}
for (const omitField in omitWhere) {
if (!allowedFields.includes(omitField)) {
throw new Error("You can't filter emojis with key \"" + omitField + "\" - Try \"category\", \"group\", \"subgroup\" or \"version\" instead");
}
else if (!(omitWhere[omitField] instanceof Array)) {
throw new Error("You can't filter emojis with value \"" + omitWhere[omitField] + "\" for key \"" + omitField + "\" - Use an array, like \"['flags', 'symbols']\" instead");
}
}
}
export function getComponents() {
return UNICODE_EMOJI.components;
}
export function getEmojis(omitWhere) {
if (omitWhere) {
safeCheckOmitBy(omitWhere);
return filterEmojis(UNICODE_EMOJI.emojis, omitWhere);
}
return UNICODE_EMOJI.emojis;
}
export function getEmojisGroupedBy(groupBy, omitWhere) {
let emojis = UNICODE_EMOJI.emojis;
if (omitWhere) {
safeCheckOmitBy(omitWhere);
emojis = filterEmojis(emojis, omitWhere);
}
safeCheckGroupBy(groupBy);
return groupEmojis(emojis, groupBy);
}