You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
987 B
32 lines
987 B
import type { Equal, Expect } from '@type-challenges/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[]>>,
|
|
Expect<Equal<Split<string, string>, string[]>>,
|
|
Expect<Equal<Split<'This is some real shit', string>, string[]>>,
|
|
]
|
|
|
|
type X = Split<'This is some real shit', string>;
|
|
|
|
type Split<
|
|
S extends string,
|
|
Z extends string,
|
|
> =
|
|
S extends string
|
|
? string extends S
|
|
? string[]
|
|
: Z extends string
|
|
? string extends Z
|
|
? string[]
|
|
: S extends `${infer Head}${Z}${infer Tail}`
|
|
? [Head, ...Split<Tail, Z>]
|
|
: Z extends '' ? [] : [S]
|
|
: never
|
|
: never
|
|
;
|