diginext-utils
Version:
README.md
26 lines • 851 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.randomElement = randomElement;
const randomIndex_1 = require("./randomIndex");
/**
* 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
* ```
*/
function randomElement(array) {
if (!Array.isArray(array) || array.length === 0) {
return undefined;
}
const index = (0, randomIndex_1.randomIndex)(array);
return index >= 0 ? array[index] : undefined;
}
//# sourceMappingURL=randomElement.js.map