change-case vs camelcase vs camelcase-keys vs case-anything vs to-camel-case vs uppercamelcase
String Case Transformation
change-casecamelcasecamelcase-keyscase-anythingto-camel-caseuppercamelcaseSimilar Packages:

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).

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
change-case18,390,3912,40435.9 kB132 years agoMIT
camelcase069717.8 kB04 months agoMIT
camelcase-keys074816.6 kB0a month agoMIT
case-anything011133.1 kB10a year agoMIT
to-camel-case035-010 years agoMIT
uppercamelcase080-59 years agoMIT

Feature Comparison: change-case vs camelcase vs camelcase-keys vs case-anything vs to-camel-case vs uppercamelcase

Case Conversion

  • 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).

  • 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' }.

  • 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

  • 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.

  • 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.

  • 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

  • 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
    
  • 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' }
    
  • 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: change-case vs camelcase vs camelcase-keys vs case-anything vs to-camel-case vs uppercamelcase

  • 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.

  • 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.

  • 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 change-case

Change Case

Transform a string between camelCase, PascalCase, Capital Case, snake_case, kebab-case, CONSTANT_CASE and others.

Installation

npm install change-case --save

Usage

import * as changeCase from "change-case";

changeCase.camelCase("TEST_VALUE"); //=> "testValue"

Included case functions:

MethodResult
camelCasetwoWords
capitalCaseTwo Words
constantCaseTWO_WORDS
dotCasetwo.words
kebabCasetwo-words
noCasetwo words
pascalCaseTwoWords
pascalSnakeCaseTwo_Words
pathCasetwo/words
sentenceCaseTwo words
snakeCasetwo_words
trainCaseTwo-Words

All methods accept an options object as the second argument:

  • delimiter?: string The character to use between words. Default depends on method, e.g. _ in snake case.
  • locale?: string[] | string | false Lower/upper according to specified locale, defaults to host environment. Set to false to disable.
  • split?: (value: string) => string[] A function to define how the input is split into words. Defaults to split.
  • prefixCharacters?: string Retain at the beginning of the string. Defaults to "". Example: use "_" to keep the underscores in __typename.
  • suffixCharacters?: string Retain at the end of the string. Defaults to "". Example: use "_" to keep the underscore in type_.

By default, pascalCase and snakeCase separate ambiguous characters with _. For example, V1.2 would become V1_2 instead of V12. If you prefer them merged you can set mergeAmbiguousCharacters to true.

Split

Change case exports a split utility which can be used to build other case functions. It accepts a string and returns each "word" as an array. For example:

split("fooBar")
  .map((x) => x.toLowerCase())
  .join("_"); //=> "foo_bar"

Change Case Keys

import * as changeKeys from "change-case/keys";

changeKeys.camelCase({ TEST_KEY: true }); //=> { testKey: true }

Change case keys wraps around the core methods to transform object keys to any case.

API

  • input: any Any JavaScript value.
  • depth: number Specify the depth to transfer for case transformation. Defaults to 1.
  • options: object Same as base case library.

TypeScript and ESM

This package is a pure ESM package and ships with TypeScript definitions. It cannot be require'd or used with CommonJS module resolution in TypeScript.

License

MIT