npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bitchcraft/ocake

v1.0.2

Published

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

Downloads

7

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