Make _.sampleSize default n to 1 instead of 0.

This commit is contained in:
John-David Dalton
2016-04-01 18:03:53 -07:00
parent f599c4817a
commit 58f93567fc
2 changed files with 23 additions and 10 deletions

View File

@@ -8705,7 +8705,8 @@
* @since 4.0.0
* @category Collection
* @param {Array|Object} collection The collection to sample.
* @param {number} [n=0] The number of elements to sample.
* @param {number} [n=1] The number of elements to sample.
* @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
* @returns {Array} Returns the random elements.
* @example
*
@@ -8715,13 +8716,17 @@
* _.sampleSize([1, 2, 3], 4);
* // => [2, 3, 1]
*/
function sampleSize(collection, n) {
function sampleSize(collection, n, guard) {
var index = -1,
result = toArray(collection),
length = result.length,
lastIndex = length - 1;
n = baseClamp(toInteger(n), 0, length);
if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
n = 1;
} else {
n = baseClamp(toInteger(n), 0, length);
}
while (++index < n) {
var rand = baseRandom(index, lastIndex),
value = result[rand];