-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114-camelcase.ts
44 lines (39 loc) · 2.26 KB
/
114-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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 114 - CamelCase
*
* Implement `CamelCase<T>` which converts `snake_case` string to `camelCase`.
*
* For example
*
* ```ts
* type camelCase1 = CamelCase<'hello_world_with_types'> // expected to be 'helloWorldWithTypes'
* type camelCase2 = CamelCase<'HELLO_WORLD_WITH_TYPES'> // expected to be same as previous one
* ```
*
*
* <!--info-footer-start--><br><a href="../../README.md" target="_blank"><img src="https://img.shields.io/badge/-Back-grey" alt="Back"/></a> <a href="https://tsch.js.org/114/answer" target="_blank"><img src="https://img.shields.io/badge/-Share%20your%20Solutions-teal" alt="Share your Solutions"/></a> <a href="https://tsch.js.org/114/solutions" target="_blank"><img src="https://img.shields.io/badge/-Check%20out%20Solutions-de5a77?logo=awesome-lists&logoColor=white" alt="Check out Solutions"/></a> <hr><h3>Related Challenges</h3><a href="https://github.com/type-challenges/type-challenges/blob/main/questions/00612-medium-kebabcase/README.md" target="_blank"><img src="https://img.shields.io/badge/-612%E3%83%BBKebabCase-d9901a" alt="612・KebabCase"/></a> <!--info-footer-end-->
*/
/* _____________ Your Code Here _____________ */
type IsLetter<S extends string> = Lowercase<S> extends Uppercase<S> ? false : true;
type CamelCase<S extends string> = S extends `${infer Left}_${infer Right}${infer Rest}`
? IsLetter<Right> extends true
? `${Lowercase<Left>}${Uppercase<Right>}${CamelCase<Rest>}`
: `${Left}_${CamelCase<`${Right}${Rest}`>}`
: Lowercase<S>
/* _____________ Test Cases _____________ */
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<'😎'>, '😎'>>,
]