Generate random, alliterated animal names.
at main 890 B view raw
1/** 2 * Generates a randomized, two-word, alliterated animal name. 3 * 4 * ```ts 5 * import { expect } from "jsr:@std/expect"; 6 * 7 * const name = await generateRandomName(); 8 * 9 * expect(name.split(" ")).toHaveLength(2); 10 * 11 * const [adjective, animal] = name.split(" "); 12 * 13 * expect(adjective[0]).toEqual(animal[0]); 14 * ``` 15 */ 16export async function generateRandomName(): Promise<string> { 17 const [{ default: adjectives }, { default: animals }] = await Promise.all([ 18 import("./data/adjectives.json", { with: { type: "json" } }), 19 import("./data/animals.json", { with: { type: "json" } }), 20 ]); 21 22 const animal = animals[Math.floor(Math.random() * animals.length)]; 23 const letter = animal[0].toUpperCase() as keyof typeof adjectives; 24 const options = adjectives[letter]; 25 const adjective = options[Math.floor(Math.random() * options.length)]; 26 27 return `${adjective} ${animal}`; 28}