Sha256: 947f5ebc766d007f4e1ac6d870daf39055ea4849875549c821980b93833f3698
Contents?: true
Size: 905 Bytes
Versions: 14
Compression:
Stored size: 905 Bytes
Contents
var baseRandom = require('../internal/baseRandom'), toIterable = require('../internal/toIterable'); /** * Creates an array of shuffled values, using a version of the Fisher-Yates * shuffle. See [Wikipedia](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle) * for more details. * * @static * @memberOf _ * @category Collection * @param {Array|Object|string} collection The collection to shuffle. * @returns {Array} Returns the new shuffled array. * @example * * _.shuffle([1, 2, 3, 4]); * // => [4, 1, 3, 2] */ function shuffle(collection) { collection = toIterable(collection); var index = -1, length = collection.length, result = Array(length); while (++index < length) { var rand = baseRandom(0, index); if (index != rand) { result[index] = result[rand]; } result[rand] = collection[index]; } return result; } module.exports = shuffle;
Version data entries
14 entries across 7 versions & 1 rubygems