mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-13 12:27:49 +00:00
Use toInteger in pad methods and make nullish chars checks in pad and trim methods undefined checks.
This commit is contained in:
18
lodash.js
18
lodash.js
@@ -3341,13 +3341,13 @@
|
|||||||
*/
|
*/
|
||||||
function createPadding(string, length, chars) {
|
function createPadding(string, length, chars) {
|
||||||
var strLength = string.length;
|
var strLength = string.length;
|
||||||
length = +length;
|
length = toInteger(length);
|
||||||
|
|
||||||
if (strLength >= length || !nativeIsFinite(length)) {
|
if (!length || strLength >= length) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
var padLength = length - strLength;
|
var padLength = length - strLength;
|
||||||
chars = chars == null ? ' ' : (chars + '');
|
chars = chars === undefined ? ' ' : (chars + '');
|
||||||
return repeat(chars, nativeCeil(padLength / chars.length)).slice(0, padLength);
|
return repeat(chars, nativeCeil(padLength / chars.length)).slice(0, padLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10018,10 +10018,10 @@
|
|||||||
*/
|
*/
|
||||||
function pad(string, length, chars) {
|
function pad(string, length, chars) {
|
||||||
string = baseToString(string);
|
string = baseToString(string);
|
||||||
length = +length;
|
length = toInteger(length);
|
||||||
|
|
||||||
var strLength = string.length;
|
var strLength = string.length;
|
||||||
if (strLength >= length || !nativeIsFinite(length)) {
|
if (!length || strLength >= length) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
var mid = (length - strLength) / 2,
|
var mid = (length - strLength) / 2,
|
||||||
@@ -10466,7 +10466,7 @@
|
|||||||
if (!string) {
|
if (!string) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if (guard || chars == null) {
|
if (guard || chars === undefined) {
|
||||||
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
|
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
|
||||||
}
|
}
|
||||||
chars = (chars + '');
|
chars = (chars + '');
|
||||||
@@ -10497,7 +10497,7 @@
|
|||||||
if (!string) {
|
if (!string) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if (guard || chars == null) {
|
if (guard || chars === undefined) {
|
||||||
return string.slice(trimmedLeftIndex(string));
|
return string.slice(trimmedLeftIndex(string));
|
||||||
}
|
}
|
||||||
return string.slice(charsLeftIndex(string, (chars + '')));
|
return string.slice(charsLeftIndex(string, (chars + '')));
|
||||||
@@ -10527,7 +10527,7 @@
|
|||||||
if (!string) {
|
if (!string) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if (guard || chars == null) {
|
if (guard || chars === undefined) {
|
||||||
return string.slice(0, trimmedRightIndex(string) + 1);
|
return string.slice(0, trimmedRightIndex(string) + 1);
|
||||||
}
|
}
|
||||||
return string.slice(0, charsRightIndex(string, (chars + '')) + 1);
|
return string.slice(0, charsRightIndex(string, (chars + '')) + 1);
|
||||||
@@ -10587,7 +10587,7 @@
|
|||||||
return omission;
|
return omission;
|
||||||
}
|
}
|
||||||
var result = string.slice(0, end);
|
var result = string.slice(0, end);
|
||||||
if (separator == null) {
|
if (separator === undefined) {
|
||||||
return result + omission;
|
return result + omission;
|
||||||
}
|
}
|
||||||
if (isRegExp(separator)) {
|
if (isRegExp(separator)) {
|
||||||
|
|||||||
@@ -11381,7 +11381,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should treat nullish values as empty strings', 6, function() {
|
test('`_.' + methodName + '` should treat nullish values as empty strings', 6, function() {
|
||||||
_.each([null, '_-'], function(chars) {
|
_.each([undefined, '_-'], function(chars) {
|
||||||
var expected = chars ? (isPad ? '__' : chars) : ' ';
|
var expected = chars ? (isPad ? '__' : chars) : ' ';
|
||||||
strictEqual(func(null, 2, chars), expected);
|
strictEqual(func(null, 2, chars), expected);
|
||||||
strictEqual(func(undefined, 2, chars), expected);
|
strictEqual(func(undefined, 2, chars), expected);
|
||||||
@@ -15839,11 +15839,10 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should work with nullish or empty string values for `chars`', 3, function() {
|
test('`_.' + methodName + '` should work with `undefined` or empty string values for `chars`', 2, function() {
|
||||||
var string = whitespace + 'a b c' + whitespace,
|
var string = whitespace + 'a b c' + whitespace,
|
||||||
expected = (index == 2 ? whitespace : '') + 'a b c' + (index == 1 ? whitespace : '');
|
expected = (index == 2 ? whitespace : '') + 'a b c' + (index == 1 ? whitespace : '');
|
||||||
|
|
||||||
strictEqual(func(string, null), expected);
|
|
||||||
strictEqual(func(string, undefined), expected);
|
strictEqual(func(string, undefined), expected);
|
||||||
strictEqual(func(string, ''), string);
|
strictEqual(func(string, ''), string);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user