-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9616-parse-url-params.ts
42 lines (35 loc) · 1.24 KB
/
9616-parse-url-params.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
/**
* 9616 - Parse URL Params
*
* You're required to implement a type-level parser to parse URL params string into an Union.
*
* ```ts
* ParseUrlParams<':id'> // id
* ParseUrlParams<'posts/:id'> // id
* ParseUrlParams<'posts/:id/:user'> // id | user
* ```
*
*
*/
/* _____________ Your Code Here _____________ */
type SplitBy<S extends string, SplitChar extends string> = S extends `${infer A}${SplitChar}${infer B}`
? [A, ...SplitBy<B, SplitChar>]
: S extends ''
? []
: [S]
type ExtractUrlParams<L extends string[]> = L extends [infer A, ...infer R extends string[]]
? A extends `:${infer Val}`
? Val | ExtractUrlParams<R>
: ExtractUrlParams<R>
: never;
type ParseUrlParams<T extends string> = ExtractUrlParams<SplitBy<T, '/'>>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<ParseUrlParams<''>, never>>,
Expect<Equal<ParseUrlParams<':id'>, 'id'>>,
Expect<Equal<ParseUrlParams<'posts/:id'>, 'id'>>,
Expect<Equal<ParseUrlParams<'posts/:id/'>, 'id'>>,
Expect<Equal<ParseUrlParams<'posts/:id/:user'>, 'id' | 'user'>>,
Expect<Equal<ParseUrlParams<'posts/:id/:user/like'>, 'id' | 'user'>>,
]