-
Notifications
You must be signed in to change notification settings - Fork 0
/
02822-hard-split.ts
42 lines (40 loc) · 916 Bytes
/
02822-hard-split.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type cases = [
Expect<Equal<Split<'Hi! How are you?', 'z'>, ['Hi! How are you?']>>,
Expect<Equal<Split<'Hi! How are you?', ' '>, ['Hi!', 'How', 'are', 'you?']>>,
Expect<
Equal<
Split<'Hi! How are you?', ''>,
[
'H',
'i',
'!',
' ',
'H',
'o',
'w',
' ',
'a',
'r',
'e',
' ',
'y',
'o',
'u',
'?'
]
>
>,
Expect<Equal<Split<'', ''>, []>>,
Expect<Equal<Split<'', 'z'>, ['']>>,
Expect<Equal<Split<string, 'whatever'>, string[]>>
]
// ============= Your Code Here =============
type Split<T extends string, U extends string> = string extends T
? string[]
: T extends `${infer Head}${U}${infer Tail}`
? [Head, ...Split<Tail, U>]
: U extends ''
? []
: [T]