@totemish/case-transformer v1.3.0
Case Transformer
Totemish case transformer util provides an easy way for transforming strings from/to different cases. It provides fluent interface for writing super-understandable code, but you are free to write it in a shorter way if you feel like it.
Supported cases:
- camel =>
camelCase
- pascal =>
PascalCase
- snake =>
snake_case
- kebab =>
kebab-case
- colon † =>
colon:case
- arrow =>
arrow->case
- fatArrow =>
fatarrow=>case
- any other case you can imagine, thanks to
custom()
and your own transformers!
Marginalia
† I don't know if it is the right name (or at least it has a given name) but it certainly makes sense in many cases including denoting index of a subgroup, contraction, standing out of the crowd, etc. I can't say I'm happy with this name myself, so if you have ideas on how to name it, post an issue on GitHub. As the homonym for this one is way too... digestive.
Installation
npm i --save @totemish/case-transformer
Usage
Case transformer module is dependency-free and can be used both in NodeJS and the browser.
To use it in browser, use the minified version at ./index.umd.min.js
or the normal build (with sourcemaps) at ./index.umd.js
.
There is a range of applicable syntax, each of these will do.
Example
import { transform, CaseTransformer as CT } from '@totemish/case-transformer';
console.log(transform('hello-world').from.kebab().to.camel());
// helloWorld
console.log(transform('helloWorld').from.camel().to.colon());
// hello:world
/**
* You can totally omit the fluent interface and do it the short way
*/
console.log(transform('hello-world').kebab().snake(), transform('hello-world').kebab().snake());
// hello_world hello_world
/**
* You can also use internal 'transform' method yet it is deprecated as it breaks fluent workflow
* NOTE: Argument constructor takes priority over transform function argument if you provide both
*/
console.log(new CT('hello-world').transform().from.kebab().to.snake());
// hello_world
console.log(new CT('some-string').transform('another-string').from.kebab().to.snake());
// some_string
Custom transformers
It's super simple to add your own custom transformers:
import { transform } from '@totemish/case-transformer';
const myTransformer = x => x.map((item, index) => `${index + 1}. ${CS.toTitleCase(item)}`);
console.log(transform('milk.bread.butter.eggs').custom('.', myTransformer, '\n'));
// 1. Milk
// 2. Bread
// 3. Butter
// 4. Eggs