forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emoji_utils.jsx
72 lines (59 loc) · 2.17 KB
/
emoji_utils.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
const defaultRule = (aName, bName, emojiA, emojiB) => {
if (emojiA && emojiB && emojiA.category === 'custom' && emojiB.category !== 'custom') {
return 1;
} else if (emojiB && emojiA && emojiB.category === 'custom' && emojiA.category !== 'custom') {
return -1;
}
return aName.localeCompare(bName);
};
const thumbsDownRule = (otherName) => {
if (otherName === 'thumbsup' || otherName === '+1') {
return 1;
}
return 0;
};
const thumbsUpRule = (otherName) => {
if (otherName === 'thumbsdown' || otherName === '-1') {
return -1;
}
return 0;
};
const customRules = {
thumbsdown: thumbsDownRule,
'-1': thumbsDownRule,
thumbsup: thumbsUpRule,
'+1': thumbsUpRule,
};
const getEmojiName = (emoji, searchedName) => {
// There's an edge case for custom emojis that start with a thumb prefix.
// It doesn't match the first alias for the relevant system emoji.
// We don't have control over the names or aliases of custom emojis...
// ... and how they compare to the relevant system ones.
// So we need to search for a matching alias in the whole array.
// E.g. thumbsup-custom vs [+1, thumbsup]
if (!emoji || !(emoji.name || emoji.aliases)) {
return '';
}
if (searchedName) {
return emoji.name || emoji.aliases.find((alias) => alias.startsWith(searchedName)) || emoji.aliases[0];
}
return emoji.name || emoji.aliases[0];
};
export function compareEmojis(emojiA, emojiB, searchedName) {
const aName = getEmojiName(emojiA, searchedName);
const bName = getEmojiName(emojiB, searchedName);
// Have the emojis that contain the search appear first
const aPrefix = aName.startsWith(searchedName);
const bPrefix = bName.startsWith(searchedName);
if (aPrefix === bPrefix) {
if (customRules[aName]) {
return customRules[aName](bName, emojiB) || defaultRule(aName, bName, emojiA, emojiB);
}
return defaultRule(aName, bName, emojiA, emojiB);
} else if (aPrefix) {
return -1;
}
return 1;
}