camelcase vs camelcase-keys vs change-case vs case-anything vs to-camel-case vs uppercamelcase
String Case Transformation Comparison
3 Years
camelcasecamelcase-keyschange-casecase-anythingto-camel-caseuppercamelcaseSimilar Packages:
What's String Case Transformation?

String case transformation libraries in JavaScript provide functions to convert strings between different casing styles, such as camelCase, snake_case, kebab-case, and more. These libraries are useful for formatting strings to meet specific coding standards, API requirements, or user interface designs. They help automate the process of changing string cases, ensuring consistency and reducing manual errors in string manipulation tasks. Examples include converting a string like 'hello world' to 'helloWorld' (camelCase) or 'hello-world' (kebab-case).

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
camelcase98,529,513
69511.2 kB32 years agoMIT
camelcase-keys14,723,470
72312.9 kB162 years agoMIT
change-case11,281,489
2,36435.9 kB13a year agoMIT
case-anything880,677
10833.1 kB106 months agoMIT
to-camel-case583,081
35-010 years agoMIT
uppercamelcase78,223
80-58 years agoMIT
Feature Comparison: camelcase vs camelcase-keys vs change-case vs case-anything vs to-camel-case vs uppercamelcase

Case Conversion

  • camelcase:

    camelcase converts a given string to camelCase, which means it transforms the string by removing spaces and special characters while capitalizing the first letter of each subsequent word. For example, 'hello world' becomes 'helloWorld'.

  • camelcase-keys:

    camelcase-keys converts the keys of an object to camelCase recursively. It does not modify the values, only the keys. For instance, an object with keys like { 'first_name': 'John', 'last_name': 'Doe' } would be transformed to { firstName: 'John', lastName: 'Doe' }.

  • change-case:

    change-case offers a wide range of case conversion functions, including camelCase, snake_case, kebab-case, and more. It allows for easy conversion between different case styles, making it a versatile choice for projects that require extensive string manipulation. For example, it can convert 'hello world' to 'hello-world' (kebab-case) or 'helloWorld' to 'hello_world' (snake_case).

  • case-anything:

    case-anything supports multiple case conversions, including camelCase, snake_case, and kebab-case. It provides a flexible API that allows you to specify the desired case format, making it suitable for various use cases. For example, it can convert 'hello_world' to 'helloWorld' (camelCase) or 'hello-world' to 'hello_world' (snake_case).

  • to-camel-case:

    to-camel-case focuses solely on converting strings to camelCase. It is a lightweight and straightforward utility that handles simple case conversion tasks efficiently. For example, it can convert 'hello world' to 'helloWorld' or 'hello_world' to 'helloWorld'.

  • uppercamelcase:

    uppercamelcase converts strings to UpperCamelCase (PascalCase), where the first letter of each word is capitalized. This is useful for scenarios that require a more formal casing style. For example, 'hello world' would be transformed to 'HelloWorld'.

Object Key Transformation

  • camelcase-keys:

    camelcase-keys is specifically designed to transform object keys to camelCase. It operates recursively, meaning it will convert keys at all levels of nested objects. This feature is particularly useful when dealing with API responses that use snake_case or kebab-case keys, allowing you to standardize them to camelCase for consistency in your JavaScript code.

  • change-case:

    change-case provides utilities for transforming both strings and object keys. While it does not specifically focus on key transformation, it offers functions that can be used to convert keys to different case styles, including camelCase, snake_case, and kebab-case. This makes it a versatile choice for projects that need comprehensive case manipulation.

  • case-anything:

    case-anything does not transform object keys; it focuses on string case conversion. However, it provides a flexible API for handling various case formats, making it a good choice for projects that require multiple case transformations without modifying object structures.

Modularity

  • change-case:

    change-case is highly modular, allowing you to import only the specific case transformation functions you need. This reduces bundle size and improves performance, making it an excellent choice for projects that require only a subset of its features.

  • case-anything:

    case-anything is designed to be flexible and modular, supporting multiple case formats with a single API. However, it does not offer the same level of modularity as change-case, as it focuses on providing a unified interface for various case conversions rather than individual function imports.

