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
import EmojiKit
import Foundation
enum EmojiCatalog {
struct Entry: Equatable, Identifiable, Sendable {
let shortcode: String
let emoji: String
var id: String { emoji }
}
private struct SearchEntry {
let emoji: String
let aliases: [String]
}
private struct Match {
let entry: Entry
let rank: Int
func precedes(_ other: Match) -> Bool {
if rank != other.rank { return rank < other.rank }
if entry.shortcode.count != other.entry.shortcode.count {
return entry.shortcode.count < other.entry.shortcode.count
}
if entry.shortcode != other.entry.shortcode { return entry.shortcode < other.entry.shortcode }
return entry.emoji < other.entry.emoji
}
}
private static let entries: [SearchEntry] = {
let english = Locale(identifier: "en")
var seen = Set<String>()
return Emoji.all.compactMap { emoji in
guard seen.insert(emoji.char).inserted else { return nil }
let names = [emoji.localizedName(in: english), emoji.unicodeName].map(shortcode)
let aliases = Array(Set((commonAliases[emoji.char] ?? []) + names)).filter { !$0.isEmpty }.sorted()
return SearchEntry(emoji: emoji.char, aliases: aliases)
}
}()
static func suggestions(for query: String, limit: Int = 8) -> [Entry] {
guard limit > 0, !query.isEmpty else { return [] }
let query = query == "-1" ? query : query.lowercased().replacingOccurrences(of: "-", with: "_")
let matches = entries.compactMap { entry -> Match? in
entry.aliases.compactMap { alias -> Match? in
let rank: Int
if alias == query {
rank = 0
} else if alias.hasPrefix(query) {
rank = 1
} else if alias.split(separator: "_").contains(where: { $0.hasPrefix(query) }) {
rank = 2
} else if alias.contains(query) {
rank = 3
} else {
return nil
}
return Match(entry: Entry(shortcode: alias, emoji: entry.emoji), rank: rank)
}.min { $0.precedes($1) }
}
return matches.sorted { $0.precedes($1) }.prefix(limit).map(\.entry)
}
private static func shortcode(_ name: String) -> String {
let folded = name.folding(options: [.diacriticInsensitive, .caseInsensitive], locale: Locale(identifier: "en"))
return folded.lowercased().components(separatedBy: CharacterSet.alphanumerics.inverted)
.filter { !$0.isEmpty }.joined(separator: "_")
}
private static let commonAliases: [String: [String]] = [
"😀": ["grinning"], "😃": ["smiley"], "😄": ["smile"], "😁": ["grin"],
"😆": ["laughing", "satisfied"], "😅": ["sweat_smile"], "😂": ["joy"], "🤣": ["rofl"],
"😊": ["blush"], "😇": ["innocent"], "😉": ["wink"], "😌": ["relieved"],
"😍": ["heart_eyes"], "😘": ["kissing_heart"], "😋": ["yum"], "😎": ["sunglasses"],
"😏": ["smirk"], "😒": ["unamused"], "😞": ["disappointed"], "😔": ["pensive"],
"😢": ["cry"], "😭": ["sob"], "😤": ["triumph"], "😡": ["rage"], "😠": ["angry"],
"😱": ["scream"], "😳": ["flushed"], "😴": ["sleeping"], "😷": ["mask"],
"🤔": ["thinking", "thinking_face"], "🤗": ["hugs"], "🤩": ["star_struck"],
"🥳": ["partying_face"], "🥺": ["pleading_face"], "🫠": ["melting_face"],
"🙄": ["roll_eyes", "rolling_eyes"], "🤦": ["facepalm"], "🤷": ["shrug"],
"💩": ["poop", "shit", "hankey"], "💯": ["100"], "💥": ["boom", "collision"],
"👍": ["thumbsup", "thumbs_up", "+1"], "👎": ["thumbsdown", "thumbs_down", "-1"],
"👏": ["clap"], "🙌": ["raised_hands"], "🙏": ["pray"], "👋": ["wave"],
"👊": ["punch", "facepunch"], "✊": ["fist"], "🤞": ["crossed_fingers"],
"👀": ["eyes"], "👁": ["eye"], "❤️": ["heart"], "💔": ["broken_heart"],
"🔥": ["fire"], "🚀": ["rocket"], "🎉": ["tada"], "🎊": ["confetti_ball"],
"✨": ["sparkles"], "⭐️": ["star"], "💪": ["muscle"], "✅": ["white_check_mark"],
"❌": ["x"], "⚠️": ["warning"], "☕️": ["coffee"], "🍻": ["beers"],
"🎂": ["birthday"], "🐶": ["dog"], "🐱": ["cat"], "🦋": ["butterfly"]
]
}