-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhard_camelcase.ts
More file actions
40 lines (36 loc) · 1.51 KB
/
hard_camelcase.ts
File metadata and controls
40 lines (36 loc) · 1.51 KB
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
// https://typehero.dev/challenge/camelcase
import type { Equal, Expect } from '@type-challenges/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<'😎'>, '😎'>>,
]
type CamelCase<S extends string> =
// first + second
S extends `${infer First}${infer Second}${infer Rest}` ?
IsSpace<Second> extends true ?
// any + space
`${Lowercase<First>}${CamelCase<`${Second}${Rest}`>}`
: IsSpace<First> extends true ?
// space + !space
IsAlphabet<Second> extends true ?
// space + alpha
`${Uppercase<Second>}${CamelCase<`${Rest}`>}`
: // space + !(space || alpha)
`${First}${Second}${CamelCase<Rest>}`
: // !space + !space
`${Lowercase<First>}${CamelCase<`${Second}${Rest}`>}`
: Lowercase<S>
type IsSpace<C extends string> = C extends '_' | '-' ? true : false
type IsAlphabet<C extends string> =
Uppercase<C> extends Lowercase<C> ? false : true