From f3a8e55e70ecf7bfa88361d48b48f5172957555e Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 3 Feb 2017 22:49:18 -0800 Subject: [PATCH] Use built-in String#trim methods when possible. --- trim.js | 5 ++--- trimEnd.js | 7 ++++--- trimStart.js | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/trim.js b/trim.js index 7ddd21181..a1a64f0c1 100644 --- a/trim.js +++ b/trim.js @@ -5,8 +5,7 @@ import charsStartIndex from './.internal/charsStartIndex.js'; import stringToArray from './.internal/stringToArray.js'; import toString from './toString.js'; -/** Used to match leading and trailing whitespace. */ -const reTrim = /^\s+|\s+$/g; +const nativeTrim = String.prototype.trim; /** * Removes leading and trailing whitespace or specified characters from `string`. @@ -32,7 +31,7 @@ const reTrim = /^\s+|\s+$/g; function trim(string, chars, guard) { string = toString(string); if (string && (guard || chars === undefined)) { - return string.replace(reTrim, ''); + return nativeTrim.call(string); } if (!string || !(chars = baseToString(chars))) { return string; diff --git a/trimEnd.js b/trimEnd.js index f12f6351d..7020414e5 100644 --- a/trimEnd.js +++ b/trimEnd.js @@ -4,8 +4,9 @@ import charsEndIndex from './.internal/charsEndIndex.js'; import stringToArray from './.internal/stringToArray.js'; import toString from './toString.js'; -/** Used to match leading and trailing whitespace. */ -const reTrimEnd = /\s+$/; +const stringProto = String.prototype; +const nativeTrimEnd = stringProto.trimRight || stringProto.trimEnd; + /** * Removes trailing whitespace or specified characters from `string`. @@ -28,7 +29,7 @@ const reTrimEnd = /\s+$/; function trimEnd(string, chars, guard) { string = toString(string); if (string && (guard || chars === undefined)) { - return string.replace(reTrimEnd, ''); + return nativeTrimEnd.call(string); } if (!string || !(chars = baseToString(chars))) { return string; diff --git a/trimStart.js b/trimStart.js index 9b384958a..96947ccc8 100644 --- a/trimStart.js +++ b/trimStart.js @@ -4,8 +4,8 @@ import charsStartIndex from './.internal/charsStartIndex.js'; import stringToArray from './.internal/stringToArray.js'; import toString from './toString.js'; -/** Used to match leading and trailing whitespace. */ -const reTrimStart = /^\s+/; +const stringProto = String.prototype; +const nativeTrimStart = stringProto.trimLeft || stringProto.trimStart; /** * Removes leading whitespace or specified characters from `string`. @@ -28,7 +28,7 @@ const reTrimStart = /^\s+/; function trimStart(string, chars, guard) { string = toString(string); if (string && (guard || chars === undefined)) { - return string.replace(reTrimStart, ''); + return nativeTrimStart.call(string); } if (!string || !(chars = baseToString(chars))) { return string;