UNPKG

656 BJavaScriptView Raw
1import sampleSize from './sampleSize';
2
3/** Used as references for the maximum length and index of an array. */
4var MAX_ARRAY_LENGTH = 4294967295;
5
6/**
7 * Creates an array of shuffled values, using a version of the
8 * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
9 *
10 * @static
11 * @memberOf _
12 * @since 0.1.0
13 * @category Collection
14 * @param {Array|Object} collection The collection to shuffle.
15 * @returns {Array} Returns the new shuffled array.
16 * @example
17 *
18 * _.shuffle([1, 2, 3, 4]);
19 * // => [4, 1, 3, 2]
20 */
21function shuffle(collection) {
22 return sampleSize(collection, MAX_ARRAY_LENGTH);
23}
24
25export default shuffle;