/** * Fruit emojis paired with their avatar background color. * Single source of truth for both `getAvatarEmoji` and `getAvatarBgColor`. */ const FRUITS = [ { emoji: '🍎', bgColor: '#FFD1DD' }, // Apple { emoji: '🍌', bgColor: '#FFF1C9' }, // Banana { emoji: '🍇', bgColor: '#E6D9FF' }, // Grape { emoji: '🍊', bgColor: '#D6F4FF' }, // Orange { emoji: '🍓', bgColor: '#FFE0AE' }, // Strawberry { emoji: '🥥', bgColor: '#F7E7D8' }, // Coconut { emoji: '🍒', bgColor: '#DFF4D8' }, // Cherry { emoji: '🥭', bgColor: '#FFD8C2' }, // Mango { emoji: '🍉', bgColor: '#D6EBFF' }, // Watermelon { emoji: '🍋', bgColor: '#CAEEB5' }, // Lemon { emoji: '🥝', bgColor: '#FFE7C7' }, // Kiwi { emoji: '🫒', bgColor: '#F6E3B4' }, // Olive { emoji: '🍈', bgColor: '#D9F7E1' }, // Melon ] /** * Simple hash function to convert string to number */ function hashString(str: string): number { let hash = 0 for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i) hash = (hash << 5) - hash + char hash = hash & hash // Convert to 32-bit integer } return Math.abs(hash) } /** * Pick a consistent fruit for an id string */ function getFruit(id: string) { return FRUITS[hashString(id) % FRUITS.length] } /** * Get a fruit emoji based on an id string * @param id - The string id to generate emoji from * @returns A fruit emoji string */ export function getAvatarEmoji(id: string): string { return getFruit(id).emoji } /** * Get the background color paired with an id's fruit emoji * @param id - The string id to generate the color from * @returns A hex color string */ export function getAvatarBgColor(id: string): string { return getFruit(id).bgColor }