-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
EmojiUtils.js
254 lines (231 loc) · 8.67 KB
/
EmojiUtils.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import _ from 'underscore';
import lodashOrderBy from 'lodash/orderBy';
import moment from 'moment';
import Str from 'expensify-common/lib/str';
import CONST from '../CONST';
import * as User from './actions/User';
import emojisTrie from './EmojiTrie';
/**
* Get the unicode code of an emoji in base 16.
* @param {String} input
* @returns {String}
*/
const getEmojiUnicode = _.memoize((input) => {
if (input.length === 0) {
return '';
}
if (input.length === 1) {
return _.map(input.charCodeAt(0).toString().split(' '), val => parseInt(val, 10).toString(16)).join(' ');
}
const pairs = [];
// Some Emojis in UTF-16 are stored as pair of 2 Unicode characters (eg Flags)
// The first char is generally between the range U+D800 to U+DBFF called High surrogate
// & the second char between the range U+DC00 to U+DFFF called low surrogate
// More info in the following links:
// 1. https://docs.microsoft.com/en-us/windows/win32/intl/surrogates-and-supplementary-characters
// 2. https://thekevinscott.com/emojis-in-javascript/
for (let i = 0; i < input.length; i++) {
if (input.charCodeAt(i) >= 0xd800 && input.charCodeAt(i) <= 0xdbff) { // high surrogate
if (input.charCodeAt(i + 1) >= 0xdc00 && input.charCodeAt(i + 1) <= 0xdfff) { // low surrogate
pairs.push(
((input.charCodeAt(i) - 0xd800) * 0x400)
+ (input.charCodeAt(i + 1) - 0xdc00) + 0x10000,
);
}
} else if (input.charCodeAt(i) < 0xd800 || input.charCodeAt(i) > 0xdfff) {
// modifiers and joiners
pairs.push(input.charCodeAt(i));
}
}
return _.map(pairs, val => parseInt(val, 10).toString(16)).join(' ');
});
/**
* Function to remove Skin Tone and utf16 surrogates from Emoji
* @param {String} emojiCode
* @returns {String}
*/
function trimEmojiUnicode(emojiCode) {
return emojiCode.replace(/(fe0f|1f3fb|1f3fc|1f3fd|1f3fe|1f3ff)$/, '').trim();
}
/**
* Validates that this message contains only emojis
*
* @param {String} message
* @returns {Boolean}
*/
function containsOnlyEmojis(message) {
const trimmedMessage = Str.replaceAll(message.replace(/ /g, ''), '\n', '');
const match = trimmedMessage.match(CONST.REGEX.EMOJIS);
if (!match) {
return false;
}
const codes = [];
_.map(match, emoji => _.map(getEmojiUnicode(emoji).split(' '), (code) => {
if (!CONST.EMOJI_INVISIBLE_CODEPOINTS.includes(code)) {
codes.push(code);
}
return code;
}));
// Emojis are stored as multiple characters, so we're using spread operator
// to iterate over the actual emojis, not just characters that compose them
const messageCodes = _.filter(_.map([...trimmedMessage], char => getEmojiUnicode(char)), string => string.length > 0 && !CONST.EMOJI_INVISIBLE_CODEPOINTS.includes(string));
return codes.length === messageCodes.length;
}
/**
* Get the header indices based on the max emojis per row
* @param {Object[]} emojis
* @returns {Number[]}
*/
function getDynamicHeaderIndices(emojis) {
const headerIndices = [];
_.each(emojis, (emoji, index) => {
if (!emoji.header) {
return;
}
headerIndices.push(Math.floor(index / CONST.EMOJI_NUM_PER_ROW));
});
return headerIndices;
}
/**
* Get number of empty spaces to be filled to get equal emojis for every row
* @param {Number} emojiCount
* @param {Number} suffix
* @returns {Object[]}
*/
function getDynamicSpacing(emojiCount, suffix) {
const spacerEmojis = [];
let modLength = CONST.EMOJI_NUM_PER_ROW - (emojiCount % CONST.EMOJI_NUM_PER_ROW);
// Empty spaces is pushed if the given row has less than eight emojis
while (modLength > 0 && modLength < CONST.EMOJI_NUM_PER_ROW) {
spacerEmojis.push({
code: `${CONST.EMOJI_SPACER}_${suffix}_${modLength}`,
spacer: true,
});
modLength -= 1;
}
return spacerEmojis;
}
/**
* Add dynamic spaces to emoji categories
* @param {Object[]} emojis
* @returns {Object[]}
*/
function addSpacesToEmojiCategories(emojis) {
let updatedEmojis = [];
_.each(emojis, (emoji, index) => {
if (emoji.header) {
updatedEmojis = updatedEmojis.concat(getDynamicSpacing(updatedEmojis.length, index), [emoji], getDynamicSpacing(1, index));
return;
}
updatedEmojis.push(emoji);
});
return updatedEmojis;
}
/**
* Get a merged array with frequently used emojis
* @param {Object[]} emojis
* @param {Object[]} frequentlyUsedEmojis
* @returns {Object[]}
*/
function mergeEmojisWithFrequentlyUsedEmojis(emojis, frequentlyUsedEmojis = []) {
if (frequentlyUsedEmojis.length === 0) {
return addSpacesToEmojiCategories(emojis);
}
let allEmojis = [{
header: true,
code: 'frequentlyUsed',
}];
allEmojis = allEmojis.concat(frequentlyUsedEmojis, emojis);
return addSpacesToEmojiCategories(allEmojis);
}
/**
* Update the frequently used emojis list by usage and sync with API
* @param {Object[]} frequentlyUsedEmojis
* @param {Object} newEmoji
*/
function addToFrequentlyUsedEmojis(frequentlyUsedEmojis, newEmoji) {
let frequentEmojiList = frequentlyUsedEmojis;
let currentEmojiCount = 1;
const currentTimestamp = moment().unix();
const emojiIndex = _.findIndex(frequentEmojiList, e => e.code === newEmoji.code);
if (emojiIndex >= 0) {
currentEmojiCount = frequentEmojiList[emojiIndex].count + 1;
frequentEmojiList.splice(emojiIndex, 1);
}
const updatedEmoji = {...newEmoji, ...{count: currentEmojiCount, lastUpdatedAt: currentTimestamp}};
const maxFrequentEmojiCount = (CONST.EMOJI_FREQUENT_ROW_COUNT * CONST.EMOJI_NUM_PER_ROW) - 1;
// We want to make sure the current emoji is added to the list
// Hence, we take one less than the current high frequent used emojis and if same then sorted by lastUpdatedAt
frequentEmojiList = lodashOrderBy(frequentEmojiList, ['count', 'lastUpdatedAt'], ['desc', 'desc']);
frequentEmojiList = frequentEmojiList.slice(0, maxFrequentEmojiCount);
frequentEmojiList.push(updatedEmoji);
// Second sorting is required so that new emoji is properly placed at sort-ordered location
frequentEmojiList = lodashOrderBy(frequentEmojiList, ['count', 'lastUpdatedAt'], ['desc', 'desc']);
User.updateFrequentlyUsedEmojis(frequentEmojiList);
}
/**
* Replace any emoji name in a text with the emoji icon
* @param {String} text
* @returns {String}
*/
function replaceEmojis(text) {
let newText = text;
const emojiData = text.match(CONST.REGEX.EMOJI_NAME);
if (!emojiData || emojiData.length === 0) {
return text;
}
for (let i = 0; i < emojiData.length; i++) {
const checkEmoji = emojisTrie.search(emojiData[i].slice(1, -1));
if (checkEmoji && checkEmoji.metaData.code) {
let emojiReplacement = checkEmoji.metaData.code;
// If this is the last emoji in the message and it's the end of the message so far,
// add a space after it so the user can keep typing easily.
if (i === emojiData.length - 1 && text.endsWith(emojiData[i])) {
emojiReplacement += ' ';
}
newText = newText.replace(emojiData[i], emojiReplacement);
}
}
return newText;
}
/**
* Suggest emojis when typing emojis prefix after colon
* @param {String} text
* @param {Number} [limit] - matching emojis limit
* @returns {Array}
*/
function suggestEmojis(text, limit = 5) {
const emojiData = text.match(CONST.REGEX.EMOJI_SUGGESTIONS);
if (emojiData) {
const matching = [];
const nodes = emojisTrie.getAllMatchingWords(emojiData[0].toLowerCase().slice(1));
for (let j = 0; j < nodes.length; j++) {
if (nodes[j].metaData.code && !_.find(matching, obj => obj.name === nodes[j].name)) {
if (matching.length === limit) {
return matching;
}
matching.unshift({code: nodes[j].metaData.code, name: nodes[j].name});
}
const suggestions = nodes[j].metaData.suggestions;
for (let i = 0; i < suggestions.length; i++) {
if (matching.length === limit) {
return matching;
}
if (!_.find(matching, obj => obj.name === suggestions[i].name)) {
matching.unshift(suggestions[i]);
}
}
}
return matching;
}
return [];
}
export {
getDynamicHeaderIndices,
mergeEmojisWithFrequentlyUsedEmojis,
addToFrequentlyUsedEmojis,
containsOnlyEmojis,
replaceEmojis,
suggestEmojis,
trimEmojiUnicode,
};