mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 07:17:50 +00:00
41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
import baseToString from './.internal/baseToString.js'
|
|
import castSlice from './.internal/castSlice.js'
|
|
import charsStartIndex from './.internal/charsStartIndex.js'
|
|
import stringToArray from './.internal/stringToArray.js'
|
|
import toString from './toString.js'
|
|
|
|
const methodName = ''.trimLeft ? 'trimLeft' : 'trimStart'
|
|
|
|
/**
|
|
* Removes leading whitespace or specified characters from `string`.
|
|
*
|
|
* @since 4.0.0
|
|
* @category String
|
|
* @param {string} [string=''] The string to trim.
|
|
* @param {string} [chars=whitespace] The characters to trim.
|
|
* @param- {Object} [guard] Enables use as an iteratee for methods like `map`.
|
|
* @returns {string} Returns the trimmed string.
|
|
* @see trim, trimEnd
|
|
* @example
|
|
*
|
|
* trimStart(' abc ')
|
|
* // => 'abc '
|
|
*
|
|
* trimStart('-_-abc-_-', '_-')
|
|
* // => 'abc-_-'
|
|
*/
|
|
function trimStart(string, chars, guard) {
|
|
string = toString(string)
|
|
if (string && (guard || chars === undefined)) {
|
|
return string[methodName]()
|
|
}
|
|
if (!string || !(chars = baseToString(chars))) {
|
|
return string
|
|
}
|
|
const strSymbols = stringToArray(string)
|
|
const start = charsStartIndex(strSymbols, stringToArray(chars))
|
|
return castSlice(strSymbols, start).join('')
|
|
}
|
|
|
|
export default trimStart
|