mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
36 lines
870 B
JavaScript
36 lines
870 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](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
|
|
*
|
|
* @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;
|