Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: foo-bar → fooBar. Correctly handles Unicode strings with locale-aware conversion and advanced options for preserving consecutive uppercase characters.
npm install camelcaseimport camelCase from 'camelcase';For CommonJS environments:
const camelCase = require('camelcase');import camelCase from 'camelcase';
// Basic string conversion
camelCase('foo-bar');
//=> 'fooBar'
camelCase('foo_bar');
//=> 'fooBar'
camelCase('Foo-Bar');
//=> 'fooBar'
// Array input (joins with hyphens then processes)
camelCase(['foo', 'bar']);
//=> 'fooBar'
// PascalCase conversion
camelCase('foo-bar', {pascalCase: true});
//=> 'FooBar'
// Preserve consecutive uppercase
camelCase('foo-BAR', {preserveConsecutiveUppercase: true});
//=> 'fooBAR'
// Unicode support with locale awareness
camelCase('розовый_пушистый_единорог');
//=> 'розовыйПушистыйЕдинорог'Converts dash/dot/underscore/space separated strings to camelCase or PascalCase with comprehensive Unicode and locale support.
/**
* Convert a dash/dot/underscore/space separated string to camelCase or PascalCase
* @param input - The string or array of strings to convert to camel case
* @param options - Configuration options for conversion behavior
* @returns The converted camelCase string
* @throws TypeError when input is not string | string[]
*/
function camelCase(input: string | readonly string[], options?: Options): string;Parameters:
input: string | readonly string[] - The string to convert or array of strings (joined with hyphens)options: Options (optional) - Configuration object for conversion behaviorReturns: string - The converted camelCase or PascalCase string
Error Handling:
Throws TypeError with message 'Expected the input to be \string | string[]`'` when input is not a string or array of strings.
Behavior Details:
-), dot (.), underscore (_), space ( )Usage Examples:
// Various separator types
camelCase('foo-bar'); //=> 'fooBar'
camelCase('foo.bar'); //=> 'fooBar'
camelCase('foo_bar'); //=> 'fooBar'
camelCase('foo bar'); //=> 'fooBar'
// Mixed and consecutive separators
camelCase('--foo..bar__'); //=> 'fooBar'
camelCase('foo--bar'); //=> 'fooBar'
// Leading/trailing separators and whitespace
camelCase(' foo-bar '); //=> 'fooBar'
camelCase('-foo-bar-'); //=> 'fooBar'
// Array input
camelCase(['foo', 'bar']); //=> 'fooBar'
camelCase(['__foo__', '--bar']); //=> 'fooBar'
// Edge cases
camelCase(''); //=> ''
camelCase(' - '); //=> ''
camelCase(['', '']); //=> ''
// Existing camelCase input
camelCase('fooBar'); //=> 'fooBar'
camelCase('fooBarBaz'); //=> 'fooBarBaz'
// Single character
camelCase('f'); //=> 'f'
camelCase('F'); //=> 'f'Configuration options for controlling conversion behavior.
interface Options {
/** Uppercase the first character to create PascalCase: `foo-bar` → `FooBar` */
readonly pascalCase?: boolean;
/** Preserve consecutive uppercase characters: `foo-BAR` → `FooBAR` */
readonly preserveConsecutiveUppercase?: boolean;
/** Locale for case conversion mappings, or false for Unicode default */
readonly locale?: false | string | readonly string[];
}Property Details:
booleanfalsetrue, uppercases the first character to produce PascalCase instead of camelCasecamelCase('foo-bar', {pascalCase: true}) → 'FooBar'booleanfalsetrue, preserves consecutive uppercase characters instead of converting them to lowercasecamelCase('foo-BAR', {preserveConsecutiveUppercase: true}) → 'fooBAR'false | string | readonly string[]false: Uses Unicode Default Case Conversion algorithm, ignoring platform localestring: Single locale identifier (e.g., 'en-US', 'tr-TR')string[]: Array of locale identifiers; best available locale is usedLocale Usage Examples:
// Single locale
camelCase('lorem-ipsum', {locale: 'en-US'});
//=> 'loremIpsum'
camelCase('lorem-ipsum', {locale: 'tr-TR'});
//=> 'loremİpsum' (Turkish-specific capitalization)
// Multiple locales (best available used)
camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']});
//=> 'loremIpsum'
camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']});
//=> 'loremİpsum'
// Disable locale (use Unicode default)
camelCase('lorem-ipsum', {locale: false});
//=> 'loremIpsum'Combined Options Examples:
// PascalCase with consecutive uppercase preservation
camelCase('fooBAR', {
pascalCase: true,
preserveConsecutiveUppercase: true
});
//=> 'FooBAR'
// Array input with PascalCase
camelCase(['foo', 'BAR'], {
pascalCase: true,
preserveConsecutiveUppercase: true
});
//=> 'FooBAR'
// Locale-aware PascalCase
camelCase('istanbul-turkey', {
pascalCase: true,
locale: 'tr-TR'
});
//=> 'İstanbulTurkey'