To Camel Case
utilcn/to-camel-caseFreeUtility
Convert a string to camelCase.
Install it
npx shadcn@latest add https://utilcn.dev/r/to-camel-case.jsonSource
1/**2 * Converts a string to camelCase.3 *4 * Handles acronyms properly, ensuring that something like5 * "XMLHttpRequest" becomes "xmlHttpRequest".6 *7 * @param str - The input string.8 * @returns The camelCased string.9 *10 * @example11 * toCamelCase("hello world-example"); // "helloWorldExample"12 * toCamelCase("XMLHttpRequest"); // "xmlHttpRequest"13 */14export function toCamelCase(str: string): string {15 const result = str16 // Split acronym groups into proper boundaries17 .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')18 // Split between lowercase/digit and uppercase19 .replace(/([a-z\d])([A-Z])/g, '$1 $2')20 // Normalize spaces/underscores/dashes21 .replace(/[-_\s]+/g, ' ')22 // Lowercase everything to start clean23 .toLowerCase()24 // Capitalize first letter of each "word" after the first25 .replace(/\s+(\w)/g, (_, c) => c.toUpperCase())26 // Remove any remaining spaces and trim27 .replace(/\s+/g, '')28 .trim();2930 // Ensure first character is lowercase for camelCase31 return result.charAt(0).toLowerCase() + result.slice(1);32}
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 Kebab Caseto-kebab-case | utilcn | Utilities | Free | no deps |
