locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
24 lines (23 loc) • 799 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.trimstart = trimstart;
function escapeForCharClass(chars) {
return chars.replace(/[\\\]^/-]/g, '\\$&');
}
function trimstart(str, chars) {
// discuss at: https://locutus.io/powershell/trimstart/
// parity verified: PowerShell 7.4
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: trimstart(' hello ')
// returns 1: 'hello '
// example 2: trimstart('__hello__', '_')
// returns 2: 'hello__'
const value = String(str);
if (chars === undefined) {
return value.trimStart();
}
if (chars === '') {
return value;
}
return value.replace(new RegExp(`^[${escapeForCharClass(chars)}]+`, 'g'), '');
}