Skip to content

Commit a25409c

Browse files
committed
Casing should ignore non objects
1 parent 1d662e4 commit a25409c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/helper/object.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-param-reassign */
22

3+
var assert = require('./assert');
34
var objectAssign = require('./object-assign');
45

56
function pick(object, keys) {
@@ -75,21 +76,30 @@ function snakeToCamel(str) {
7576
}
7677

7778
function toSnakeCase(object, exceptions) {
79+
if (typeof(object) !== 'object' || assert.isArray(object) || !object === null) {
80+
return object;
81+
}
82+
7883
exceptions = exceptions || [];
7984

8085
return Object.keys(object).reduce(function (p, key) {
8186
var newKey = exceptions.indexOf(key) === -1 ? camelToSnake(key) : key;
82-
p[newKey] = typeof(object[key]) === 'object' ? toSnakeCase(object[key]) : object[key];
87+
p[newKey] = toSnakeCase(object[key]);
8388
return p;
8489
}, {});
8590
}
8691

8792
function toCamelCase(object, exceptions) {
93+
94+
if (typeof(object) !== 'object' || assert.isArray(object) || !object === null) {
95+
return object;
96+
}
97+
8898
exceptions = exceptions || [];
8999

90100
return Object.keys(object).reduce(function (p, key) {
91101
var newKey = exceptions.indexOf(key) === -1 ? snakeToCamel(key) : key;
92-
p[newKey] = typeof(object[key]) === 'object' ? toCamelCase(object[key]) : object[key];
102+
p[newKey] = toCamelCase(object[key]);
93103
return p;
94104
}, {});
95105
}

test/helper/object.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ describe('helpers', function () {
336336
}
337337
});
338338

339+
expect(newObject.array_att).to.be.an('array');
340+
expect(newObject.some_obj.inner_array_att).to.be.an('array');
341+
339342
expect(newObject).to.eql({
340343
attr_name_1: 'attribute_1',
341344
attr_name_22: 'attribute_2',
@@ -373,6 +376,7 @@ describe('helpers', function () {
373376
});
374377

375378
describe('toCamelCase', function () {
379+
376380
it('should change the casing to all the attributes', function () {
377381
var object = {
378382
attr_name_1: 'attribute_1',
@@ -400,6 +404,9 @@ describe('helpers', function () {
400404
}
401405
});
402406

407+
expect(newObject.arrAtt).to.be.an('array');
408+
expect(newObject.someObj.innerArrayAtt).to.be.an('array');
409+
403410
expect(newObject).to.eql({
404411
attrName1: 'attribute_1',
405412
attrName22: 'attribute_2',

0 commit comments

Comments
 (0)