~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
91 lines · 4.4 KB Raw History
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"]
    ]
}