From 930b034da5aa138cc9c83066bd28c14eca760899 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 1 Apr 2016 17:20:32 -0700 Subject: [PATCH] Make `_.repeat` default `n` to `1` instead of `0`. --- lodash.js | 12 +++++++++--- test/test.js | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lodash.js b/lodash.js index e812be46b..2dc0bf6ae 100644 --- a/lodash.js +++ b/lodash.js @@ -13398,7 +13398,8 @@ * @since 3.0.0 * @category String * @param {string} [string=''] The string to repeat. - * @param {number} [n=0] The number of times to repeat the string. + * @param {number} [n=1] The number of times to repeat the string. + * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`. * @returns {string} Returns the repeated string. * @example * @@ -13411,8 +13412,13 @@ * _.repeat('abc', 0); * // => '' */ - function repeat(string, n) { - return baseRepeat(toString(string), toInteger(n)); + function repeat(string, n, guard) { + if ((guard ? isIterateeCall(string, n, guard) : n === undefined)) { + n = 1; + } else { + n = toInteger(n); + } + return baseRepeat(toString(string), n); } /** diff --git a/test/test.js b/test/test.js index 107a8e75f..fc893a171 100644 --- a/test/test.js +++ b/test/test.js @@ -18298,35 +18298,57 @@ QUnit.module('lodash.repeat'); (function() { + var string = 'abc'; + QUnit.test('should repeat a string `n` times', function(assert) { assert.expect(2); assert.strictEqual(_.repeat('*', 3), '***'); - assert.strictEqual(_.repeat('abc', 2), 'abcabc'); + assert.strictEqual(_.repeat(string, 2), 'abcabc'); }); - QUnit.test('should return an empty string for negative `n` or `n` of `0`', function(assert) { + QUnit.test('should treat falsey `n` values, except `undefined`, as `0`', function(assert) { + assert.expect(1); + + var expected = lodashStable.map(falsey, function(value) { + return value === undefined ? string : ''; + }); + + var actual = lodashStable.map(falsey, function(n, index) { + return index ? _.repeat(string, n) : _.repeat(string); + }); + + assert.deepEqual(actual, expected); + }); + + QUnit.test('should return an empty string if `n` is <= `0`', function(assert) { assert.expect(2); - assert.strictEqual(_.repeat('abc', 0), ''); - assert.strictEqual(_.repeat('abc', -2), ''); + assert.strictEqual(_.repeat(string, 0), ''); + assert.strictEqual(_.repeat(string, -2), ''); }); QUnit.test('should coerce `n` to an integer', function(assert) { - assert.expect(4); + assert.expect(3); - assert.strictEqual(_.repeat('abc'), ''); - assert.strictEqual(_.repeat('abc', '2'), 'abcabc'); - assert.strictEqual(_.repeat('abc', 2.6), 'abcabc'); + assert.strictEqual(_.repeat(string, '2'), 'abcabc'); + assert.strictEqual(_.repeat(string, 2.6), 'abcabc'); assert.strictEqual(_.repeat('*', { 'valueOf': alwaysThree }), '***'); }); QUnit.test('should coerce `string` to a string', function(assert) { assert.expect(2); - assert.strictEqual(_.repeat(Object('abc'), 2), 'abcabc'); + assert.strictEqual(_.repeat(Object(string), 2), 'abcabc'); assert.strictEqual(_.repeat({ 'toString': lodashStable.constant('*') }, 3), '***'); }); + + QUnit.test('should work as an iteratee for methods like `_.map`', function(assert) { + assert.expect(1); + + var actual = lodashStable.map(['a', 'b', 'c'], _.repeat); + assert.deepEqual(actual, ['a', 'b', 'c']); + }); }()); /*--------------------------------------------------------------------------*/