To Snake Case
utilcn/to-snake-caseFreeUtility
Convert a string to snake_case.
Install it
npx shadcn@latest add https://utilcn.dev/r/to-snake-case.jsonSource
1/**2 * Converts a string to snake_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 snake_cased string.10 *11 * @example12 * toSnakeCase("Hello World"); // "hello_world"13 * toSnakeCase("XMLHttpRequest"); // "xml_http_request"14 */15export function toSnakeCase(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 dashes with underscores23 .replace(/[\s-]+/g, '_')24 .toLowerCase()25 // Remove leading and trailing underscores26 .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 |
