-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00114-hard-camelcase.ts
27 lines (25 loc) · 1.09 KB
/
00114-hard-camelcase.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
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type cases = [
Expect<Equal<CamelCase<'foobar'>, 'foobar'>>,
Expect<Equal<CamelCase<'FOOBAR'>, 'foobar'>>,
Expect<Equal<CamelCase<'foo_bar'>, 'fooBar'>>,
Expect<Equal<CamelCase<'foo__bar'>, 'foo_Bar'>>,
Expect<Equal<CamelCase<'foo_$bar'>, 'foo_$bar'>>,
Expect<Equal<CamelCase<'foo_bar_'>, 'fooBar_'>>,
Expect<Equal<CamelCase<'foo_bar__'>, 'fooBar__'>>,
Expect<Equal<CamelCase<'foo_bar_$'>, 'fooBar_$'>>,
Expect<Equal<CamelCase<'foo_bar_hello_world'>, 'fooBarHelloWorld'>>,
Expect<Equal<CamelCase<'HELLO_WORLD_WITH_TYPES'>, 'helloWorldWithTypes'>>,
Expect<Equal<CamelCase<'-'>, '-'>>,
Expect<Equal<CamelCase<''>, ''>>,
Expect<Equal<CamelCase<'😎'>, '😎'>>
]
// ============= Your Code Here =============
type CamelCase<S extends string> = S extends Lowercase<S>
? S extends `${infer Head}_${infer Body}${infer Tail}`
? Body extends Uppercase<Body>
? `${Head}_${CamelCase<`${Body}${Tail}`>}`
: `${Head}${Uppercase<Body>}${CamelCase<Tail>}`
: S
: CamelCase<Lowercase<S>>