camelCase()
camelCase(
str):string
Converts a string to camelCase.
Handles kebab-case, snake_case, PascalCase, space-separated strings, and acronyms.
Parametersโ
str: stringโ
The string to convert.
Returns: stringโ
The string in camelCase.
Sinceโ
2.0.0
Performanceโ
O(n) single-pass charCode traversal, no regex.
Also known asโ
camel (Radashi) ยท camelCase (Lodash, es-toolkit, Modern Dash) ยท toCamelCase (Remeda) ยท โ (Ramda, Effect, Antfu)
Exampleโ
camelCase('background-color'); // => 'backgroundColor'
camelCase('font_size'); // => 'fontSize'
camelCase('Hello World'); // => 'helloWorld'
camelCase('PascalCase'); // => 'pascalCase'
camelCase('HTTPRequest'); // => 'httpRequest'
camelCase('--foo--bar--'); // => 'fooBar'
How it works?โ
Converts a string to camelCase format. First word lowercase, subsequent words capitalized, no separators.
Conversion Examplesโ
| Input | Output |
|---|---|
background-color | backgroundColor |
font_size | fontSize |
Hello World | helloWorld |
PascalCase | pascalCase |
HTTPRequest | httpRequest |
Processโ
Use Casesโ
Normalize keys ๐โ
Convert property names or identifiers to standard JavaScript camelCase.
Essential for API response normalization and consistent coding style.
const key = camelCase('user-id'); // 'userId'
const prop = camelCase('first_name'); // 'firstName'
Ensure identifier safetyโ
Ensure a string is a valid variable identifier. Useful when generating code or dynamic property accessors.
const varName = camelCase('My Class Name'); // 'myClassName'
Convert API response keys from snake_caseโ
Transform snake_case keys from a backend API into JavaScript-standard camelCase. The most common real-world use case, especially with Python/Ruby/Go backends.
const apiResponse = {
user_id: 42,
first_name: "Alice",
created_at: "2025-01-15",
is_active: true,
};
// Combined with mapKeys for full object transformation
const normalized = mapKeys(apiResponse, (_, key) => camelCase(key));
// => { userId: 42, firstName: "Alice", createdAt: "2025-01-15", isActive: true }