@monstermann/fn
Version:
A utility library for TypeScript.
23 lines (21 loc) • 447 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/array/random.ts
/**
* `random(array)`
*
* Returns a random element from `array`, or `undefined` if the array is empty.
*
* ```ts
* random([1, 2, 3, 4]); // 2 (random)
* ```
*
* ```ts
* pipe([1, 2, 3, 4], random()); // 2 (random)
* ```
*/
const random = dfdlT((target) => {
const idx = Math.floor(Math.random() * target.length);
return target[idx];
}, 1);
//#endregion
export { random };