To Kebab Case
utilcn/to-kebab-caseFreeUtility
Convert a string to kebabCase.
Install it
npx shadcn@latest add https://utilcn.dev/r/to-kebab-case.jsonSource
1/**2 * Converts a string to kebab-case.3 *4 * Handles acronyms and consecutive uppercase letters properly.5 * Example:6 * "XMLHttpRequest" -> "xml-http-request"7 *8 * @param str - The input string.9 * @returns The kebab-cased string.10 *11 * @example12 * toKebabCase("Hello World Again"); // "hello-world-again"13 * toKebabCase("XMLHttpRequest"); // "xml-http-request"14 */15export function toKebabCase(str: string): string {16 return (17 str18 // Split acronym groups (e.g. "XMLHttp" -> "XML-Http")19 .replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2')20 // Split between a lowercase/digit and an uppercase (e.g. "testHTTP" -> "test-HTTP")21 .replace(/([a-z\d])([A-Z])/g, '$1-$2')22 // Replace spaces and underscores with hyphens23 .replace(/[\s_]+/g, '-')24 .toLowerCase()25 // Remove leading and trailing hyphens26 .replace(/^-+|-+$/g, '')27 );28}
Files it writes
More from utilcn
All 17 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Capitalizecapitalize | utilcn | Utilities | Free | no deps | |
| Capitalize Allcapitalize-all | utilcn | Utilities | Free | no deps | |
| Environment Server Configurationenv-server | utilcn | Utilities | Free | 1 dep | |
| Generate Presigned Download URLgenerate-presigned-download-url | utilcn | Utilities | Free | 2 deps · 2 files | |
| Generate Presigned Upload URLgenerate-presigned-upload-url | utilcn | Utilities | Free | 2 deps · 2 files | |
| Repeat Padrepeat-pad | utilcn | Utilities | Free | no deps | |
| Reverse Stringreverse-string | utilcn | Utilities | Free | no deps | |
| To Camel Caseto-camel-case | utilcn | Utilities | Free | no deps |
