-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
emojiMap.js
118 lines (112 loc) · 3.77 KB
/
emojiMap.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
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
/*
* This file contains the Map of word --> emoji substitutions.
*/
/* exported sortedEmojiMap */
let dictionary = new Map();
dictionary.set('apple', '🍎');
dictionary.set('banana', '🍌');
dictionary.set('bang', '💥');
dictionary.set('baseball', '⚾');
dictionary.set('basketball', '🏀');
dictionary.set('beer', '🍺');
dictionary.set('bicycle', '🚴');
dictionary.set('bike', '🚴');
dictionary.set('bomb', '💣');
dictionary.set('boy', '👦');
dictionary.set('bug', '🐛');
dictionary.set('burger', '🍔');
dictionary.set('burn', '🔥');
dictionary.set('cake', '🎂');
dictionary.set('candy', '🍬');
dictionary.set('cat', '🐱');
dictionary.set('celebration', '🎉');
dictionary.set('cheeseburger', '🍔');
dictionary.set('cookie', '🍪');
dictionary.set('cool', '😎');
dictionary.set('cry', '😢');
dictionary.set('dog', '🐶');
dictionary.set('doge', '🐕');
dictionary.set('earth', '🌎');
dictionary.set('explode', '💥');
dictionary.set('fart', '💨');
dictionary.set('fast', '💨');
dictionary.set('female', '👩');
dictionary.set('fire', '🔥');
dictionary.set('fish', '🐟');
dictionary.set('flame', '🔥');
dictionary.set('flower', '🌹');
dictionary.set('food', '🍕');
dictionary.set('football', '🏈');
dictionary.set('girl', '👧');
dictionary.set('golf', '⛳');
dictionary.set('hamburger', '🍔');
dictionary.set('happy', '😀');
dictionary.set('horse', '🐴');
dictionary.set('hot', '🔥');
dictionary.set('kiss', '😘');
dictionary.set('laugh', '😂');
dictionary.set('lit', '🔥');
dictionary.set('lock', '🔒');
dictionary.set('lol', '😂');
dictionary.set('love', '😍');
dictionary.set('male', '👨');
dictionary.set('man', '👨');
dictionary.set('monkey', '🐵');
dictionary.set('moon', '🌙');
dictionary.set('note', '📝');
dictionary.set('paint', '🎨');
dictionary.set('panda', '🐼');
dictionary.set('party', '🎉');
dictionary.set('pig', '🐷');
dictionary.set('pizza', '🍕');
dictionary.set('planet', '🌎');
dictionary.set('rose', '🌹');
dictionary.set('rofl', '😂');
dictionary.set('sad', '😢');
dictionary.set('sleep', '😴');
dictionary.set('smile', '😀');
dictionary.set('smiley', '😀');
dictionary.set('soccer', '⚽');
dictionary.set('star', '⭐');
dictionary.set('sun', '☀️');
dictionary.set('sunglasses', '😎');
dictionary.set('surprised', '😮');
dictionary.set('tree', '🌲');
dictionary.set('trophy', '🏆');
dictionary.set('win', '🏆');
dictionary.set('wind', '💨');
dictionary.set('wine', '🍷');
dictionary.set('wink', '😉');
dictionary.set('woman', '👩');
dictionary.set('world', '🌎');
dictionary.set('wow', '😮');
/*
* After all the dictionary entries have been set, sort them by length.
*
* Because iteration over Maps happens by insertion order, this avoids
* scenarios where words that are substrings of other words get substituted
* first, leading to the longer word's substitution never triggering.
*
* For example, the 'woman' substitution would never get triggered
* if the 'man' substitution happens first because the input term 'woman'
* would become 'wo👨', and the search for 'woman' would not find any matches.
*/
let tempArray = Array.from(dictionary);
tempArray.sort((pair1, pair2) => {
// Each pair is an array with two entries: a word, and its emoji.
// Ex: ['woman', '👩']
const firstWord = pair1[0];
const secondWord = pair2[0];
if (firstWord.length > secondWord.length) {
// The first word should come before the second word.
return -1;
}
if (secondWord.length > firstWord.length) {
// The second word should come before the first word.
return 1;
}
// The words have the same length, it doesn't matter which comes first.
return 0;
});
// Now that the entries are sorted, put them back into a Map.
let sortedEmojiMap = new Map(tempArray);