UNPKG

voca

Version:

The ultimate JavaScript string library

53 lines (44 loc) 1.51 kB
import { i as isNil } from './internal/is_nil.js'; import './is_string.js'; import { c as coerceToString } from './internal/coerce_to_string.js'; import { j as REGEXP_TRIM_LEFT } from './internal/const.js'; import { t as toString } from './internal/to_string.js'; import './internal/to_integer.js'; import includes from './includes.js'; var reduce = Array.prototype.reduce; /** * Removes whitespaces from the left side of the `subject`. * * @function trimLeft * @static * @since 1.0.0 * @memberOf Manipulate * @param {string} [subject=''] The string to trim. * @param {string} [whitespace=whitespace] The whitespace characters to trim. List all characters that you want to be stripped. * @return {string} Returns the trimmed string. * @example * v.trimLeft(' Starship Troopers'); * // => 'Starship Troopers' * * v.trimLeft('***Mobile Infantry', '*'); * // => 'Mobile Infantry' */ function trimLeft(subject, whitespace) { var subjectString = coerceToString(subject); if (whitespace === '' || subjectString === '') { return subjectString; } var whitespaceString = toString(whitespace); if (isNil(whitespaceString)) { return subjectString.replace(REGEXP_TRIM_LEFT, ''); } var matchWhitespace = true; return reduce.call(subjectString, function (trimmed, character) { if (matchWhitespace && includes(whitespaceString, character)) { return trimmed; } matchWhitespace = false; return trimmed + character; }, ''); } export default trimLeft;