“Draw” a random item from an array. The item is not removed from the array. Returns `null` if the array is empty.
const numbers = [1, 2, 3]; draw(numbers); // => 2numbers; // => [1, 2, 3]
npx atmx add helper draw
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
import { random } from "@/helpers/random.ts"; /*** “Draw” a random item from an array. The item is not removed from* the array. Returns `null` if the array is empty.** @example* const numbers = [1, 2, 3]** draw(numbers) // => 2* numbers // => [1, 2, 3]*/export function draw<T>(array: readonly T[]): T | null {const max = array.length; if (max === 0) {return null;} const index = random(0, max - 1); return array[index];}