UNPKG

diginext-utils

Version:
23 lines 723 B
import { randomIndex } from "./randomIndex.js"; /** * Returns a random element from an array. * * @template T - The type of elements in the array * @param array - The array to get a random element from * @returns A random element, or undefined if array is empty * * @example * ```ts * randomElement([1, 2, 3, 4, 5]); // Random element from array * randomElement([]); // undefined * randomElement(['apple', 'banana', 'cherry']); // Random fruit * ``` */ export function randomElement(array) { if (!Array.isArray(array) || array.length === 0) { return undefined; } const index = randomIndex(array); return index >= 0 ? array[index] : undefined; } //# sourceMappingURL=randomElement.js.map