Aller au contenu principal

camelCase()

camelCase(str): string

Converts a string to camelCase.

remarque

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

InputOutput
background-colorbackgroundColor
font_sizefontSize
Hello WorldhelloWorld
PascalCasepascalCase
HTTPRequesthttpRequest

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 }