Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
689 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | var baseRandom = require('./_baseRandom');
|
---|
| 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 | */
|
---|
| 11 | function 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 |
|
---|
| 28 | module.exports = shuffleSelf;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.