Last active
October 18, 2024 14:18
-
-
Save albertms10/09f14ef7ebdc3ce0e95683c728616253 to your computer and use it in GitHub Desktop.
TypeScript type to convert camel-cased strings to kebab-cased
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
/** | |
* Converts the given string from camel-case to kebab-case. | |
* @template T The string to convert the case. | |
* @see https://gist.github.com/albertms10/09f14ef7ebdc3ce0e95683c728616253 | |
* @example | |
* type Kebab = CamelToKebab<'exampleVarName'>; | |
* // 'example-var-name' | |
*/ | |
type CamelToKebab<S extends string> = S extends `${infer T}${infer U}` | |
? U extends Uncapitalize<U> | |
? `${Uncapitalize<T>}${CamelToKebab<U>}` | |
: `${Uncapitalize<T>}-${CamelToKebab<U>}` | |
: ''; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this. The inverse comes in handy sometimes too, so just sharing here: