What is change-case?
The change-case package is a utility library that provides various functions to transform strings between different cases, such as camel case, snake case, kebab case, and more. It is useful for formatting strings in different coding conventions or textual formats.
What are change-case's main functionalities?
camelCase
Converts a string to camel case.
const { camelCase } = require('change-case');
console.log(camelCase('test string')); // 'testString'
snakeCase
Converts a string to snake case.
const { snakeCase } = require('change-case');
console.log(snakeCase('test string')); // 'test_string'
kebabCase
Converts a string to kebab case.
const { kebabCase } = require('change-case');
console.log(kebabCase('test string')); // 'test-string'
titleCase
Converts a string to title case.
const { titleCase } = require('change-case');
console.log(titleCase('test string')); // 'Test String'
constantCase
Converts a string to constant case.
const { constantCase } = require('change-case');
console.log(constantCase('test string')); // 'TEST_STRING'
Other packages similar to change-case
lodash
Lodash is a comprehensive utility library that includes methods for string case transformation, such as _.camelCase, _.snakeCase, and _.kebabCase. It offers a wider range of utilities beyond string case conversion, making it more versatile but also larger in size compared to change-case.
case
The 'case' package is another string case conversion library that supports various case transformations. It is similar to change-case but has a different API and may have different case conversion implementations.
string-case
String-case is a library that provides case conversion functions similar to change-case. It may offer a different set of features or API design, which could be preferred by some developers over 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
These case change functions are included:
Method | Result |
---|
camelCase | twoWords |
capitalCase | Two Words |
constantCase | TWO_WORDS |
dotCase | two.words |
trainCase | Two-Words |
kebabCase | two-words |
noCase | two words |
pascalCase | TwoWords |
pathCase | two/words |
sentenceCase | Two words |
snakeCase | two_words |
All core methods accept options
as the second argument.
Options
locale?: string[] | string | false
- lower/upper according to specified locale, defaults to host environment. Set to false
to disable.separateNumbers?: boolean
Splits foo123
into foo 123
instead of keeping them together. Defaults to true
.
Split
Change case also exports a split
function which can be used to build your own case formatting methods. It accepts a string and returns each "word" as an array. For example:
split("fooBar")
.map((x) => x.toLowerCase())
.join("_");
Change Case Keys
import * as changeKeys from "change-case/keys";
changeKeys.camelCase({ TEST_KEY: true });
Keys is a wrapper around all case methods to support transforming objects to any case.
API
- input: unknown Any JavaScript value.
- depth: number Specify the depth to transfer for case transformation. Defaults to
1
. - options: object Same as base case library.
License
MIT