Ease of Use: Code Examples

  • camelcase:

    Simple camelCase conversion with camelcase

    import camelcase from 'camelcase';
    console.log(camelcase('hello world')); // Output: helloWorld
    
  • camelcase-keys:

    Object key transformation with camelcase-keys

    import camelcaseKeys from 'camelcase-keys';
    const obj = { first_name: 'John', last_name: 'Doe' };
    const newObj = camelcaseKeys(obj);
    console.log(newObj); // Output: { firstName: 'John', lastName: 'Doe' }
    
  • change-case:

    Comprehensive case transformation with change-case

    import { camelCase, snakeCase, kebabCase } from 'change-case';
    console.log(camelCase('hello world')); // Output: helloWorld
    console.log(snakeCase('hello world')); // Output: hello_world
    console.log(kebabCase('hello world')); // Output: hello-world
    
  • case-anything:

    Multiple case conversions with case-anything

    import { toCamelCase, toSnakeCase } from 'case-anything';
    console.log(toCamelCase('hello_world')); // Output: helloWorld
    console.log(toSnakeCase('helloWorld')); // Output: hello_world
    
  • to-camel-case:

    Simple camelCase conversion with to-camel-case

    import toCamelCase from 'to-camel-case';
    console.log(toCamelCase('hello world')); // Output: helloWorld
    
  • uppercamelcase:

    UpperCamelCase conversion with uppercamelcase

    import uppercamelcase from 'uppercamelcase';
    console.log(uppercamelcase('hello world')); // Output: HelloWorld
    
How to Choose: camelcase vs camelcase-keys vs change-case vs case-anything vs to-camel-case vs uppercamelcase
  • camelcase:

    Choose camelcase if you need a simple and lightweight solution for converting a single string to camelCase. It is ideal for quick transformations without any additional dependencies or complexity.

  • camelcase-keys:

    Select camelcase-keys when you need to convert the keys of an object to camelCase recursively. This is useful for normalizing API responses or ensuring consistent key naming in JavaScript objects.

  • change-case:

    Opt for change-case if you need a comprehensive solution for converting strings between various cases. It offers a modular approach with a wide range of case transformation functions, making it ideal for projects that require extensive case manipulation.

  • case-anything:

    Use case-anything if you require a versatile library that can handle multiple case conversions (camelCase, snake_case, kebab-case, etc.) with a single API. It is suitable for projects that need flexibility in handling different casing styles.

  • to-camel-case:

    Choose to-camel-case for a straightforward and focused solution that converts strings to camelCase. It is lightweight and easy to use, making it perfect for simple use cases where only camelCase conversion is needed.

  • uppercamelcase:

    Select uppercamelcase when you specifically need to convert strings to UpperCamelCase (PascalCase). This is useful for scenarios where you need the first letter of each word to be capitalized, such as in certain naming conventions or UI components.

README for camelcase

camelcase

Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: foo-barfooBar

Correctly handles Unicode strings.

If you use this on untrusted user input, don't forget to limit the length to something reasonable.

Install

npm install camelcase

Usage

import camelCase from 'camelcase';

camelCase('foo-bar');
//=> 'fooBar'

camelCase('foo_bar');
//=> 'fooBar'

camelCase('Foo-Bar');
//=> 'fooBar'

camelCase('розовый_пушистый_единорог');
//=> 'розовыйПушистыйЕдинорог'

camelCase('Foo-Bar', {pascalCase: true});
//=> 'FooBar'

camelCase('--foo.bar', {pascalCase: false});
//=> 'fooBar'

camelCase('Foo-BAR', {preserveConsecutiveUppercase: true});
//=> 'fooBAR'

camelCase('fooBAR', {pascalCase: true, preserveConsecutiveUppercase: true});
//=> 'FooBAR'

camelCase('foo bar');
//=> 'fooBar'

console.log(process.argv[3]);
//=> '--foo-bar'
camelCase(process.argv[3]);
//=> 'fooBar'

camelCase(['foo', 'bar']);
//=> 'fooBar'

camelCase(['__foo__', '--bar'], {pascalCase: true});
//=> 'FooBar'

camelCase(['foo', 'BAR'], {pascalCase: true, preserveConsecutiveUppercase: true})
//=> 'FooBAR'

camelCase('lorem-ipsum', {locale: 'en-US'});
//=> 'loremIpsum'

API

camelCase(input, options?)

input

Type: string | string[]

The string to convert to camel case.

options

Type: object

pascalCase

Type: boolean
Default: false

Uppercase the first character: foo-barFooBar

preserveConsecutiveUppercase

Type: boolean
Default: false

Preserve consecutive uppercase characters: foo-BARFooBAR.

locale

Type: false | string | string[]
Default: The host environment’s current locale.

The locale parameter indicates the locale to be used to convert to upper/lower case according to any locale-specific case mappings. If multiple locales are given in an array, the best available locale is used.

import camelCase from 'camelcase';

camelCase('lorem-ipsum', {locale: 'en-US'});
//=> 'loremIpsum'

camelCase('lorem-ipsum', {locale: 'tr-TR'});
//=> 'loremİpsum'

camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']});
//=> 'loremIpsum'

camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']});
//=> 'loremİpsum'

Setting locale: false ignores the platform locale and uses the Unicode Default Case Conversion algorithm:

import camelCase from 'camelcase';

// On a platform with 'tr-TR'

camelCase('lorem-ipsum');
//=> 'loremİpsum'

camelCase('lorem-ipsum', {locale: false});
//=> 'loremIpsum'

Related