File
Blob: src/shared/emoji/asset-lookup.ts
| 1 | import { EMOJI_ICON_DATA } from "./generated/emoji-icon-data"; |
| 2 | import { buildEmojiAssetUrl, normalizeEmoji } from "./shared"; |
| 3 | |
| 4 | const emojiIconData = EMOJI_ICON_DATA as Record<string, string>; |
| 5 | const SKIN_TONE_MODIFIER_RE = /[\u{1F3FB}-\u{1F3FF}]/u; |
| 6 | |
| 7 | function deriveToneAsset(emoji: string): string | null { |
| 8 | if (!SKIN_TONE_MODIFIER_RE.test(emoji)) { |
| 9 | return null; |
| 10 | } |
| 11 | |
| 12 | const codePoints: string[] = []; |
| 13 | for (const glyph of emoji) { |
| 14 | const codePoint = glyph.codePointAt(0); |
| 15 | if (codePoint === undefined || codePoint === 0xfe0e) { |
| 16 | continue; |
| 17 | } |
| 18 | codePoints.push(codePoint.toString(16)); |
| 19 | } |
| 20 | return codePoints.length > 0 ? `${codePoints.join("-")}.png` : null; |
| 21 | } |
| 22 | |
| 23 | export function getEmojiAsset(emoji: string): string | null { |
| 24 | const normalizedEmoji = normalizeEmoji(emoji); |
| 25 | if (normalizedEmoji === "") { |
| 26 | return null; |
| 27 | } |
| 28 | return emojiIconData[normalizedEmoji] ?? deriveToneAsset(emoji); |
| 29 | } |
| 30 | |
| 31 | export function getEmojiAssetUrl(emoji: string): string | null { |
| 32 | const asset = getEmojiAsset(emoji); |
| 33 | return asset ? buildEmojiAssetUrl(asset) : null; |
| 34 | } |