diff --git a/lodash.js b/lodash.js index bada755de..b13c80e42 100644 --- a/lodash.js +++ b/lodash.js @@ -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]; diff --git a/test/test.js b/test/test.js index 6b7866f87..662d4bf96 100644 --- a/test/test.js +++ b/test/test.js @@ -18982,13 +18982,15 @@ 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); - 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) { - return index ? _.sampleSize([1], n) : _.sampleSize([1]); + var actual = lodashStable.map(falsey, function(size, index) { + return index ? _.sampleSize(['a'], size) : _.sampleSize(['a']); }); assert.deepEqual(actual, expected); @@ -19041,6 +19043,13 @@ assert.strictEqual(actual.length, 2); 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', 'reject', 'remove', - 'sampleSize', 'shuffle', 'sortBy', 'tail', @@ -25397,7 +25405,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(308); + assert.expect(307); var emptyArrays = lodashStable.map(falsey, alwaysEmptyArray); @@ -25435,7 +25443,7 @@ }); QUnit.test('should return an array', function(assert) { - assert.expect(72); + assert.expect(70); var array = [1, 2, 3];