-
Notifications
You must be signed in to change notification settings - Fork 0
/
612-kebabcase.ts
51 lines (39 loc) · 1.44 KB
/
612-kebabcase.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
/*
612 - KebabCase
-------
by Johnson Chu (@johnsoncodehk) #medium #template-literal
### Question
Replace the `camelCase` or `PascalCase` string with `kebab-case`.
`FooBarBaz` -> `foo-bar-baz`
For example
```ts
type FooBarBaz = KebabCase<"FooBarBaz">;
const foobarbaz: FooBarBaz = "foo-bar-baz";
type DoNothing = KebabCase<"do-nothing">;
const doNothing: DoNothing = "do-nothing";
```
> View on GitHub: https://tsch.js.org/612
*/
/* _____________ Your Code Here _____________ */
type KebabCase<S extends string> = S extends `${infer F}${infer R}`
? R extends Uncapitalize<R> ? `${Uncapitalize<F>}${KebabCase<R>}` : `${Uncapitalize<F>}-${KebabCase<R>}`
: S;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<KebabCase<'FooBarBaz'>, 'foo-bar-baz'>>,
Expect<Equal<KebabCase<'fooBarBaz'>, 'foo-bar-baz'>>,
Expect<Equal<KebabCase<'foo-bar'>, 'foo-bar'>>,
Expect<Equal<KebabCase<'foo_bar'>, 'foo_bar'>>,
Expect<Equal<KebabCase<'Foo-Bar'>, 'foo--bar'>>,
Expect<Equal<KebabCase<'ABC'>, 'a-b-c'>>,
Expect<Equal<KebabCase<'-'>, '-'>>,
Expect<Equal<KebabCase<''>, ''>>,
Expect<Equal<KebabCase<'😎'>, '😎'>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/612/answer
> View solutions: https://tsch.js.org/612/solutions
> More Challenges: https://tsch.js.org
*/