Make pad methods default to a chars of " " if chars is an empty string.

This commit is contained in:
John-David Dalton
2016-03-26 11:49:41 -07:00
parent 0f3338013e
commit 0dfe176fe6
2 changed files with 42 additions and 10 deletions

View File

@@ -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;
}

View File

@@ -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');
});
});
/*--------------------------------------------------------------------------*/