diff --git a/src/string/trimEnd.ts b/src/string/trimEnd.ts index 33237a187..d712bfa3f 100644 --- a/src/string/trimEnd.ts +++ b/src/string/trimEnd.ts @@ -36,6 +36,7 @@ export function trimEnd(str: string, chars?: string | string[]): string { while (endIndex > 0 && chars.includes(str[endIndex - 1])) { endIndex--; } + break; } } diff --git a/src/string/trimStart.spec.ts b/src/string/trimStart.spec.ts index df5eca675..db6bc81cd 100644 --- a/src/string/trimStart.spec.ts +++ b/src/string/trimStart.spec.ts @@ -65,4 +65,8 @@ describe('trimStart', () => { it('should remove leading spaces and other characters when specified in an array', () => { expect(trimStart(' hello world', [' ', 'h'])).toEqual('ello world'); }); + + it('should throw an error when chars is a string with multiple characters', () => { + expect(() => trimStart('hello', 'ab')).toThrow(`The 'chars' parameter should be a single character string.`); + }); }); diff --git a/src/string/trimStart.ts b/src/string/trimStart.ts index f25da8e26..6f829501d 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++; } @@ -32,6 +36,7 @@ export function trimStart(str: string, chars?: string | string[]): string { while (startIndex < str.length && chars.includes(str[startIndex])) { startIndex++; } + break; } }