transform-string-case

1.1.0 • Public • Published

transform-string-case

Transform strings between snake, camel, and upper camel case.


Build status Code Coverage Dependencies Maintainability BSD-3-Clause License Version

Code of Conduct All Contributors PRs Welcome

Watch on GitHub Star on GitHub Tweet

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!

Contributors

Trezy
Trezy

💻 📖 🤔 🚇 🚧 🔧 ⚠️

Package Sidebar

Install

npm i transform-string-case

Weekly Downloads

5

Version

1.1.0

License

BSD-3-Clause

Unpacked Size

26.5 kB

Total Files

15

Last publish

Collaborators

  • trezy