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 * @since 4.0.0
* @category Collection * @category Collection
* @param {Array|Object} collection The collection to sample. * @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. * @returns {Array} Returns the random elements.
* @example * @example
* *
@@ -8715,13 +8716,17 @@
* _.sampleSize([1, 2, 3], 4); * _.sampleSize([1, 2, 3], 4);
* // => [2, 3, 1] * // => [2, 3, 1]
*/ */
function sampleSize(collection, n) { function sampleSize(collection, n, guard) {
var index = -1, var index = -1,
result = toArray(collection), result = toArray(collection),
length = result.length, length = result.length,
lastIndex = length - 1; 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) { while (++index < n) {
var rand = baseRandom(index, lastIndex), var rand = baseRandom(index, lastIndex),
value = result[rand]; value = result[rand];

View File

@@ -18982,13 +18982,15 @@
assert.deepEqual(actual, array); assert.deepEqual(actual, array);
}); });
QUnit.test('should treat falsey `n` values as `0`', function(assert) { QUnit.test('should treat falsey `size` values, except `undefined`, as `0`', function(assert) {
assert.expect(1); assert.expect(1);
var expected = lodashStable.map(falsey, alwaysEmptyArray); var expected = lodashStable.map(falsey, function(value) {
return value === undefined ? ['a'] : [];
});
var actual = lodashStable.map(falsey, function(n, index) { var actual = lodashStable.map(falsey, function(size, index) {
return index ? _.sampleSize([1], n) : _.sampleSize([1]); return index ? _.sampleSize(['a'], size) : _.sampleSize(['a']);
}); });
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
@@ -19041,6 +19043,13 @@
assert.strictEqual(actual.length, 2); assert.strictEqual(actual.length, 2);
assert.deepEqual(lodashStable.difference(actual, lodashStable.values(object)), []); assert.deepEqual(lodashStable.difference(actual, lodashStable.values(object)), []);
}); });
QUnit.test('should work as an iteratee for methods like `_.map`', function(assert) {
assert.expect(1);
var actual = lodashStable.map([['a']], _.sampleSize);
assert.deepEqual(actual, [['a']]);
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -25377,7 +25386,6 @@
'rangeRight', 'rangeRight',
'reject', 'reject',
'remove', 'remove',
'sampleSize',
'shuffle', 'shuffle',
'sortBy', 'sortBy',
'tail', 'tail',
@@ -25397,7 +25405,7 @@
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) { QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(308); assert.expect(307);
var emptyArrays = lodashStable.map(falsey, alwaysEmptyArray); var emptyArrays = lodashStable.map(falsey, alwaysEmptyArray);
@@ -25435,7 +25443,7 @@
}); });
QUnit.test('should return an array', function(assert) { QUnit.test('should return an array', function(assert) {
assert.expect(72); assert.expect(70);
var array = [1, 2, 3]; var array = [1, 2, 3];