@bitchcraft/ocake

Object keys case converter (and collection of case string converters)

Stats

stars 🌟issues ⚠️updated πŸ› created 🐣size πŸ‹οΈβ€β™€οΈ
345Sep 5, 2020Feb 28, 2018Minified + gzip package size for @bitchcraft/ocake in KB

Readme

@bitchcraft/ocake

  ,.·——–——·.ΒΈ
,Β°   ≑OCAKE≑  Β°,
|   ,––––––––––.\
 \ΒΈ| (OMNOMNOM)'/
   ``----------Β΄

Convert the case of keys in an object (e. g. to camelCase), or simply convert the case of a string to another case.

{ camelCased: 'some value' } => { snake_cased: 'some value' }

Installation

$ yarn add @bitchcraft/ocake
$ npm install -P @bitchcraft/ocake

Usage

convertKeys

convertKeys(haystack: Object, replacer: (key: string) => string): Object

Creates an identical object where the keys have been converted to the desired case.

  • haystack can be any plain object including arrays
  • replacer function which takes a string and returns a string: (key: string) => string. You can use one of the supplied StringConverters.

ES6 example

import { convertKeys, StringConverters } from '@bitchcraft/ocake';

const payload = convertKeys({
    userName: 'Sam Axe',
    userId: 5462,
    clientId: 17,
}, StringConverters.toKebabCase);

const requestBody = JSON.stringify(payload);

ES5 example

var convertKeys = require('@bitchcraft/ocake').convertKeys;
var toKebabCase = require('@bitchcraft/ocake').StringConverters.toKebabCase;

var payload = convertKeys({
    userName: 'Sam Axe',
    userId: 5462,
    clientId: 17
}, StringConverters.toKebabCase);

const requestBody = JSON.stringify(payload);

StringConverters

Convert the case in a string to another case, e. g.

'sentence case' => 'sentenceCase'

All string converters provided by OCake currently use no-case, which converts using a common intermediary. There are no source-target-pair specific replacers. Meaning that every conversion in fact first converts to sentence case before converting to the target case, e. g. when converting to snake case: 'unknownCase' -> 'unknown case' -> 'unknown_case'. This has to be kept in mind when designing your app for roundtrip conversion. In that case you either have to be aware of the limitations or provide your own source-target specific replacers.

Known limitations are:

  • Tall-man-case does not work roundtrip, because of ambiguity with camel case. ('someTALLMANCASEkey' -> 'some tallmancase ekey' -> 'someTallmancaseEkey' -> 'some tallmancase ekey' -> 'someTALLMANCASekey' -> …)
  • Odd camel case is not preserved on roundtrip ('XMLHttpRequest' -> 'xml http request' -> 'XmlHttpRequest')
  • When converting keys that contain numbers, some cases cannot properly store subtle differences, e. g. ('snake_case_1' -> 'snakeCase1' -> 'snake_case1')

List of provided StringReplacers

Replacer Description
toCamelCase() alias for toLowerCamelCase
toLowerCamelCase() e. g. 'theCakeIsFake'
toUpperCamelCase() e. g. 'TheCakeIsFake'
toKebabCase() e. g. 'the-cake-is-fake'
toAngryKebabCase() e. g. 'THE-CAKE-IS-FAKE'
toSnakeCase() e. g. 'the_cake_is_fake'
toAngrySnakeCase() e. g. 'THE_CAKE_IS_FAKE'
toSentenceCase() e. g. the cake is fake
toTitleCase() e. g. 'The Cake Is Fake'
toShoutCase() e. g. 'THE CAKE IS FAKE'
toTallManCase() e. g. 'theCAKEisFAKE'

ES2015+ imports with flow types

// babel: env, stage-0, flow
import { convertKeys, StringConverters } from '@bitchcraft/ocake/src/Ocake';
import convertKeys from '@bitchcraft/ocake/src/convertKeys';
import StringConverters, { toCamelCase, … } from '@bitchcraft/ocake/src/StringConverters';

Bundle size

Gzipped size (non-minified) is around 19KB, with 47% of that taken up by core-js. You can check out the bundle analytics for the non-minified bundle.

Help and feedback

Please file issues in Github

Contribute

We are open for PRs. Please respect to the linting rules.

License

Keyconst is free software und the BSD-3-Clause (see LICENSE.md).

Contributors

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.