lib.js (4979B)
1 (function (root) { 2 'use strict'; 3 4 const ANIMALS = [ 5 // A 6 { key: 'alligator', letter: 'A', name: 'Alligator', codepoint: '1f40a' }, 7 { key: 'ant', letter: 'A', name: 'Ant', codepoint: '1f41c' }, 8 // B 9 { key: 'bear', letter: 'B', name: 'Bear', codepoint: '1f43b' }, 10 { key: 'bee', letter: 'B', name: 'Bee', codepoint: '1f41d' }, 11 { key: 'butterfly', letter: 'B', name: 'Butterfly', codepoint: '1f98b' }, 12 // C 13 { key: 'camel', letter: 'C', name: 'Camel', codepoint: '1f42a' }, 14 { key: 'cat', letter: 'C', name: 'Cat', codepoint: '1f431' }, 15 { key: 'chicken', letter: 'C', name: 'Chicken', codepoint: '1f414' }, 16 { key: 'cow', letter: 'C', name: 'Cow', codepoint: '1f404' }, 17 { key: 'crab', letter: 'C', name: 'Crab', codepoint: '1f980' }, 18 // D 19 { key: 'deer', letter: 'D', name: 'Deer', codepoint: '1f98c' }, 20 { key: 'dog', letter: 'D', name: 'Dog', codepoint: '1f436' }, 21 { key: 'dolphin', letter: 'D', name: 'Dolphin', codepoint: '1f42c' }, 22 { key: 'duck', letter: 'D', name: 'Duck', codepoint: '1f986' }, 23 // E 24 { key: 'eagle', letter: 'E', name: 'Eagle', codepoint: '1f985' }, 25 { key: 'elephant', letter: 'E', name: 'Elephant', codepoint: '1f418' }, 26 // F 27 { key: 'fish', letter: 'F', name: 'Fish', codepoint: '1f41f' }, 28 { key: 'flamingo', letter: 'F', name: 'Flamingo', codepoint: '1f9a9' }, 29 { key: 'fox', letter: 'F', name: 'Fox', codepoint: '1f98a' }, 30 { key: 'frog', letter: 'F', name: 'Frog', codepoint: '1f438' }, 31 // G 32 { key: 'giraffe', letter: 'G', name: 'Giraffe', codepoint: '1f992' }, 33 { key: 'goat', letter: 'G', name: 'Goat', codepoint: '1f410' }, 34 { key: 'gorilla', letter: 'G', name: 'Gorilla', codepoint: '1f98d' }, 35 // H 36 { key: 'hamster', letter: 'H', name: 'Hamster', codepoint: '1f439' }, 37 { key: 'hippo', letter: 'H', name: 'Hippo', codepoint: '1f99b' }, 38 { key: 'horse', letter: 'H', name: 'Horse', codepoint: '1f434' }, 39 // K 40 { key: 'kangaroo', letter: 'K', name: 'Kangaroo', codepoint: '1f998' }, 41 { key: 'koala', letter: 'K', name: 'Koala', codepoint: '1f428' }, 42 // L 43 { key: 'ladybug', letter: 'L', name: 'Ladybug', codepoint: '1f41e' }, 44 { key: 'lion', letter: 'L', name: 'Lion', codepoint: '1f981' }, 45 // M 46 { key: 'monkey', letter: 'M', name: 'Monkey', codepoint: '1f435' }, 47 { key: 'mouse', letter: 'M', name: 'Mouse', codepoint: '1f42d' }, 48 // O 49 { key: 'octopus', letter: 'O', name: 'Octopus', codepoint: '1f419' }, 50 { key: 'otter', letter: 'O', name: 'Otter', codepoint: '1f9a6' }, 51 { key: 'owl', letter: 'O', name: 'Owl', codepoint: '1f989' }, 52 // P 53 { key: 'panda', letter: 'P', name: 'Panda', codepoint: '1f43c' }, 54 { key: 'parrot', letter: 'P', name: 'Parrot', codepoint: '1f99c' }, 55 { key: 'penguin', letter: 'P', name: 'Penguin', codepoint: '1f427' }, 56 { key: 'pig', letter: 'P', name: 'Pig', codepoint: '1f437' }, 57 // R 58 { key: 'rabbit', letter: 'R', name: 'Rabbit', codepoint: '1f430' }, 59 { key: 'raccoon', letter: 'R', name: 'Raccoon', codepoint: '1f99d' }, 60 // S 61 { key: 'shark', letter: 'S', name: 'Shark', codepoint: '1f988' }, 62 { key: 'snail', letter: 'S', name: 'Snail', codepoint: '1f40c' }, 63 { key: 'snake', letter: 'S', name: 'Snake', codepoint: '1f40d' }, 64 { key: 'swan', letter: 'S', name: 'Swan', codepoint: '1f9a2' }, 65 // T 66 { key: 'tiger', letter: 'T', name: 'Tiger', codepoint: '1f42f' }, 67 { key: 'turtle', letter: 'T', name: 'Turtle', codepoint: '1f422' }, 68 // U 69 { key: 'unicorn', letter: 'U', name: 'Unicorn', codepoint: '1f984' }, 70 // W 71 { key: 'whale', letter: 'W', name: 'Whale', codepoint: '1f433' }, 72 { key: 'wolf', letter: 'W', name: 'Wolf', codepoint: '1f43a' }, 73 // Z 74 { key: 'zebra', letter: 'Z', name: 'Zebra', codepoint: '1f993' }, 75 ]; 76 77 // Fisher-Yates shuffle — returns a new shuffled array, does not mutate ANIMALS 78 function shuffleAnimals(rngFn) { 79 const arr = ANIMALS.slice(); 80 for (let i = arr.length - 1; i > 0; i--) { 81 const j = Math.floor(rngFn() * (i + 1)); 82 const tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; 83 } 84 return arr; 85 } 86 87 // Kept for unit-test compatibility 88 function pickAnimal(rng, excludeKey) { 89 const pool = excludeKey ? ANIMALS.filter(a => a.key !== excludeKey) : ANIMALS; 90 return pool[Math.floor(rng() * pool.length)]; 91 } 92 93 const api = { ANIMALS, shuffleAnimals, pickAnimal }; 94 if (typeof module !== 'undefined' && module.exports) module.exports = api; 95 root.AnimalLetterLib = api; 96 }(typeof globalThis !== 'undefined' ? globalThis : this));