mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 08:57:49 +00:00
Bump to v3.9.2.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
var baseRandom = require('../internal/baseRandom'),
|
||||
isIterateeCall = require('../internal/isIterateeCall'),
|
||||
shuffle = require('./shuffle'),
|
||||
toArray = require('../lang/toArray'),
|
||||
toIterable = require('../internal/toIterable');
|
||||
|
||||
/* Native method references for those with the same name as other `lodash` methods. */
|
||||
@@ -30,8 +30,20 @@ function sample(collection, n, guard) {
|
||||
var length = collection.length;
|
||||
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
|
||||
}
|
||||
var result = shuffle(collection);
|
||||
result.length = nativeMin(n < 0 ? 0 : (+n || 0), result.length);
|
||||
var index = -1,
|
||||
result = toArray(collection),
|
||||
length = result.length,
|
||||
lastIndex = length - 1;
|
||||
|
||||
n = nativeMin(n < 0 ? 0 : (+n || 0), length);
|
||||
while (++index < n) {
|
||||
var rand = baseRandom(index, lastIndex),
|
||||
value = result[rand];
|
||||
|
||||
result[rand] = result[index];
|
||||
result[index] = value;
|
||||
}
|
||||
result.length = n;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user