aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/common/array.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/core-utils/common/array.ts')
-rw-r--r--shared/core-utils/common/array.ts14
1 files changed, 14 insertions, 0 deletions
diff --git a/shared/core-utils/common/array.ts b/shared/core-utils/common/array.ts
index e1b422165..878ed1ffe 100644
--- a/shared/core-utils/common/array.ts
+++ b/shared/core-utils/common/array.ts
@@ -20,8 +20,22 @@ function uniqify <T> (elements: T[]) {
20 return Array.from(new Set(elements)) 20 return Array.from(new Set(elements))
21} 21}
22 22
23// Thanks: https://stackoverflow.com/a/12646864
24function shuffle <T> (elements: T[]) {
25 const shuffled = [ ...elements ]
26
27 for (let i = shuffled.length - 1; i > 0; i--) {
28 const j = Math.floor(Math.random() * (i + 1));
29
30 [ shuffled[i], shuffled[j] ] = [ shuffled[j], shuffled[i] ]
31 }
32
33 return shuffled
34}
35
23export { 36export {
24 uniqify, 37 uniqify,
25 findCommonElement, 38 findCommonElement,
39 shuffle,
26 arrayify 40 arrayify
27} 41}