From 93b6df96bbcd391526a06d3a8dd816ef0e98e287 Mon Sep 17 00:00:00 2001 From: Guga Guichard Date: Tue, 3 Oct 2023 14:47:07 -0300 Subject: [PATCH] fix: Fixes a bug where split by empty string would add an extra empty char to the result tuple --- src/primitives.test.ts | 10 ++++++++++ src/primitives.ts | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/primitives.test.ts b/src/primitives.test.ts index 1a12071..8b0e2cf 100644 --- a/src/primitives.test.ts +++ b/src/primitives.test.ts @@ -77,6 +77,16 @@ describe('primitives', () => { expect(result).toEqual(['some', 'nice', 'string']) type test = Expect> }) + + test('should no add extra characters when splitting by empty string', () => { + const data = 'hello' + const result = subject.split(data, '') + expect(result).toEqual(['h', 'e', 'l', 'l', 'o']) + type test = Expect> + + expect(subject.split('', '')).toEqual([]) + type test2 = Expect, []>> + }) }) test('trimStart', () => { diff --git a/src/primitives.ts b/src/primitives.ts index 0f705ad..a6baf85 100644 --- a/src/primitives.ts +++ b/src/primitives.ts @@ -126,9 +126,11 @@ function replaceAll( */ type Split< T, - delimiter extends string, + delimiter extends string = '', > = T extends `${infer first}${delimiter}${infer rest}` ? [first, ...Split] + : T extends '' + ? [] : [T] /** * A strongly typed version of `String.prototype.split`.