camelCase
VerifiedConverts any string (spaces, kebab, snake, Pascal) to camelCase.
- string
- case
- camelcase
Install
npx shadcn@latest add https://quicksnip.dev/r/camel-case.jsonCode
/** Splits any casing style (spaces, kebab, snake, camel, Pascal) into plain words. */
const words = (input: string): string[] =>
input
.normalize("NFKD")
.replace(/[̀-ͯ]/g, "")
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
.split(/[^A-Za-z0-9]+/)
.filter(Boolean);
/** Converts any string (spaces, kebab, snake, Pascal) to camelCase. */
export function camelCase(input: string): string {
return words(input)
.map((w, i) => (i === 0 ? w.toLowerCase() : w[0].toUpperCase() + w.slice(1).toLowerCase()))
.join("");
}Usage
camelCase("hello-world"); // "helloWorld"
camelCase("XMLHttpRequest"); // "xmlHttpRequest"