UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

24 lines (23 loc) 825 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.trimleft = trimleft; const escapeForCharClass = (chars) => chars.replace(/[\\\]^/-]/g, '\\$&'); function trimleft(str, chars) { // discuss at: https://locutus.io/tcl/trimleft/ // parity verified: Tcl 8.6 // original by: Kevin van Zonneveld (https://kvz.io) // example 1: trimleft(' hello ') // returns 1: 'hello ' // example 2: trimleft('__hello__', '_') // returns 2: 'hello__' const value = String(str); if (chars === undefined) { return value.trimStart(); } if (chars === '') { return value; } const escaped = escapeForCharClass(chars); const pattern = new RegExp(`^[${escaped}]+`, 'g'); return value.replace(pattern, ''); }