Kebabcase
介绍
替换camelCase
或PascalCase
字符串为kebab-case
. FooBarBaz
-> foo-bar-baz
ts
type FooBarBaz = KebabCase<"FooBarBaz">const foobarbaz: FooBarBaz = "foo-bar-baz"type DoNothing = KebabCase<"do-nothing">const doNothing: DoNothing = "do-nothing"
View on GitHubts
type FooBarBaz = KebabCase<"FooBarBaz">const foobarbaz: FooBarBaz = "foo-bar-baz"type DoNothing = KebabCase<"do-nothing">const doNothing: DoNothing = "do-nothing"
起点
ts
/* _____________ Your Code Here _____________ */typeKebabCase <S > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'FooBarBaz'>, 'foo-bar-baz'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'fooBarBaz'>, 'foo-bar-baz'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'foo-bar'>, 'foo-bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'foo_bar'>, 'foo_bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'Foo-Bar'>, 'foo--bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'ABC'>, 'a-b-c'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'-'>, '-'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <''>, ''>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'😎'>, '😎'>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeKebabCase <S > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'FooBarBaz'>, 'foo-bar-baz'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'fooBarBaz'>, 'foo-bar-baz'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'foo-bar'>, 'foo-bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'foo_bar'>, 'foo_bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'Foo-Bar'>, 'foo--bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'ABC'>, 'a-b-c'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'-'>, '-'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <''>, ''>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <KebabCase <'😎'>, '😎'>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeKebabCase <S extends string> =S extends `${inferS1 }${inferS2 }`?S2 extendsUncapitalize <S2 >? `${Uncapitalize <S1 >}${KebabCase <S2 >}`: `${Uncapitalize <S1 >}-${KebabCase <S2 >}`:S ;
ts
// most populartypeKebabCase <S extends string> =S extends `${inferS1 }${inferS2 }`?S2 extendsUncapitalize <S2 >? `${Uncapitalize <S1 >}${KebabCase <S2 >}`: `${Uncapitalize <S1 >}-${KebabCase <S2 >}`:S ;
ts
// 方法2 when i see my solution, i am tired too.typeStrToArr <T extends string ,U extends any[] = []> =T extends `${inferF }${inferRest }`?StrToArr <Rest , [...U ,F ]>:U ;typeArrToStr <T extends any[],U extends string = ''> =T extends [inferF , ...inferRest ]?Type 'F' is not assignable to type 'string | number | bigint | boolean | null | undefined'.2322Type 'F' is not assignable to type 'string | number | bigint | boolean | null | undefined'.ArrToStr <Rest , `${U }${}`> F :U ;typeUpperToLower = {A : 'a';B : 'b';C : 'c';D : 'd';E : 'e';F : 'f';G : 'g';H : 'h';I : 'i';J : 'j';K : 'k';L : 'l';M : 'm';N : 'n';O : 'o';P : 'p';Q : 'q';R : 'r';S : 's';T : 't';U : 'u';V : 'v';W : 'w';X : 'x';Y : 'y';Z : 'z';}typestrArr <S extends string> =StrToArr <S >;typeFormatArr <strArr ,T extends any[] = []> =strArr extends [inferF , ...inferRest extends string[]]? [F extends keyofUpperToLower ? `-${UpperToLower [F ]}` :F , ...FormatArr <Rest >]:T ;typeKebabCase <S extends string> =ArrToStr <FormatArr <strArr <S >>> extends `-${inferRest extends string}`?Rest extends '' ? '-' :Rest :ArrToStr <FormatArr <strArr <S >>>
ts
// 方法2 when i see my solution, i am tired too.typeStrToArr <T extends string ,U extends any[] = []> =T extends `${inferF }${inferRest }`?StrToArr <Rest , [...U ,F ]>:U ;typeArrToStr <T extends any[],U extends string = ''> =T extends [inferF , ...inferRest ]?Type 'F' is not assignable to type 'string | number | bigint | boolean | null | undefined'.2322Type 'F' is not assignable to type 'string | number | bigint | boolean | null | undefined'.ArrToStr <Rest , `${U }${}`> F :U ;typeUpperToLower = {A : 'a';B : 'b';C : 'c';D : 'd';E : 'e';F : 'f';G : 'g';H : 'h';I : 'i';J : 'j';K : 'k';L : 'l';M : 'm';N : 'n';O : 'o';P : 'p';Q : 'q';R : 'r';S : 's';T : 't';U : 'u';V : 'v';W : 'w';X : 'x';Y : 'y';Z : 'z';}typestrArr <S extends string> =StrToArr <S >;typeFormatArr <strArr ,T extends any[] = []> =strArr extends [inferF , ...inferRest extends string[]]? [F extends keyofUpperToLower ? `-${UpperToLower [F ]}` :F , ...FormatArr <Rest >]:T ;typeKebabCase <S extends string> =ArrToStr <FormatArr <strArr <S >>> extends `-${inferRest extends string}`?Rest extends '' ? '-' :Rest :ArrToStr <FormatArr <strArr <S >>>