trimEnd
string
Removes trailing whitespace or specified characters from string.
Installation
Import
import { trimEnd } from '@tulx/utils';Source Code
Implementation
/**
* Removes trailing 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
* trimEnd(' abc '); // ' abc'
* trimEnd('-_-abc-_-', '_-'); // '-_-abc'
* ```
*/
function escapeRegExpForTrimEnd(chars: string): string {
return chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export function trimEnd(string: string, chars?: string): string {
if (chars === undefined) {
return string.trimEnd();
}
const pattern = `[${escapeRegExpForTrimEnd(chars)}]+`;
return string.replace(new RegExp(`${pattern}$`, 'g'), '');
}
Example
import { trimEnd } from '@tulx/utils';
trimEnd(' abc '); // ' abc'
trimEnd('-_-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.