Skip to content

Commit

Permalink
fix: Fixes a bug where split by empty string would add an extra empty…
Browse files Browse the repository at this point in the history
… char to the result tuple
  • Loading branch information
gustavoguichard committed Oct 3, 2023
1 parent 5646d71 commit 93b6df9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ describe('primitives', () => {
expect(result).toEqual(['some', 'nice', 'string'])
type test = Expect<Equal<typeof result, ['some', 'nice', 'string']>>
})

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<Equal<typeof result, ['h', 'e', 'l', 'l', 'o']>>

expect(subject.split('', '')).toEqual([])
type test2 = Expect<Equal<Subject.Split<''>, []>>
})
})

test('trimStart', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ function replaceAll<T extends string, S extends string, R extends string = ''>(
*/
type Split<
T,
delimiter extends string,
delimiter extends string = '',
> = T extends `${infer first}${delimiter}${infer rest}`
? [first, ...Split<rest, delimiter>]
: T extends ''
? []
: [T]
/**
* A strongly typed version of `String.prototype.split`.
Expand Down

0 comments on commit 93b6df9

Please sign in to comment.