Commit a25409c 1 parent 1d662e4 commit a25409c Copy full SHA for a25409c
File tree 2 files changed +19
-2
lines changed
2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change 1
1
/* eslint-disable no-param-reassign */
2
2
3
+ var assert = require ( './assert' ) ;
3
4
var objectAssign = require ( './object-assign' ) ;
4
5
5
6
function pick ( object , keys ) {
@@ -75,21 +76,30 @@ function snakeToCamel(str) {
75
76
}
76
77
77
78
function toSnakeCase ( object , exceptions ) {
79
+ if ( typeof ( object ) !== 'object' || assert . isArray ( object ) || ! object === null ) {
80
+ return object ;
81
+ }
82
+
78
83
exceptions = exceptions || [ ] ;
79
84
80
85
return Object . keys ( object ) . reduce ( function ( p , key ) {
81
86
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 ] ) ;
83
88
return p ;
84
89
} , { } ) ;
85
90
}
86
91
87
92
function toCamelCase ( object , exceptions ) {
93
+
94
+ if ( typeof ( object ) !== 'object' || assert . isArray ( object ) || ! object === null ) {
95
+ return object ;
96
+ }
97
+
88
98
exceptions = exceptions || [ ] ;
89
99
90
100
return Object . keys ( object ) . reduce ( function ( p , key ) {
91
101
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 ] ) ;
93
103
return p ;
94
104
} , { } ) ;
95
105
}
Original file line number Diff line number Diff line change @@ -336,6 +336,9 @@ describe('helpers', function () {
336
336
}
337
337
} ) ;
338
338
339
+ expect ( newObject . array_att ) . to . be . an ( 'array' ) ;
340
+ expect ( newObject . some_obj . inner_array_att ) . to . be . an ( 'array' ) ;
341
+
339
342
expect ( newObject ) . to . eql ( {
340
343
attr_name_1 : 'attribute_1' ,
341
344
attr_name_22 : 'attribute_2' ,
@@ -373,6 +376,7 @@ describe('helpers', function () {
373
376
} ) ;
374
377
375
378
describe ( 'toCamelCase' , function ( ) {
379
+
376
380
it ( 'should change the casing to all the attributes' , function ( ) {
377
381
var object = {
378
382
attr_name_1 : 'attribute_1' ,
@@ -400,6 +404,9 @@ describe('helpers', function () {
400
404
}
401
405
} ) ;
402
406
407
+ expect ( newObject . arrAtt ) . to . be . an ( 'array' ) ;
408
+ expect ( newObject . someObj . innerArrayAtt ) . to . be . an ( 'array' ) ;
409
+
403
410
expect ( newObject ) . to . eql ( {
404
411
attrName1 : 'attribute_1' ,
405
412
attrName22 : 'attribute_2' ,
You can’t perform that action at this time.
0 commit comments