or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-camelcase

Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/camelcase@8.0.x

To install, run

npx @tessl/cli install tessl/npm-camelcase@8.0.0

index.mddocs/

camelcase

Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: foo-barfooBar. Correctly handles Unicode strings with locale-aware conversion and advanced options for preserving consecutive uppercase characters.

Package Information

  • Package Name: camelcase
  • Package Type: npm
  • Language: JavaScript (ES Module)
  • Installation: npm install camelcase
  • Node.js Requirement: >=16

Core Imports

import camelCase from 'camelcase';

For CommonJS environments:

const camelCase = require('camelcase');

Basic Usage

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('розовый_пушистый_единорог');
//=> 'розовыйПушистыйЕдинорог'

Capabilities

Main Function

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 behavior

Returns: 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:

  • Converts multiple separator types: dash (-), dot (.), underscore (_), space ( )
  • Handles mixed separators and consecutive separators
  • Trims whitespace from input strings
  • Returns empty string for separator-only input or empty input
  • Supports both single strings and arrays of strings as input
  • Array input is joined with hyphens before processing
  • Preserves Unicode character integrity with locale-aware transformations

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'

Types

Options Interface

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:

pascalCase

  • Type: boolean
  • Default: false
  • Description: When true, uppercases the first character to produce PascalCase instead of camelCase
  • Example: camelCase('foo-bar', {pascalCase: true})'FooBar'

preserveConsecutiveUppercase

  • Type: boolean
  • Default: false
  • Description: When true, preserves consecutive uppercase characters instead of converting them to lowercase
  • Example: camelCase('foo-BAR', {preserveConsecutiveUppercase: true})'fooBAR'

locale

  • Type: false | string | readonly string[]
  • Default: Host environment's current locale
  • Description: Specifies locale(s) for case conversion according to locale-specific mappings
  • Values:
    • false: Uses Unicode Default Case Conversion algorithm, ignoring platform locale
    • string: Single locale identifier (e.g., 'en-US', 'tr-TR')
    • string[]: Array of locale identifiers; best available locale is used

Locale 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'