mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Simplify trim methods.
This commit is contained in:
73
lodash.js
73
lodash.js
@@ -119,6 +119,11 @@
|
|||||||
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
|
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
|
||||||
reHasRegExpChar = RegExp(reRegExpChar.source);
|
reHasRegExpChar = RegExp(reRegExpChar.source);
|
||||||
|
|
||||||
|
/** Used to match leading and trailing whitespace. */
|
||||||
|
var reTrim = /^\s+|\s+$/g,
|
||||||
|
reTrimStart = /^\s+/,
|
||||||
|
reTrimEnd = /\s+$/;
|
||||||
|
|
||||||
/** Used to match backslashes in property paths. */
|
/** Used to match backslashes in property paths. */
|
||||||
var reEscapeChar = /\\(\\)?/g;
|
var reEscapeChar = /\\(\\)?/g;
|
||||||
|
|
||||||
@@ -842,20 +847,6 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The base implementation of `_.trim` without support trimming non-whitespace
|
|
||||||
* characters.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {string} string The string to trim.
|
|
||||||
* @returns {string} Returns the trimmed string.
|
|
||||||
*/
|
|
||||||
function baseTrim(string) {
|
|
||||||
return string
|
|
||||||
? string.slice(trimmedStartIndex(string), trimmedEndIndex(string) + 1)
|
|
||||||
: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.unary` without support for storing wrapper metadata.
|
* The base implementation of `_.unary` without support for storing wrapper metadata.
|
||||||
*
|
*
|
||||||
@@ -1090,19 +1081,6 @@
|
|||||||
return value > -1 && value % 1 == 0 && value < length;
|
return value > -1 && value % 1 == 0 && value < length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Used by `trimmedStartIndex` and `trimmedEndIndex` to determine if a
|
|
||||||
* character code is whitespace.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {number} charCode The character code to inspect.
|
|
||||||
* @returns {boolean} Returns `true` if `charCode` is whitespace, else `false`.
|
|
||||||
*/
|
|
||||||
function isSpace(charCode) {
|
|
||||||
return ((charCode <= 160 && (charCode >= 9 && charCode <= 13) || charCode == 32 || charCode == 160) || charCode == 5760 || charCode == 6158 ||
|
|
||||||
(charCode >= 8192 && (charCode <= 8202 || charCode == 8232 || charCode == 8233 || charCode == 8239 || charCode == 8287 || charCode == 12288 || charCode == 65279)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts `iterator` to an array.
|
* Converts `iterator` to an array.
|
||||||
*
|
*
|
||||||
@@ -1206,37 +1184,6 @@
|
|||||||
return string.match(reComplexSymbol);
|
return string.match(reComplexSymbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Used by `_.trim` and `_.trimStart` to get the index of the first non-whitespace
|
|
||||||
* character of `string`.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {string} string The string to inspect.
|
|
||||||
* @returns {number} Returns the index of the first non-whitespace character.
|
|
||||||
*/
|
|
||||||
function trimmedStartIndex(string) {
|
|
||||||
var index = -1,
|
|
||||||
length = string.length;
|
|
||||||
|
|
||||||
while (++index < length && isSpace(string.charCodeAt(index))) {}
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used by `_.trim` and `_.trimEnd` to get the index of the last non-whitespace
|
|
||||||
* character of `string`.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {string} string The string to inspect.
|
|
||||||
* @returns {number} Returns the index of the last non-whitespace character.
|
|
||||||
*/
|
|
||||||
function trimmedEndIndex(string) {
|
|
||||||
var index = string.length;
|
|
||||||
|
|
||||||
while (index-- && isSpace(string.charCodeAt(index))) {}
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used by `_.unescape` to convert HTML entities to characters.
|
* Used by `_.unescape` to convert HTML entities to characters.
|
||||||
*
|
*
|
||||||
@@ -10293,7 +10240,7 @@
|
|||||||
if (typeof value == 'number' || !isString(value)) {
|
if (typeof value == 'number' || !isString(value)) {
|
||||||
return +value;
|
return +value;
|
||||||
}
|
}
|
||||||
value = baseTrim(value);
|
value = value.replace(reTrim, '');
|
||||||
var isBinary = reIsBinary.test(value);
|
var isBinary = reIsBinary.test(value);
|
||||||
return (isBinary || reIsOctal.test(value))
|
return (isBinary || reIsOctal.test(value))
|
||||||
? nativeParseInt(value.slice(2), isBinary ? 2 : 8)
|
? nativeParseInt(value.slice(2), isBinary ? 2 : 8)
|
||||||
@@ -12127,7 +12074,7 @@
|
|||||||
} else if (radix) {
|
} else if (radix) {
|
||||||
radix = +radix;
|
radix = +radix;
|
||||||
}
|
}
|
||||||
string = baseTrim(toString(string));
|
string = toString(string).replace(reTrim, '');
|
||||||
return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10));
|
return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -12565,7 +12512,7 @@
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if (guard || chars === undefined) {
|
if (guard || chars === undefined) {
|
||||||
return baseTrim(string);
|
return string.replace(reTrim, '');
|
||||||
}
|
}
|
||||||
chars = (chars + '');
|
chars = (chars + '');
|
||||||
if (!chars) {
|
if (!chars) {
|
||||||
@@ -12601,7 +12548,7 @@
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if (guard || chars === undefined) {
|
if (guard || chars === undefined) {
|
||||||
return string.slice(0, trimmedEndIndex(string) + 1);
|
return string.replace(reTrimEnd, '');
|
||||||
}
|
}
|
||||||
chars = (chars + '');
|
chars = (chars + '');
|
||||||
if (!chars) {
|
if (!chars) {
|
||||||
@@ -12635,7 +12582,7 @@
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if (guard || chars === undefined) {
|
if (guard || chars === undefined) {
|
||||||
return string.slice(trimmedStartIndex(string));
|
return string.replace(reTrimStart, '');
|
||||||
}
|
}
|
||||||
chars = (chars + '');
|
chars = (chars + '');
|
||||||
if (!chars) {
|
if (!chars) {
|
||||||
|
|||||||
24
test/test.js
24
test/test.js
@@ -316,7 +316,19 @@
|
|||||||
* Used to check for problems removing whitespace. For a whitespace reference,
|
* Used to check for problems removing whitespace. For a whitespace reference,
|
||||||
* see [V8's unit test](https://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/whitespaces.js).
|
* see [V8's unit test](https://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/whitespaces.js).
|
||||||
*/
|
*/
|
||||||
var whitespace = ' \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000';
|
var whitespace = lodashStable.filter([
|
||||||
|
// Basic whitespace characters.
|
||||||
|
' ', '\t', '\x0b', '\f', '\xa0', '\ufeff',
|
||||||
|
|
||||||
|
// Line terminators.
|
||||||
|
'\n', '\r', '\u2028', '\u2029',
|
||||||
|
|
||||||
|
// Unicode category "Zs" space separators.
|
||||||
|
'\u1680', '\u180e', '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005',
|
||||||
|
'\u2006', '\u2007', '\u2008', '\u2009', '\u200a', '\u202f', '\u205f', '\u3000'
|
||||||
|
],
|
||||||
|
function(chr) { return /\s/.exec(chr); })
|
||||||
|
.join('');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the unwrapped value from its wrapper.
|
* Extracts the unwrapped value from its wrapper.
|
||||||
@@ -21024,16 +21036,6 @@
|
|||||||
assert.strictEqual(func(string), expected);
|
assert.strictEqual(func(string), expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('`_.' + methodName + '` should not remove non-whitespace characters', function(assert) {
|
|
||||||
assert.expect(1);
|
|
||||||
|
|
||||||
// Zero-width space (zws), next line character (nel), and non-character (bom) are not whitespace.
|
|
||||||
var problemChars = '\x85\u200b\ufffe',
|
|
||||||
string = problemChars + 'a b c' + problemChars;
|
|
||||||
|
|
||||||
assert.strictEqual(func(string), string);
|
|
||||||
});
|
|
||||||
|
|
||||||
QUnit.test('`_.' + methodName + '` should coerce `string` to a string', function(assert) {
|
QUnit.test('`_.' + methodName + '` should coerce `string` to a string', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user