From 2ce9e09e8bbf55dcc4e4df10ac3b9cb602fca5a8 Mon Sep 17 00:00:00 2001 From: jdalton Date: Thu, 30 Apr 2015 09:06:48 -0700 Subject: [PATCH] Ensure `_.padLeft` and `_.padRight` handle empty strings correctly. --- lodash.src.js | 2 +- test/test.js | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index d0474f654..2a0b1687f 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -3638,7 +3638,7 @@ function createPadDir(fromRight) { return function(string, length, chars) { string = baseToString(string); - return string && ((fromRight ? string : '') + createPadding(string, length, chars) + (fromRight ? '' : string)); + return (fromRight ? string : '') + createPadding(string, length, chars) + (fromRight ? '' : string); }; } diff --git a/test/test.js b/test/test.js index a521d33ac..b1bf54805 100644 --- a/test/test.js +++ b/test/test.js @@ -11474,6 +11474,7 @@ _.each(['pad', 'padLeft', 'padRight'], function(methodName) { var func = _[methodName], + isPad = methodName == 'pad', isPadLeft = methodName == 'padLeft'; test('`_.' + methodName + '` should not pad is string is >= `length`', 2, function() { @@ -11494,11 +11495,12 @@ }); }); - test('`_.' + methodName + '` should return an empty string when provided nullish or empty string values and `chars`', 6, function() { + test('`_.' + methodName + '` should treat nullish values as empty strings', 6, function() { _.each([null, '_-'], function(chars) { - strictEqual(func(null, 0, chars), ''); - strictEqual(func(undefined, 0, chars), ''); - strictEqual(func('', 0, chars), ''); + var expected = chars ? (isPad ? '__' : chars) : ' '; + strictEqual(func(null, 2, chars), expected); + strictEqual(func(undefined, 2, chars), expected); + strictEqual(func('', 2, chars), expected); }); });