mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Simplify method juggle in trim modules.
This commit is contained in:
4
trim.js
4
trim.js
@@ -5,8 +5,6 @@ import charsStartIndex from './.internal/charsStartIndex.js'
|
|||||||
import stringToArray from './.internal/stringToArray.js'
|
import stringToArray from './.internal/stringToArray.js'
|
||||||
import toString from './toString.js'
|
import toString from './toString.js'
|
||||||
|
|
||||||
const nativeTrim = String.prototype.trim
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes leading and trailing whitespace or specified characters from `string`.
|
* Removes leading and trailing whitespace or specified characters from `string`.
|
||||||
*
|
*
|
||||||
@@ -31,7 +29,7 @@ const nativeTrim = String.prototype.trim
|
|||||||
function trim(string, chars, guard) {
|
function trim(string, chars, guard) {
|
||||||
string = toString(string)
|
string = toString(string)
|
||||||
if (string && (guard || chars === undefined)) {
|
if (string && (guard || chars === undefined)) {
|
||||||
return nativeTrim.call(string)
|
return string.trim()
|
||||||
}
|
}
|
||||||
if (!string || !(chars = baseToString(chars))) {
|
if (!string || !(chars = baseToString(chars))) {
|
||||||
return string
|
return string
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import charsEndIndex from './.internal/charsEndIndex.js'
|
|||||||
import stringToArray from './.internal/stringToArray.js'
|
import stringToArray from './.internal/stringToArray.js'
|
||||||
import toString from './toString.js'
|
import toString from './toString.js'
|
||||||
|
|
||||||
const stringProto = String.prototype
|
const methodName = ''.trimRight ? 'trimRight': 'trimEnd'
|
||||||
const nativeTrimEnd = stringProto.trimRight || stringProto.trimEnd
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes trailing whitespace or specified characters from `string`.
|
* Removes trailing whitespace or specified characters from `string`.
|
||||||
@@ -28,7 +27,7 @@ const nativeTrimEnd = stringProto.trimRight || stringProto.trimEnd
|
|||||||
function trimEnd(string, chars, guard) {
|
function trimEnd(string, chars, guard) {
|
||||||
string = toString(string)
|
string = toString(string)
|
||||||
if (string && (guard || chars === undefined)) {
|
if (string && (guard || chars === undefined)) {
|
||||||
return nativeTrimEnd.call(string)
|
return string[methodName]()
|
||||||
}
|
}
|
||||||
if (!string || !(chars = baseToString(chars))) {
|
if (!string || !(chars = baseToString(chars))) {
|
||||||
return string
|
return string
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import charsStartIndex from './.internal/charsStartIndex.js'
|
|||||||
import stringToArray from './.internal/stringToArray.js'
|
import stringToArray from './.internal/stringToArray.js'
|
||||||
import toString from './toString.js'
|
import toString from './toString.js'
|
||||||
|
|
||||||
const stringProto = String.prototype
|
const methodName = ''.trimLeft ? 'trimLeft' : 'trimStart'
|
||||||
const nativeTrimStart = stringProto.trimLeft || stringProto.trimStart
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes leading whitespace or specified characters from `string`.
|
* Removes leading whitespace or specified characters from `string`.
|
||||||
@@ -28,7 +27,7 @@ const nativeTrimStart = stringProto.trimLeft || stringProto.trimStart
|
|||||||
function trimStart(string, chars, guard) {
|
function trimStart(string, chars, guard) {
|
||||||
string = toString(string)
|
string = toString(string)
|
||||||
if (string && (guard || chars === undefined)) {
|
if (string && (guard || chars === undefined)) {
|
||||||
return nativeTrimStart.call(string)
|
return string[methodName]()
|
||||||
}
|
}
|
||||||
if (!string || !(chars = baseToString(chars))) {
|
if (!string || !(chars = baseToString(chars))) {
|
||||||
return string
|
return string
|
||||||
|
|||||||
Reference in New Issue
Block a user