-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01383-hard-camelize.ts
54 lines (49 loc) · 1.3 KB
/
01383-hard-camelize.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
43
44
45
46
47
48
49
50
51
52
53
54
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type cases = [
Expect<
Equal<
Camelize<{
some_prop: string
prop: { another_prop: string }
array: [
{ snake_case: string },
{ another_element: { yet_another_prop: string } },
{ yet_another_element: string }
]
}>,
{
someProp: string
prop: { anotherProp: string }
array: [
{ snakeCase: string },
{ anotherElement: { yetAnotherProp: string } },
{ yetAnotherElement: string }
]
}
>
>
]
// ============= Your Code Here =============
type CamelizeArray<T> = T extends [infer Head, ...infer Tail]
? [Camelize<Head>, ...CamelizeArray<Tail>]
: T
type CamelizeCase<T> = T extends `${infer Head}_${infer Second}${infer Tail}`
? `${Head}${CamelizeCase<`${Uppercase<Second>}${Tail}`>}`
: T
type Camelize<T> = T extends object
? {
[P in keyof T as CamelizeCase<P>]: T[P] extends unknown[]
? CamelizeArray<T[P]>
: Camelize<T[P]>
}
: T
type Test = Camelize<{
some_prop: string
prop: { another_prop: string }
array: [
{ snake_case: string },
{ another_element: { yet_another_prop: string } },
{ yet_another_element: string }
]
}>