mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-04-19 13:40:41 -05:00
17 lines
444 B
TypeScript
17 lines
444 B
TypeScript
export const shuffleArray = <T>(array: T[]) => {
|
|
return array
|
|
.map((a) => ({ sort: Math.random(), value: a }))
|
|
.sort((a, b) => a.sort - b.sort)
|
|
.map((a) => a.value);
|
|
};
|
|
|
|
/**
|
|
* Get random element from array.
|
|
* @link https://stackoverflow.com/a/5915122
|
|
* @example
|
|
* randomElement(["dog", "cat", "horse"])
|
|
*/
|
|
export const randomElement = <T>(items: [T, ...T[]]) => {
|
|
return items[Math.floor(Math.random() * items.length)];
|
|
};
|