trimStart
string
Removes leading whitespace or specified characters from string.
Installation
Import
import { trimStart } from '@tulx/utils';Source Code
Implementation
/**
* Removes leading whitespace or specified characters from string.
*
* @param string - The string to trim.
* @param chars - The characters to trim.
* @returns Returns the trimmed string.
*
* @example
* ```ts
* trimStart(' abc '); // 'abc '
* trimStart('-_-abc-_-', '_-'); // 'abc-_-'
* ```
*/
function escapeRegExpForTrimStart(chars: string): string {
return chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export function trimStart(string: string, chars?: string): string {
if (chars === undefined) {
return string.trimStart();
}
const pattern = `[${escapeRegExpForTrimStart(chars)}]+`;
return string.replace(new RegExp(`^${pattern}`, 'g'), '');
}
Example
import { trimStart } from '@tulx/utils';
trimStart(' abc '); // 'abc '
trimStart('-_-abc-_-', '_-'); // 'abc-_-'Related Functions
camelCase
Converts string to camel case.
capitalize
Converts the first character of string to upper case and the remaining to lower case.
deburr
Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.
endsWith
Checks if string ends with the given target string.
escape
Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
escapeRegExp
Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.