1 Star 0 Fork 0

UnPourTous/lodash

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
trim.js 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
import castSlice from './.internal/castSlice.js'
import charsEndIndex from './.internal/charsEndIndex.js'
import charsStartIndex from './.internal/charsStartIndex.js'
import stringToArray from './.internal/stringToArray.js'
/**
* Removes leading and trailing whitespace or specified characters from `string`.
*
* @since 3.0.0
* @category String
* @param {string} [string=''] The string to trim.
* @param {string} [chars=whitespace] The characters to trim.
* @returns {string} Returns the trimmed string.
* @see trimEnd, trimStart
* @example
*
* trim(' abc ')
* // => 'abc'
*
* trim('-_-abc-_-', '_-')
* // => 'abc'
*/
function trim(string, chars) {
if (string && chars === undefined) {
return string.trim()
}
if (!string || !chars) {
return string
}
const strSymbols = stringToArray(string)
const chrSymbols = stringToArray(chars)
const start = charsStartIndex(strSymbols, chrSymbols)
const end = charsEndIndex(strSymbols, chrSymbols) + 1
return castSlice(strSymbols, start, end).join('')
}
export default trim
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/UnPourTous/lodash.git
git@gitee.com:UnPourTous/lodash.git
UnPourTous
lodash
lodash
master

搜索帮助