From 93b98abb8d5785580afdf4f98b68ac8fa8f6e3f0 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 17 Apr 2014 00:02:14 -0700 Subject: [PATCH] Remove native `trim`, `trimLeft`, & `trimRight` use. --- lodash.js | 113 +++++++++++++++--------------------------------------- 1 file changed, 31 insertions(+), 82 deletions(-) diff --git a/lodash.js b/lodash.js index 6e3afb83d..69d8a5e0b 100644 --- a/lodash.js +++ b/lodash.js @@ -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.