Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

lodash-humps

cape-io20.5kISC3.1.6

Recursive camelCase of object keys and arrays of objects

utils, camel, case, keys, underscore, converter, strings, lodash, objects

readme

lodash-humps v3.1.2

Converting object keys to camelCase. Works on deeply nested objects/arrays. Handy for converting underscore keys to camelCase. Using lodash keeps the code small and light at around 10 lines.

Install

$ npm i --save lodash-humps

Usage

Converting object keys

Remove any hyphens, underscores, whitespace characters, and uppercases the first character that follows. Returns a new object. See _.camelCase() https://lodash.com/docs#camelCase and https://en.wikipedia.org/wiki/CamelCase.

import humps from 'lodash-humps'

const object = { attr_one: 'foo', attr_two: 'bar', attr_three: { attr_one: 'foo' } }
humps(object) // { attrOne: 'foo', attrTwo: 'bar', attrThree: { attrOne: 'foo' } }

Arrays of objects are also converted:

const array = [{ attr_one: 'foo' }, { attr_one: 'bar' }]
humps(array) // [{ attrOne: 'foo' }, { attrOne: 'bar' }]

Custom key converter

What if you want to convert it back?!? See test/createHumps.spec.js for an example. Open an issue if you want a proper export.

import createHumps from 'humps/lib/createHumps'
import { snakeCase } from 'lodash'

const snakes = createHumps(snakeCase)
const object = { attrOne: 'foo', attrTwo: 'bar', attrThree: { attrOne: 'foo' } }
snakes(object) // { attr_one: 'foo', attr_two: 'bar', attr_three: { attr_one: 'foo' } }

Version 3 Changes

NOTE: Version 3.x will only work with objects created by the Object constructor. You may need to do something like const result = humps({ ...SomeOtherClass }) to get humps to process your stuff. Function properties are now kept and not converted to {}. Some might say this is a bug and others might call it a feature. Full version bump so you can have your pick.

Internally switched from using _.isObject to _.isPlainObject before converting the keys and children objects.

Prior Art

https://www.npmjs.com/package/humps

changelog

[3.1.2]

2017-07-13

Documentation fixes. Thanks Tim!

[3.1.0]

2017-06-20

Added createHumps() for conversion back or other custom key conversion if desired.

[3.0.0]

2017-06-20

NOTE: Will only work with objects created by the Object constructor. You will need to do something like const result = humps({ ...SomeOtherClass }) to get it process.

Switch from using _.isObject to _.isPlainObject before converting the keys and children objects. Switched to _.isObjectLike within the getVal() function. With version 2.0 function properties were converted to plain objects.

[2.0.0]

2016-07-28

Drop support for passing a string. Use _.camelCase() instead.

[1.1.1]

2016-07-19

Do not camelCase all values, only object values!

[1.1.0]

2016-07-19

Yep, I was a little premature on the 1.0 release. No longer need require('lodash-humps').default. See issue #2. Thanks @jdalton for making the build process so much better.

[1.0.0]

2016-07-19

Initial Release