UNPKG

shuffle-array

Version:

Randomize the order of the elements in a given array.

83 lines (66 loc) 2.03 kB
'use strict'; /** * Randomize the order of the elements in a given array. * @param {Array} arr - The given array. * @param {Object} [options] - Optional configuration options. * @param {Boolean} [options.copy] - Sets if should return a shuffled copy of the given array. By default it's a falsy value. * @param {Function} [options.rng] - Specifies a custom random number generator. * @returns {Array} */ function shuffle(arr, options) { if (!Array.isArray(arr)) { throw new Error('shuffle expect an array as parameter.'); } options = options || {}; var collection = arr, len = arr.length, rng = options.rng || Math.random, random, temp; if (options.copy === true) { collection = arr.slice(); } while (len) { random = Math.floor(rng() * len); len -= 1; temp = collection[len]; collection[len] = collection[random]; collection[random] = temp; } return collection; }; /** * Pick one or more random elements from the given array. * @param {Array} arr - The given array. * @param {Object} [options] - Optional configuration options. * @param {Number} [options.picks] - Specifies how many random elements you want to pick. By default it picks 1. * @param {Function} [options.rng] - Specifies a custom random number generator. * @returns {Object} */ shuffle.pick = function(arr, options) { if (!Array.isArray(arr)) { throw new Error('shuffle.pick() expect an array as parameter.'); } options = options || {}; var rng = options.rng || Math.random, picks = options.picks || 1; if (typeof picks === 'number' && picks !== 1) { var len = arr.length, collection = arr.slice(), random = [], index; while (picks && len) { index = Math.floor(rng() * len); random.push(collection[index]); collection.splice(index, 1); len -= 1; picks -= 1; } return random; } return arr[Math.floor(rng() * arr.length)]; }; /** * Expose */ module.exports = shuffle;