Rename padLeft, padRight, trimLeft, and trimRight to padStart, padEnd, trimStart, and trimEnd.

This commit is contained in:
John-David Dalton
2015-11-17 20:43:45 -08:00
parent dcca9b82b1
commit 44397f79a6
2 changed files with 119 additions and 119 deletions

182
lodash.js
View File

@@ -904,7 +904,7 @@
} }
/** /**
* Used by `_.trim` and `_.trimLeft` to get the index of the first string symbol * Used by `_.trim` and `_.trimStart` to get the index of the first string symbol
* that is not found in the character symbols. * that is not found in the character symbols.
* *
* @private * @private
@@ -912,7 +912,7 @@
* @param {Array} chrSymbols The character symbols to find. * @param {Array} chrSymbols The character symbols to find.
* @returns {number} Returns the index of the first unmatched string symbol. * @returns {number} Returns the index of the first unmatched string symbol.
*/ */
function charsLeftIndex(strSymbols, chrSymbols) { function charsStartIndex(strSymbols, chrSymbols) {
var index = -1, var index = -1,
length = strSymbols.length; length = strSymbols.length;
@@ -921,7 +921,7 @@
} }
/** /**
* Used by `_.trim` and `_.trimRight` to get the index of the last string symbol * Used by `_.trim` and `_.trimEnd` to get the index of the last string symbol
* that is not found in the character symbols. * that is not found in the character symbols.
* *
* @private * @private
@@ -929,7 +929,7 @@
* @param {Array} chrSymbols The character symbols to find. * @param {Array} chrSymbols The character symbols to find.
* @returns {number} Returns the index of the last unmatched string symbol. * @returns {number} Returns the index of the last unmatched string symbol.
*/ */
function charsRightIndex(strSymbols, chrSymbols) { function charsEndIndex(strSymbols, chrSymbols) {
var index = strSymbols.length; var index = strSymbols.length;
while (index-- && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {} while (index-- && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {}
@@ -1167,7 +1167,7 @@
} }
/** /**
* Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a * Used by `trimmedStartIndex` and `trimmedEndIndex` to determine if a
* character code is whitespace. * character code is whitespace.
* *
* @private * @private
@@ -1283,14 +1283,14 @@
} }
/** /**
* Used by `_.trim` and `_.trimLeft` to get the index of the first non-whitespace * Used by `_.trim` and `_.trimStart` to get the index of the first non-whitespace
* character of `string`. * character of `string`.
* *
* @private * @private
* @param {string} string The string to inspect. * @param {string} string The string to inspect.
* @returns {number} Returns the index of the first non-whitespace character. * @returns {number} Returns the index of the first non-whitespace character.
*/ */
function trimmedLeftIndex(string) { function trimmedStartIndex(string) {
var index = -1, var index = -1,
length = string.length; length = string.length;
@@ -1299,14 +1299,14 @@
} }
/** /**
* Used by `_.trim` and `_.trimRight` to get the index of the last non-whitespace * Used by `_.trim` and `_.trimEnd` to get the index of the last non-whitespace
* character of `string`. * character of `string`.
* *
* @private * @private
* @param {string} string The string to inspect. * @param {string} string The string to inspect.
* @returns {number} Returns the index of the last non-whitespace character. * @returns {number} Returns the index of the last non-whitespace character.
*/ */
function trimmedRightIndex(string) { function trimmedEndIndex(string) {
var index = string.length; var index = string.length;
while (index-- && isSpace(string.charCodeAt(index))) {} while (index-- && isSpace(string.charCodeAt(index))) {}
@@ -1489,9 +1489,9 @@
* The chainable wrapper methods are: * The chainable wrapper methods are:
* `after`, `ary`, `assign`, `assignIn`, `assignInWith`, `assignWith`, * `after`, `ary`, `assign`, `assignIn`, `assignInWith`, `assignWith`,
* `at`, `before`, `bind`, `bindAll`, `bindKey`, `chain`, `chunk`, `commit`, * `at`, `before`, `bind`, `bindAll`, `bindKey`, `chain`, `chunk`, `commit`,
* `compact`, `concat`, `conforms`, `conj`, `constant`, `countBy`, `create`, * `compact`, `concat`, `conforms`, `overEvery`, `constant`, `countBy`, `create`,
* `curry`, `debounce`, `defaults`, `defaultsDeep`, `defer`, `delay`, * `curry`, `debounce`, `defaults`, `defaultsDeep`, `defer`, `delay`,
* `difference`, `differenceBy`, `differenceWith`, `disj`, `drop`, `dropRight`, * `difference`, `differenceBy`, `differenceWith`, `overSome`, `drop`, `dropRight`,
* `dropRightWhile`, `dropWhile`, `fill`, `filter`, `flatten`, `flattenDeep`, * `dropRightWhile`, `dropWhile`, `fill`, `filter`, `flatten`, `flattenDeep`,
* `flip`, `flow`, `flowRight`, `forEach`, `forEachRight`, `forIn`, `forInRight`, * `flip`, `flow`, `flowRight`, `forEach`, `forEachRight`, `forIn`, `forInRight`,
* `forOwn`, `forOwnRight`, `functions`, `functionsIn`, `groupBy`, `initial`, * `forOwn`, `forOwnRight`, `functions`, `functionsIn`, `groupBy`, `initial`,
@@ -1523,13 +1523,13 @@
* `isPlainObject`, `isRegExp`, `isSafeInteger`, `isString`, `isUndefined`, * `isPlainObject`, `isRegExp`, `isSafeInteger`, `isString`, `isUndefined`,
* `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lowerCase`, * `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lowerCase`,
* `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `min`, `minBy`, * `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `min`, `minBy`,
* `noConflict`, `noop`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`, * `noConflict`, `noop`, `now`, `pad`, `padEnd`, `padStart`, `parseInt`,
* `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`, * `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`,
* `runInContext`, `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, * `runInContext`, `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`,
* `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`, `startCase`, * `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`, `startCase`,
* `startsWith`, `subtract`, `sum`, sumBy`, `template`, `times`, `toLower`, * `startsWith`, `subtract`, `sum`, sumBy`, `template`, `times`, `toLower`,
* `toInteger`, `toLength`, `toNumber`, `toSafeInteger`, toString`, `toUpper`, * `toInteger`, `toLength`, `toNumber`, `toSafeInteger`, toString`, `toUpper`,
* `trim`, `trimLeft`, `trimRight`, `truncate`, `unescape`, `uniqueId`, * `trim`, `trimEnd`, `trimStart`, `truncate`, `unescape`, `uniqueId`,
* `upperCase`, `upperFirst`, `value`, and `words` * `upperCase`, `upperFirst`, `value`, and `words`
* *
* @name _ * @name _
@@ -4092,7 +4092,7 @@
} }
/** /**
* Creates a function like `_.conj`. * Creates a function like `_.overEvery`.
* *
* @private * @private
* @param {Function} arrayFunc The function to iterate over iteratees. * @param {Function} arrayFunc The function to iterate over iteratees.
@@ -7484,7 +7484,7 @@
* The guarded methods are: * The guarded methods are:
* `ary`, `callback`, `curry`, `curryRight`, `drop`, `dropRight`, `every`, * `ary`, `callback`, `curry`, `curryRight`, `drop`, `dropRight`, `every`,
* `fill`, `invert`, `parseInt`, `random`, `range`, `slice`, `some`, `sortBy`, * `fill`, `invert`, `parseInt`, `random`, `range`, `slice`, `some`, `sortBy`,
* `take`, `takeRight`, `template`, `trim`, `trimLeft`, `trimRight`, `uniq`, * `take`, `takeRight`, `template`, `trim`, `trimEnd`, `trimStart`, `uniq`,
* and `words` * and `words`
* *
* @static * @static
@@ -11863,33 +11863,6 @@
return createPadding('', leftLength, chars) + string + createPadding('', rightLength, chars); return createPadding('', leftLength, chars) + string + createPadding('', rightLength, chars);
} }
/**
* Pads `string` on the left side if it's shorter than `length`. Padding
* characters are truncated if they exceed `length`.
*
* @static
* @memberOf _
* @category String
* @param {string} [string=''] The string to pad.
* @param {number} [length=0] The padding length.
* @param {string} [chars=' '] The string used as padding.
* @returns {string} Returns the padded string.
* @example
*
* _.padLeft('abc', 6);
* // => ' abc'
*
* _.padLeft('abc', 6, '_-');
* // => '_-_abc'
*
* _.padLeft('abc', 3);
* // => 'abc'
*/
function padLeft(string, length, chars) {
string = toString(string);
return createPadding(string, length, chars) + string;
}
/** /**
* Pads `string` on the right side if it's shorter than `length`. Padding * Pads `string` on the right side if it's shorter than `length`. Padding
* characters are truncated if they exceed `length`. * characters are truncated if they exceed `length`.
@@ -11903,20 +11876,47 @@
* @returns {string} Returns the padded string. * @returns {string} Returns the padded string.
* @example * @example
* *
* _.padRight('abc', 6); * _.padEnd('abc', 6);
* // => 'abc ' * // => 'abc '
* *
* _.padRight('abc', 6, '_-'); * _.padEnd('abc', 6, '_-');
* // => 'abc_-_' * // => 'abc_-_'
* *
* _.padRight('abc', 3); * _.padEnd('abc', 3);
* // => 'abc' * // => 'abc'
*/ */
function padRight(string, length, chars) { function padEnd(string, length, chars) {
string = toString(string); string = toString(string);
return string + createPadding(string, length, chars); return string + createPadding(string, length, chars);
} }
/**
* Pads `string` on the left side if it's shorter than `length`. Padding
* characters are truncated if they exceed `length`.
*
* @static
* @memberOf _
* @category String
* @param {string} [string=''] The string to pad.
* @param {number} [length=0] The padding length.
* @param {string} [chars=' '] The string used as padding.
* @returns {string} Returns the padded string.
* @example
*
* _.padStart('abc', 6);
* // => ' abc'
*
* _.padStart('abc', 6, '_-');
* // => '_-_abc'
*
* _.padStart('abc', 3);
* // => 'abc'
*/
function padStart(string, length, chars) {
string = toString(string);
return createPadding(string, length, chars) + string;
}
/** /**
* Converts `string` to an integer of the specified radix. If `radix` is * Converts `string` to an integer of the specified radix. If `radix` is
* `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal, * `undefined` or `0`, a `radix` of `10` is used unless `value` is a hexadecimal,
@@ -12341,7 +12341,7 @@
return string; return string;
} }
if (guard || chars === undefined) { if (guard || chars === undefined) {
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1); return string.slice(trimmedStartIndex(string), trimmedEndIndex(string) + 1);
} }
chars = (chars + ''); chars = (chars + '');
if (!chars) { if (!chars) {
@@ -12350,41 +12350,7 @@
var strSymbols = stringToArray(string), var strSymbols = stringToArray(string),
chrSymbols = stringToArray(chars); chrSymbols = stringToArray(chars);
return strSymbols.slice(charsLeftIndex(strSymbols, chrSymbols), charsRightIndex(strSymbols, chrSymbols) + 1).join(''); return strSymbols.slice(charsStartIndex(strSymbols, chrSymbols), charsEndIndex(strSymbols, chrSymbols) + 1).join('');
}
/**
* Removes leading whitespace or specified characters from `string`.
*
* @static
* @memberOf _
* @category String
* @param {string} [string=''] The string to trim.
* @param {string} [chars=whitespace] The characters to trim.
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
* @returns {string} Returns the trimmed string.
* @example
*
* _.trimLeft(' abc ');
* // => 'abc '
*
* _.trimLeft('-_-abc-_-', '_-');
* // => 'abc-_-'
*/
function trimLeft(string, chars, guard) {
string = toString(string);
if (!string) {
return string;
}
if (guard || chars === undefined) {
return string.slice(trimmedLeftIndex(string));
}
chars = (chars + '');
if (!chars) {
return string;
}
var strSymbols = stringToArray(string);
return strSymbols.slice(charsLeftIndex(strSymbols, stringToArray(chars))).join('');
} }
/** /**
@@ -12399,26 +12365,60 @@
* @returns {string} Returns the trimmed string. * @returns {string} Returns the trimmed string.
* @example * @example
* *
* _.trimRight(' abc '); * _.trimEnd(' abc ');
* // => ' abc' * // => ' abc'
* *
* _.trimRight('-_-abc-_-', '_-'); * _.trimEnd('-_-abc-_-', '_-');
* // => '-_-abc' * // => '-_-abc'
*/ */
function trimRight(string, chars, guard) { function trimEnd(string, chars, guard) {
string = toString(string); string = toString(string);
if (!string) { if (!string) {
return string; return string;
} }
if (guard || chars === undefined) { if (guard || chars === undefined) {
return string.slice(0, trimmedRightIndex(string) + 1); return string.slice(0, trimmedEndIndex(string) + 1);
} }
chars = (chars + ''); chars = (chars + '');
if (!chars) { if (!chars) {
return string; return string;
} }
var strSymbols = stringToArray(string); var strSymbols = stringToArray(string);
return strSymbols.slice(0, charsRightIndex(strSymbols, stringToArray(chars)) + 1).join(''); return strSymbols.slice(0, charsEndIndex(strSymbols, stringToArray(chars)) + 1).join('');
}
/**
* Removes leading whitespace or specified characters from `string`.
*
* @static
* @memberOf _
* @category String
* @param {string} [string=''] The string to trim.
* @param {string} [chars=whitespace] The characters to trim.
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
* @returns {string} Returns the trimmed string.
* @example
*
* _.trimStart(' abc ');
* // => 'abc '
*
* _.trimStart('-_-abc-_-', '_-');
* // => 'abc-_-'
*/
function trimStart(string, chars, guard) {
string = toString(string);
if (!string) {
return string;
}
if (guard || chars === undefined) {
return string.slice(trimmedStartIndex(string));
}
chars = (chars + '');
if (!chars) {
return string;
}
var strSymbols = stringToArray(string);
return strSymbols.slice(charsStartIndex(strSymbols, stringToArray(chars))).join('');
} }
/** /**
@@ -13829,8 +13829,8 @@
lodash.noop = noop; lodash.noop = noop;
lodash.now = now; lodash.now = now;
lodash.pad = pad; lodash.pad = pad;
lodash.padLeft = padLeft; lodash.padEnd = padEnd;
lodash.padRight = padRight; lodash.padStart = padStart;
lodash.parseInt = parseInt; lodash.parseInt = parseInt;
lodash.random = random; lodash.random = random;
lodash.reduce = reduce; lodash.reduce = reduce;
@@ -13864,8 +13864,8 @@
lodash.toString = toString; lodash.toString = toString;
lodash.toUpper = toUpper; lodash.toUpper = toUpper;
lodash.trim = trim; lodash.trim = trim;
lodash.trimLeft = trimLeft; lodash.trimEnd = trimEnd;
lodash.trimRight = trimRight; lodash.trimStart = trimStart;
lodash.truncate = truncate; lodash.truncate = truncate;
lodash.unescape = unescape; lodash.unescape = unescape;
lodash.uniqueId = uniqueId; lodash.uniqueId = uniqueId;

View File

@@ -14022,51 +14022,51 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('lodash.padLeft'); QUnit.module('lodash.padEnd');
(function() { (function() {
QUnit.test('should pad a string to a given length', function(assert) { QUnit.test('should pad a string to a given length', function(assert) {
assert.expect(1); assert.expect(1);
assert.strictEqual(_.padLeft('abc', 6), ' abc'); assert.strictEqual(_.padEnd('abc', 6), 'abc ');
}); });
QUnit.test('should truncate pad characters to fit the pad length', function(assert) { QUnit.test('should truncate pad characters to fit the pad length', function(assert) {
assert.expect(1); assert.expect(1);
assert.strictEqual(_.padLeft('abc', 6, '_-'), '_-_abc'); assert.strictEqual(_.padEnd('abc', 6, '_-'), 'abc_-_');
}); });
QUnit.test('should coerce `string` to a string', function(assert) { QUnit.test('should coerce `string` to a string', function(assert) {
assert.expect(2); assert.expect(2);
assert.strictEqual(_.padLeft(Object('abc'), 4), ' abc'); assert.strictEqual(_.padEnd(Object('abc'), 4), 'abc ');
assert.strictEqual(_.padLeft({ 'toString': lodashStable.constant('abc') }, 5), ' abc'); assert.strictEqual(_.padEnd({ 'toString': lodashStable.constant('abc') }, 5), 'abc ');
}); });
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('lodash.padRight'); QUnit.module('lodash.padStart');
(function() { (function() {
QUnit.test('should pad a string to a given length', function(assert) { QUnit.test('should pad a string to a given length', function(assert) {
assert.expect(1); assert.expect(1);
assert.strictEqual(_.padRight('abc', 6), 'abc '); assert.strictEqual(_.padStart('abc', 6), ' abc');
}); });
QUnit.test('should truncate pad characters to fit the pad length', function(assert) { QUnit.test('should truncate pad characters to fit the pad length', function(assert) {
assert.expect(1); assert.expect(1);
assert.strictEqual(_.padRight('abc', 6, '_-'), 'abc_-_'); assert.strictEqual(_.padStart('abc', 6, '_-'), '_-_abc');
}); });
QUnit.test('should coerce `string` to a string', function(assert) { QUnit.test('should coerce `string` to a string', function(assert) {
assert.expect(2); assert.expect(2);
assert.strictEqual(_.padRight(Object('abc'), 4), 'abc '); assert.strictEqual(_.padStart(Object('abc'), 4), ' abc');
assert.strictEqual(_.padRight({ 'toString': lodashStable.constant('abc') }, 5), 'abc '); assert.strictEqual(_.padStart({ 'toString': lodashStable.constant('abc') }, 5), ' abc');
}); });
}()); }());
@@ -14074,10 +14074,10 @@
QUnit.module('pad methods'); QUnit.module('pad methods');
lodashStable.each(['pad', 'padLeft', 'padRight'], function(methodName) { lodashStable.each(['pad', 'padStart', 'padEnd'], function(methodName) {
var func = _[methodName], var func = _[methodName],
isPad = methodName == 'pad', isPad = methodName == 'pad',
isPadLeft = methodName == 'padLeft'; isStart = methodName == 'padStart';
QUnit.test('`_.' + methodName + '` should not pad is string is >= `length`', function(assert) { QUnit.test('`_.' + methodName + '` should not pad is string is >= `length`', function(assert) {
assert.expect(2); assert.expect(2);
@@ -14098,7 +14098,7 @@
assert.expect(2); assert.expect(2);
lodashStable.each(['', '4'], function(length) { lodashStable.each(['', '4'], function(length) {
var actual = length ? (isPadLeft ? ' abc' : 'abc ') : 'abc'; var actual = length ? (isStart ? ' abc' : 'abc ') : 'abc';
assert.strictEqual(func('abc', length), actual); assert.strictEqual(func('abc', length), actual);
}); });
}); });
@@ -20175,7 +20175,7 @@
QUnit.module('trim methods'); QUnit.module('trim methods');
lodashStable.each(['trim', 'trimLeft', 'trimRight'], function(methodName, index) { lodashStable.each(['trim', 'trimStart', 'trimEnd'], function(methodName, index) {
var func = _[methodName]; var func = _[methodName];
var parts = []; var parts = [];
@@ -20319,19 +20319,19 @@
assert.strictEqual(_.capitalize(rocket), rocket); assert.strictEqual(_.capitalize(rocket), rocket);
assert.strictEqual(_.pad(string, 16), ' ' + string + ' '); assert.strictEqual(_.pad(string, 16), ' ' + string + ' ');
assert.strictEqual(_.padLeft(string, 16), ' ' + string); assert.strictEqual(_.padStart(string, 16), ' ' + string);
assert.strictEqual(_.padRight(string, 16), string + ' '); assert.strictEqual(_.padEnd(string, 16), string + ' ');
assert.strictEqual(_.pad(string, 16, chars), hearts + string + chars); assert.strictEqual(_.pad(string, 16, chars), hearts + string + chars);
assert.strictEqual(_.padLeft(string, 16, chars), chars + hearts + string); assert.strictEqual(_.padStart(string, 16, chars), chars + hearts + string);
assert.strictEqual(_.padRight(string, 16, chars), string + chars + hearts); assert.strictEqual(_.padEnd(string, 16, chars), string + chars + hearts);
assert.strictEqual(_.size(string), 13); assert.strictEqual(_.size(string), 13);
assert.deepEqual(_.toArray(string), ['A', ' ', leafs, ',', ' ', comboGlyph, ',', ' ', 'a', 'n', 'd', ' ', rocket]); assert.deepEqual(_.toArray(string), ['A', ' ', leafs, ',', ' ', comboGlyph, ',', ' ', 'a', 'n', 'd', ' ', rocket]);
assert.strictEqual(_.trim(trimString, chars), string); assert.strictEqual(_.trim(trimString, chars), string);
assert.strictEqual(_.trimLeft(trimString, chars), string + trimChars); assert.strictEqual(_.trimStart(trimString, chars), string + trimChars);
assert.strictEqual(_.trimRight(trimString, chars), trimChars + string); assert.strictEqual(_.trimEnd(trimString, chars), trimChars + string);
assert.strictEqual(_.truncate(string, { 'length': 13 }), string); assert.strictEqual(_.truncate(string, { 'length': 13 }), string);
assert.strictEqual(_.truncate(string, { 'length': 6 }), 'A ' + leafs + '...'); assert.strictEqual(_.truncate(string, { 'length': 6 }), 'A ' + leafs + '...');
@@ -22256,8 +22256,8 @@
'min', 'min',
'minBy', 'minBy',
'pad', 'pad',
'padLeft', 'padEnd',
'padRight', 'padStart',
'parseInt', 'parseInt',
'pop', 'pop',
'random', 'random',
@@ -22280,8 +22280,8 @@
'toString', 'toString',
'toUpper', 'toUpper',
'trim', 'trim',
'trimLeft', 'trimEnd',
'trimRight', 'trimStart',
'truncate', 'truncate',
'unescape', 'unescape',
'upperCase', 'upperCase',
@@ -22408,15 +22408,15 @@
'lowerCase', 'lowerCase',
'lowerFirst', 'lowerFirst',
'pad', 'pad',
'padLeft', 'padEnd',
'padRight', 'padStart',
'repeat', 'repeat',
'snakeCase', 'snakeCase',
'toLower', 'toLower',
'toUpper', 'toUpper',
'trim', 'trim',
'trimLeft', 'trimEnd',
'trimRight', 'trimStart',
'truncate', 'truncate',
'unescape', 'unescape',
'upperCase', 'upperCase',