mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-13 04:17:49 +00:00
Optimize _.sample.
This commit is contained in:
@@ -3888,11 +3888,14 @@
|
|||||||
* // => [3, 1]
|
* // => [3, 1]
|
||||||
*/
|
*/
|
||||||
function sample(collection, n, guard) {
|
function sample(collection, n, guard) {
|
||||||
if (!isArray(collection)) {
|
var length = collection ? collection.length : 0;
|
||||||
collection = toArray(collection);
|
if (typeof length != 'number') {
|
||||||
|
collection = values(collection);
|
||||||
|
} else if (support.unindexedChars && isString(collection)) {
|
||||||
|
collection = collection.split('');
|
||||||
}
|
}
|
||||||
if (n == null || guard) {
|
if (n == null || guard) {
|
||||||
return collection[random(collection.length - 1)];
|
return collection[random(length - 1)];
|
||||||
}
|
}
|
||||||
var result = shuffle(collection);
|
var result = shuffle(collection);
|
||||||
result.length = nativeMin(nativeMax(0, n), result.length);
|
result.length = nativeMin(nativeMax(0, n), result.length);
|
||||||
|
|||||||
Reference in New Issue
Block a user