* Creates an array of shuffled values, using a version of the Fisher-Yates * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. * * @static * @memberOf _ * @category Collections * @param {Array|Object|string} collection The collection to shuffle. * @re
(collection)
| 38998 | * // => [4, 1, 6, 3, 5, 2] |
| 38999 | */ |
| 39000 | function shuffle(collection) { |
| 39001 | var index = -1, |
| 39002 | length = collection ? collection.length : 0, |
| 39003 | result = Array(typeof length == 'number' ? length : 0); |
| 39004 | |
| 39005 | forEach(collection, function(value) { |
| 39006 | var rand = baseRandom(0, ++index); |
| 39007 | result[index] = result[rand]; |
| 39008 | result[rand] = value; |
| 39009 | }); |
| 39010 | return result; |
| 39011 | } |
| 39012 | |
| 39013 | /** |
| 39014 | * Gets the size of the `collection` by returning `collection.length` for arrays |
no test coverage detected