UNPKG

669 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.arrayShuffle = void 0;
4/**
5 * @name arrayShuffle
6 * @description Shuffles the input array (unlike sort, this is not done in-place)
7 */
8function arrayShuffle(input) {
9 const result = input.slice();
10 let curr = result.length;
11 // noop for the single entry
12 if (curr === 1) {
13 return result;
14 }
15 while (curr !== 0) {
16 // ~~ is more performant than Math.floor
17 const rand = ~~(Math.random() * curr);
18 curr--;
19 [result[curr], result[rand]] = [result[rand], result[curr]];
20 }
21 return result;
22}
23exports.arrayShuffle = arrayShuffle;