sindresorhus / camelcase-keys

Convert object keys to camel case
MIT License
691 stars 95 forks source link

Inconsistent behavior with Arrays #28

Open cinderblock opened 5 years ago

cinderblock commented 5 years ago
const camelcaseKeys = require('camelcase-keys');

At top level, array members are turned into array elements.

(a = [{test_one: 1}]).test_two = {}; a;
// [ { test_one: 1 }, test_two: {} ]
camelcaseKeys(a);
// [ { testOne: 1 }, {} ]

At lower levels, array members are lost.

(o = {deep: [{test_one: 1}]}).deep.test_two = 1; o;
// { deep: [ { test_one: 1 }, test_two: 1 ] }
camelcaseKeys(o, {deep: true});
// { deep: [ { testOne: 1 } ] } }

I realize this is bad practice. However, that this library mangles them and, in particular, inconsistently is I think problematic.

Proposed solution

map-obj already has the ability to handle Arrays. Simply remove the extra function at module.exports and export camelCaseConvert directly.

Side note

Doesn't map-obj already have a caching mechanism? It seems a little redundant to me to have a caching layer in this package. Maybe I'm missing something?

sindresorhus commented 5 years ago

map-obj already has the ability to handle Arrays.

Only nested arrays. Not a top-level array.

Doesn't map-obj already have a caching mechanism?

No

sindresorhus commented 5 years ago

Your use-case is very obscure, but I'm willing to merge a good PR from anyone that includes tests.

cinderblock commented 5 years ago

map-obj already has the ability to handle Arrays.

Only nested arrays. Not a top-level array.

I'm not following what you're saying. It seems to support them to me:

const mapObj = require('map-obj');
console.log('mapped:', mapObj(['test']));
// mapped: [ 'test' ]

Also: https://github.com/sindresorhus/camelcase-keys/blob/master/test.js#L29-L35

Doesn't map-obj already have a caching mechanism?

No

Forgive my persistence, but it sure seems to https://github.com/sindresorhus/map-obj/blob/master/index.js#L17-L23 Am I missing something?

On that subject, any thoughts on https://github.com/sindresorhus/map-obj/pull/15