mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
Break out _.sampleSize and _.shuffle into array helpers.
This commit is contained in:
69
lodash.js
69
lodash.js
@@ -2392,6 +2392,31 @@
|
||||
return length ? array[baseRandom(0, length - 1)] : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* A specialized version of `_.sampleSize` for arrays.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} array The array to sample.
|
||||
* @param {number} n The number of elements to sample.
|
||||
* @returns {Array} Returns the random elements.
|
||||
*/
|
||||
function arraySampleSize(array, n) {
|
||||
var result = arrayShuffle(array);
|
||||
result.length = baseClamp(n, 0, result.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* A specialized version of `_.shuffle` for arrays.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} array The array to shuffle.
|
||||
* @returns {Array} Returns the new shuffled array.
|
||||
*/
|
||||
function arrayShuffle(array) {
|
||||
return shuffleSelf(copyArray(array));
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by `_.defaults` to customize its `_.assignIn` use.
|
||||
*
|
||||
@@ -6514,6 +6539,28 @@
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A specialized version of `arrayShuffle` which mutates `array`.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} array The array to shuffle.
|
||||
* @returns {Array} Returns `array`.
|
||||
*/
|
||||
function shuffleSelf(array) {
|
||||
var index = -1,
|
||||
length = array.length,
|
||||
lastIndex = length - 1;
|
||||
|
||||
while (++index < length) {
|
||||
var rand = baseRandom(index, lastIndex),
|
||||
value = array[rand];
|
||||
|
||||
array[rand] = array[index];
|
||||
array[index] = value;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts `string` to a property path array.
|
||||
*
|
||||
@@ -9612,25 +9659,12 @@
|
||||
* // => [2, 3, 1]
|
||||
*/
|
||||
function sampleSize(collection, n, guard) {
|
||||
var index = -1,
|
||||
result = toArray(collection),
|
||||
length = result.length,
|
||||
lastIndex = length - 1;
|
||||
|
||||
if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
|
||||
n = 1;
|
||||
} else {
|
||||
n = baseClamp(toInteger(n), 0, length);
|
||||
n = toInteger(n);
|
||||
}
|
||||
while (++index < n) {
|
||||
var rand = baseRandom(index, lastIndex),
|
||||
value = result[rand];
|
||||
|
||||
result[rand] = result[index];
|
||||
result[index] = value;
|
||||
}
|
||||
result.length = n;
|
||||
return result;
|
||||
return arraySampleSize(isArrayLike(collection) ? collection : values(collection), n);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -9649,7 +9683,10 @@
|
||||
* // => [4, 1, 3, 2]
|
||||
*/
|
||||
function shuffle(collection) {
|
||||
return sampleSize(collection, MAX_ARRAY_LENGTH);
|
||||
return shuffleSelf(isArrayLike(collection)
|
||||
? copyArray(collection)
|
||||
: values(collection)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user