diff --git a/lodash.js b/lodash.js index 811de35ca..61bd160be 100644 --- a/lodash.js +++ b/lodash.js @@ -4556,12 +4556,13 @@ function createPadding(string, length, chars) { length = toInteger(length); - var strLength = stringSize(string); + var strLength = length ? stringSize(string) : 0; if (!length || strLength >= length) { return ''; } var padLength = length - strLength; chars = chars === undefined ? ' ' : (chars + ''); + chars = chars == '' ? ' ' : chars; var result = repeat(chars, nativeCeil(padLength / stringSize(chars))); return reHasComplexSymbol.test(chars) @@ -13044,7 +13045,7 @@ string = toString(string); length = toInteger(length); - var strLength = stringSize(string); + var strLength = length ? stringSize(string) : 0; if (!length || strLength >= length) { return string; } diff --git a/test/test.js b/test/test.js index 2ab24276f..c596fe2a1 100644 --- a/test/test.js +++ b/test/test.js @@ -16169,6 +16169,19 @@ assert.strictEqual(_.pad(Object('abc'), 4), 'abc '); assert.strictEqual(_.pad({ 'toString': lodashStable.constant('abc') }, 5), ' abc '); }); + + QUnit.test('should use " " in place of `undefined` or empty string `chars` values', function(assert) { + assert.expect(1); + + var values = [undefined, ''], + expected = lodashStable.map(values, lodashStable.constant(' abc ')); + + var actual = lodashStable.map(values, function(value) { + return _.pad('abc', 6, value); + }); + + assert.deepEqual(actual, expected); + }); }()); /*--------------------------------------------------------------------------*/ @@ -16194,6 +16207,19 @@ assert.strictEqual(_.padEnd(Object('abc'), 4), 'abc '); assert.strictEqual(_.padEnd({ 'toString': lodashStable.constant('abc') }, 5), 'abc '); }); + + QUnit.test('should use " " in place of `undefined` or empty string `chars` values', function(assert) { + assert.expect(1); + + var values = [undefined, ''], + expected = lodashStable.map(values, lodashStable.constant('abc ')); + + var actual = lodashStable.map(values, function(value) { + return _.padEnd('abc', 6, value); + }); + + assert.deepEqual(actual, expected); + }); }()); /*--------------------------------------------------------------------------*/ @@ -16219,6 +16245,19 @@ assert.strictEqual(_.padStart(Object('abc'), 4), ' abc'); assert.strictEqual(_.padStart({ 'toString': lodashStable.constant('abc') }, 5), ' abc'); }); + + QUnit.test('should use " " in place of `undefined` or empty string `chars` values', function(assert) { + assert.expect(1); + + var values = [undefined, ''], + expected = lodashStable.map(values, lodashStable.constant(' abc')); + + var actual = lodashStable.map(values, function(value) { + return _.padStart('abc', 6, value); + }); + + assert.deepEqual(actual, expected); + }); }()); /*--------------------------------------------------------------------------*/ @@ -16264,14 +16303,6 @@ assert.strictEqual(func('', 2, chars), expected); }); }); - - QUnit.test('`_.' + methodName + '` should work with nullish or empty string values for `chars`', function(assert) { - assert.expect(3); - - assert.notStrictEqual(func('abc', 6, null), 'abc'); - assert.notStrictEqual(func('abc', 6, undefined), 'abc'); - assert.strictEqual(func('abc', 6, ''), 'abc'); - }); }); /*--------------------------------------------------------------------------*/