f-utility
Version:
functional utilities
32 lines (29 loc) • 979 B
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
/**
* Shuffle the contents of an array
* @function random.shuffle
* @param {Array} list - an array to be shuffled
* @return {Array} shuffled
* @public
* @example
* import {random} from 'f-utility'
* const {shuffle} = random
* const shuffle(`abcde`.split(``)) // randomly shuffled array
*/
var shuffle = exports.shuffle = function shuffle(list) {
var newList = [].concat(_toConsumableArray(list));
// modified fisher-yates shuffle
var start = newList.length;
while (start-- > 0) {
var index = Math.floor(Math.random() * start + 1);
var current = newList[index];
var newer = newList[start];
newList[index] = newer;
newList[start] = current;
}
return newList;
};
;