From 110a6bdd1f4be91123d77aa51c0afae2e0b86ad2 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Thu, 24 Jun 2021 16:13:48 +1000 Subject: [PATCH] Add shuffle helper --- src/helpers/shared.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/helpers/shared.js b/src/helpers/shared.js index a9ed206..5940429 100644 --- a/src/helpers/shared.js +++ b/src/helpers/shared.js @@ -75,3 +75,24 @@ export function groupBy(array, key) { } 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; +}