Remove native trim, trimLeft, & trimRight use.

This commit is contained in:
John-David Dalton
2014-04-17 00:02:14 -07:00
parent 5e28e38c98
commit 93b98abb8d

113
lodash.js
View File

@@ -463,69 +463,6 @@
return typeof value.toString != 'function' && typeof (value + '') == 'string';
}
/**
* A fallback implementation of `String#trim` to remove leading and trailing
* whitespace or specified characters from `string`.
*
* @private
* @param {string} string The string to trim.
* @param {string} [chars=whitespace] The characters to trim.
* @returns {string} Returns the trimmed string.
*/
function shimTrim(string, chars) {
string = string == null ? '' : String(string);
if (!string) {
return string;
}
if (chars == null) {
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
}
chars = String(chars);
return string.slice(charsLeftIndex(string, chars), charsRightIndex(string, chars) + 1);
}
/**
* A fallback implementation of `String#trimLeft` to remove leading whitespace
* or specified characters from `string`.
*
* @private
* @param {string} string The string to trim.
* @param {string} [chars=whitespace] The characters to trim.
* @returns {string} Returns the trimmed string.
*/
function shimTrimLeft(string, chars) {
string = string == null ? '' : String(string);
if (!string) {
return string;
}
if (chars == null) {
return string.slice(trimmedLeftIndex(string))
}
chars = String(chars);
return string.slice(charsLeftIndex(string, chars));
}
/**
* A fallback implementation of `String#trimRight` to remove trailing whitespace
* or specified characters from `string`.
*
* @private
* @param {string} string The string to trim.
* @param {string} [chars=whitespace] The characters to trim.
* @returns {string} Returns the trimmed string.
*/
function shimTrimRight(string, chars) {
string = string == null ? '' : String(string);
if (!string) {
return string;
}
if (chars == null) {
return string.slice(0, trimmedRightIndex(string) + 1)
}
chars = String(chars);
return string.slice(0, charsRightIndex(string, chars) + 1);
}
/**
* Gets the index of the first non-whitespace character of `string`.
*
@@ -672,10 +609,7 @@
nativeMin = Math.min,
nativeNow = isNative(nativeNow = Date.now) && nativeNow,
nativeParseInt = context.parseInt,
nativeRandom = Math.random,
nativeTrim = isNative(nativeTrim = stringProto.trim) && !nativeTrim.call(whitespace) && nativeTrim,
nativeTrimLeft = isNative(nativeTrimLeft = stringProto.trimLeft) && !nativeTrimLeft.call(whitespace) && nativeTrimLeft,
nativeTrimRight = isNative(nativeTrimRight = stringProto.trimRight) && !nativeTrimRight.call(whitespace) && nativeTrimRight;
nativeRandom = Math.random;
/** Used to lookup built-in constructors by `[[Class]]` */
var ctorByClass = {};
@@ -7583,12 +7517,17 @@
* _.trim('-_-fred-_-', '_-');
* // => 'fred'
*/
var trim = !nativeTrim ? shimTrim : function(string, chars) {
if (string == null) {
return '';
function trim(string, chars) {
string = string == null ? '' : String(string);
if (!string) {
return string;
}
return chars == null ? nativeTrim.call(string) : shimTrim(string, chars);
};
if (chars == null) {
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
}
chars = String(chars);
return string.slice(charsLeftIndex(string, chars), charsRightIndex(string, chars) + 1);
}
/**
* Removes leading whitespace or specified characters from `string`.
@@ -7607,12 +7546,17 @@
* _.trimLeft('-_-fred-_-', '_-');
* // => 'fred-_-'
*/
var trimLeft = !nativeTrimLeft ? shimTrimLeft : function(string, chars) {
if (string == null) {
return '';
function trimLeft(string, chars) {
string = string == null ? '' : String(string);
if (!string) {
return string;
}
return chars == null ? nativeTrimLeft.call(string) : shimTrimLeft(string, chars);
};
if (chars == null) {
return string.slice(trimmedLeftIndex(string))
}
chars = String(chars);
return string.slice(charsLeftIndex(string, chars));
}
/**
* Removes trailing whitespace or specified characters from `string`.
@@ -7631,12 +7575,17 @@
* _.trimRight('-_-fred-_-', '_-');
* // => '-_-fred'
*/
var trimRight = !nativeTrimRight ? shimTrimRight : function(string, chars) {
if (string == null) {
return '';
function trimRight(string, chars) {
string = string == null ? '' : String(string);
if (!string) {
return string;
}
return chars == null ? nativeTrimRight.call(string) : shimTrimRight(string, chars);
};
if (chars == null) {
return string.slice(0, trimmedRightIndex(string) + 1)
}
chars = String(chars);
return string.slice(0, charsRightIndex(string, chars) + 1);
}
/**
* Truncates `string` if it is longer than the given maximum string length.