forked from snario/allhands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emoji.ts
52 lines (42 loc) · 1.58 KB
/
emoji.ts
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
import EmojiMapJson from "./emojiMap.json";
const EmojiMap = JSON.parse(JSON.stringify(EmojiMapJson));
export function getEmojiFromJSON(shortcode?: string | null) {
if (typeof shortcode === "undefined") return null;
if (shortcode === null) return null;
return findMatchingEmoji(shortcode);
}
export default {
getEmojiFromJSON,
};
export type EmojiData = {
short_name: string;
name: string;
short_names: string[];
unified: string;
};
function findMatchingEmoji(searchTerm: string) {
searchTerm = searchTerm.toLowerCase().replace(/:/g, ""); // Remove colons for exact match
// First, check for an exact match with short_name
const exactMatch = EmojiMap.find((emojiData: EmojiData) => {
return emojiData.short_name.toLowerCase() === searchTerm;
});
if (exactMatch) {
return unicodeToEmoji(exactMatch.unified); // Return the emoji character if exact match is found
}
// If no exact match, proceed with a broader search and return the first match found
const broadMatch = EmojiMap.find((emojiData: EmojiData) => {
return (
emojiData.name.toLowerCase().includes(searchTerm) ||
emojiData.short_name.toLowerCase().includes(searchTerm) ||
emojiData.short_names.some((shortName) =>
shortName.toLowerCase().includes(searchTerm),
)
);
});
return broadMatch ? unicodeToEmoji(broadMatch.unified) : null;
}
function unicodeToEmoji(unified: string) {
return String.fromCodePoint(
...unified.split("-").map((u) => parseInt(`0x` + u)),
);
}