transform-string-case
v1.1.0
Published
Transform strings between snake, camel, and upper camel case.
Downloads
23
Maintainers
Readme
Use Case
transform-string-case
is a tiny, versatile library that can convert your strings between several different cases. My original use case was this: I was building a JavaScript project that needed to interact with both the [Firebase][firebase] and the [Twilio][twilio] APIs. The issue is that my lettercase was different everywhere. Since I was using JavaScript, my variables were all camel cased (thisIsACamelCasedVariable
). Firebase, however, sent all their JSON keys in snake case (this_is_a_snake_cased_variable
), while Twilio sent all of their JSON keys in upper camel case (ThisIsAnUpperCamelCasedVariable
).
To make matters worse, my ESlint config enforced camel casing and I didn't want to change it! Instead, I wrote a couple of tools to solve the issue: convert-object-keys
and transform-string-case
.
Usage
To use transform-string-case
, you need to pass it a string, what case the string is starting as, and what case the string is being transformed to:
import { transformStringCase } from 'transform-string-case'
transformStringCase('camelCaseString', 'camel', 'snake') // 'camel_case_string'
transformStringCase('snake_case_string', 'snake', 'upperCamel') // 'SnakeCaseString'
transformStringCase('UpperCamelCaseString', 'upperCamel', 'snake') // 'upper_camel_case_string'
Exports
transform-string-case
has a default export, as well as a couple of named helper exports (capitalizeFirstCharacter
and lowercaseFirstCharacter
). these helpers are used internally by the transformStringCase
, but they are exposed for convenience.
import transformStringCase from 'transform-string-case'
// or
import {
capitalizeFirstCharacter,
lowercaseFirstCharacter,
transformFirstCharacter,
transformStringCase,
} from 'transform-string-case'
capitalizeFirstCharacter('hello world!') // 'Hello world!'
lowercaseFirstCharacter('Hello World!') // 'hello World!'
transformFirstCharacter('Hello World!', (firstCharacter) => {
return firstCharacter = '😁'
}) // '😁ello World!'
Contributing
If you want to contribute, make sure to check out our contributing guide!