@backtrace/sourcemap-tools
Version:
Backtrace-JavaScript sourcemap tools
18 lines (16 loc) • 541 B
text/typescript
/**
* Appends `value` to `str` before trailing whitespaces in `str`.
* @param str String to append to.
* @param value String to append.
* @example
* const str = 'abc\n\n';
* const value = 'def';
* const appended = appendBeforeWhitespaces(str, value); // 'abcdef\n\n'
*/
export function appendBeforeWhitespaces(str: string, value: string) {
const whitespaces = str.match(/\s*$/)?.[0];
if (!whitespaces) {
return str + value;
}
return str.substring(0, str.length - whitespaces.length) + value + whitespaces;
}