Skip to content

Commit

Permalink
Use built-in String#trim methods when possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Feb 4, 2017
1 parent 9d445ec commit f3a8e55
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
5 changes: 2 additions & 3 deletions trim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions trimEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions trimStart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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;
Expand Down

0 comments on commit f3a8e55

Please sign in to comment.