mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 15:57:48 +00:00
37 lines
905 B
JavaScript
37 lines
905 B
JavaScript
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;
|