You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 line
689 B

4 年之前
  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;