camel-case
Converts a string to camelCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word except the first one. Commonly used for JavaScript variable names, object properties, and function names.
Installation
npx fragmen add string/camel-caseSource Code
/**
* Converts a string to camelCase.
*
* Transforms a string by removing spaces, underscores, and hyphens, then capitalizing
* the first letter of each word except the first one. Commonly used for JavaScript
* variable names, object properties, and function names.
*
* @tags pure, string-manipulation, formatting
* @param {string} str The string to convert
* @returns {string} The camelCase version of the string
*
* @example
* ```typescript
* camelCase('Hello World'); // 'helloWorld'
* camelCase('first_name'); // 'firstName'
* camelCase('kebab-case-string'); // 'kebabCaseString'
* camelCase('PascalCase'); // 'pascalCase'
* camelCase(' multiple spaces '); // 'multipleSpaces'
* camelCase('alreadyCamelCase'); // 'alreadyCamelCase'
* camelCase(''); // ''
* ```
*/
export function camelCase(str: string): string {
if (typeof str !== 'string') {
return '';
}
return (
str
.trim()
// Split on spaces, underscores, hyphens, and camelCase boundaries
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2') // XMLHttp -> XML Http
.replace(/([a-z])([A-Z])/g, '$1 $2') // Insert space before uppercase in camelCase
.replace(/([0-9])([A-Z])/g, '$1 $2') // version2Update -> version2 Update
.split(/[\s_-]+/)
.filter(word => word.length > 0)
.map((word, index) => {
const lowerWord = word.toLowerCase();
// First word stays lowercase, subsequent words get capitalized
return index === 0
? lowerWord
: lowerWord.charAt(0).toUpperCase() + lowerWord.slice(1);
})
.join('')
);
}
Examples
camelCase('Hello World'); // 'helloWorld'
camelCase('first_name'); // 'firstName'
camelCase('kebab-case-string'); // 'kebabCaseString'
camelCase('PascalCase'); // 'pascalCase'
camelCase(' multiple spaces '); // 'multipleSpaces'
camelCase('alreadyCamelCase'); // 'alreadyCamelCase'
camelCase(''); // ''
Related Utilities
capitalize
stringCapitalizes the first letter of a string. Converts the first character to uppercase while leaving the rest unchanged. Safely handles edge cases like empty strings and non-string inputs.
kebab-case
stringConverts a string to kebab-case. Transforms a string by converting it to lowercase and replacing spaces, underscores, and camelCase boundaries with hyphens. Useful for creating URL slugs, CSS class names, and file names.
pad-end
stringPads the end of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).
pad-start
stringPads the start of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).
pascal-case
stringConverts a string to PascalCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word including the first one. Commonly used for class names, interface names, type names, and component names.
snake-case
stringConverts a string to snake_case. Transforms a string by converting it to lowercase and replacing spaces, hyphens, and camelCase boundaries with underscores. Commonly used for database column names, Python variables, and configuration keys.
Quick Actions
Tags
Parameters
strstringThe string to convert
Returns
stringThe camelCase version of the string