diff --git a/src/string/trimStart.spec.ts b/src/string/trimStart.spec.ts index df5eca675..ff7a3ae08 100644 --- a/src/string/trimStart.spec.ts +++ b/src/string/trimStart.spec.ts @@ -46,6 +46,10 @@ describe('trimStart', () => { expect(trimStart(' hello world ')).toEqual('hello world '); }); + it('should throw an error when chars is a multi-character string', () => { + expect(() => trimStart('---hello', '--')).toThrow(); + }); + it('should remove leading characters when chars is an array', () => { expect(trimStart('---hello', ['-', 'h'])).toEqual('ello'); }); diff --git a/src/string/trimStart.ts b/src/string/trimStart.ts index f25da8e26..35ba4cb17 100644 --- a/src/string/trimStart.ts +++ b/src/string/trimStart.ts @@ -23,6 +23,10 @@ export function trimStart(str: string, chars?: string | string[]): string { switch (typeof chars) { case 'string': { + if (chars.length !== 1) { + throw new Error(`The 'chars' parameter should be a single character string.`); + } + while (startIndex < str.length && str[startIndex] === chars) { startIndex++; }