mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 23:37:49 +00:00
Make _.repeat default n to 1 instead of 0.
This commit is contained in:
12
lodash.js
12
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
40
test/test.js
40
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']);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user