mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-02 16:17:50 +00:00
Fix _.shuffle on small collections. [closes #609]
This commit is contained in:
15
lodash.js
15
lodash.js
@@ -5242,19 +5242,16 @@
|
||||
function shuffle(collection) {
|
||||
collection = toIterable(collection);
|
||||
|
||||
var index = 0,
|
||||
var index = -1,
|
||||
length = collection.length,
|
||||
result = Array(length);
|
||||
|
||||
if (length) {
|
||||
result[index] = collection[index];
|
||||
}
|
||||
while (++index < length) {
|
||||
var value = collection[index],
|
||||
rand = baseRandom(0, index - 1);
|
||||
|
||||
result[index] = result[rand];
|
||||
result[rand] = value;
|
||||
var rand = baseRandom(0, index);
|
||||
if (index != rand) {
|
||||
result[index] = result[rand];
|
||||
}
|
||||
result[rand] = collection[index];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -8907,9 +8907,12 @@
|
||||
deepEqual(_.shuffle(object).sort(), array);
|
||||
});
|
||||
|
||||
test('should shuffle an object', 1, function() {
|
||||
var actual = _.shuffle(object);
|
||||
deepEqual(actual.sort(), array);
|
||||
test('should shuffle small collections', 1, function() {
|
||||
var actual = _.times(1000, function() {
|
||||
return _.shuffle([1, 2]);
|
||||
});
|
||||
|
||||
deepEqual(_.sortBy(_.uniq(actual, String), '0'), [[1, 2], [2, 1]]);
|
||||
});
|
||||
|
||||
test('should treat number values for `collection` as empty', 1, function() {
|
||||
|
||||
Reference in New Issue
Block a user