-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path612-kebabcase.ts
47 lines (41 loc) · 1.29 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
/**
* 612 - KebabCase
*
* 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"
* ```
*/
/* _____________ Your Code Here _____________ */
type Uncapitalize<S extends string, Output extends string []= []> =
S extends `${infer A}${infer Rest}`
? `${Lowercase<A>}${Rest}`
: S;
type KebabCase<S extends string> = S extends `${infer A}${infer B}`
? B extends Uncapitalize<B>
? `${Uncapitalize<A>}${KebabCase<B>}`
: `${Uncapitalize<A>}-${KebabCase<B>}`
: '';
type R = KebabCase<'😎'>;
/* _____________ 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<'😎'>, '😎'>>,
]