mirror of
https://github.com/shoelace-style/webawesome.git
synced 2026-01-12 20:19:13 +00:00
18 lines
277 B
JavaScript
18 lines
277 B
JavaScript
|
|
/**
|
||
|
|
* Picks a random element from an array.
|
||
|
|
* @param {any[]} arr
|
||
|
|
*/
|
||
|
|
export function sample(arr) {
|
||
|
|
if (!Array.isArray(arr)) {
|
||
|
|
return arr;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (arr.length < 2) {
|
||
|
|
return arr[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
let index = Math.floor(Math.random() * arr.length);
|
||
|
|
|
||
|
|
return arr[index];
|
||
|
|
}
|