Add shuffle helper

This commit is contained in:
Mitchell McCaffrey 2021-06-24 16:13:48 +10:00
parent 23662e9fa1
commit 110a6bdd1f

View File

@ -75,3 +75,24 @@ export function groupBy(array, key) {
} }
export const isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform); export const isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
export function shuffle(array) {
let temp = [...array];
var currentIndex = temp.length,
randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[temp[currentIndex], temp[randomIndex]] = [
temp[randomIndex],
temp[currentIndex],
];
}
return temp;
}