-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Simple Pseudo Selectors as typesafe (#9)
- Loading branch information
Showing
3 changed files
with
100 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
// == Interface ================================================================ | ||
export type NonNullableString = string & NonNullable<unknown>; | ||
|
||
export type ToKebabCase< | ||
InputString extends string, | ||
AccumulatorString extends string = "" | ||
> = InputString extends `${infer FirstChar}${infer RemainingString}` | ||
? ToKebabCase< | ||
RemainingString, | ||
`${AccumulatorString}${FirstChar extends Lowercase<FirstChar> | ||
? "" | ||
: "-"}${Lowercase<FirstChar>}` | ||
> | ||
: AccumulatorString; | ||
|
||
export type FromKebabCase<InputString extends string> = | ||
InputString extends `${infer BeforeString}-${infer AfterString}` | ||
? `${BeforeString}${Capitalize<FromKebabCase<AfterString>>}` | ||
: InputString; | ||
|
||
export type ColonToSnake<InputString extends string> = | ||
InputString extends `${infer BeforeString}:${infer AfterString}` | ||
? `${BeforeString}_${ColonToSnake<AfterString>}` | ||
: InputString; | ||
|
||
// == Tests ==================================================================== | ||
if (import.meta.vitest) { | ||
const { describe, it, expectTypeOf } = import.meta.vitest; | ||
|
||
describe.concurrent("String type utils", () => { | ||
it("Convert to kebab", () => { | ||
type CamelCase = ToKebabCase<"backgroundColor">; | ||
expectTypeOf<CamelCase>().toEqualTypeOf<"background-color">(); | ||
|
||
type PascalCase = ToKebabCase<"WebkitTapHighlightColor">; | ||
expectTypeOf<PascalCase>().toEqualTypeOf<"-webkit-tap-highlight-color">(); | ||
}); | ||
|
||
it("Convert from kebab", () => { | ||
type CamelCase = FromKebabCase<"background-color">; | ||
expectTypeOf<CamelCase>().toEqualTypeOf<"backgroundColor">(); | ||
|
||
type PascalCase = FromKebabCase<"-webkit-tap-highlight-color">; | ||
expectTypeOf<PascalCase>().toEqualTypeOf<"WebkitTapHighlightColor">(); | ||
}); | ||
|
||
it("Convert colon to snake", () => { | ||
type SimplePseudo = ColonToSnake<":hover">; | ||
expectTypeOf<SimplePseudo>().toEqualTypeOf<"_hover">(); | ||
|
||
type DoubleSimplePseudo = ColonToSnake<"::before">; | ||
expectTypeOf<DoubleSimplePseudo>().toEqualTypeOf<"__before">(); | ||
}); | ||
}); | ||
} |