shuffle.js 642 B

123456789101112131415161718192021222324
  1. var sample = require('./sample');
  2. /** Used as references for `-Infinity` and `Infinity`. */
  3. var POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
  4. /**
  5. * Creates an array of shuffled values, using a version of the
  6. * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @category Collection
  11. * @param {Array|Object|string} collection The collection to shuffle.
  12. * @returns {Array} Returns the new shuffled array.
  13. * @example
  14. *
  15. * _.shuffle([1, 2, 3, 4]);
  16. * // => [4, 1, 3, 2]
  17. */
  18. function shuffle(collection) {
  19. return sample(collection, POSITIVE_INFINITY);
  20. }
  21. module.exports = shuffle;