UNPKG

687 BJavaScriptView Raw
1import baseRandom from './_baseRandom.js';
2
3/**
4 * A specialized version of `_.shuffle` which mutates and sets the size of `array`.
5 *
6 * @private
7 * @param {Array} array The array to shuffle.
8 * @param {number} [size=array.length] The size of `array`.
9 * @returns {Array} Returns `array`.
10 */
11function shuffleSelf(array, size) {
12 var index = -1,
13 length = array.length,
14 lastIndex = length - 1;
15
16 size = size === undefined ? length : size;
17 while (++index < size) {
18 var rand = baseRandom(index, lastIndex),
19 value = array[rand];
20
21 array[rand] = array[index];
22 array[index] = value;
23 }
24 array.length = size;
25 return array;
26}
27
28export default shuffleSelf;