_shuffleSelf.js 689 B

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