1- /**
2- * JSON Rest parsing
3- */
4-
5- import mapObject from "map-obj" ;
61import { parse } from "circ-json" ;
72
83import { toSnakeCase } from "./utils" ;
@@ -25,23 +20,46 @@ export const decamelizeKeys = (input: any) => {
2520 return input ;
2621 }
2722
28- const makeMapper = ( parentPath ?: string ) => ( key : string , value : any ) => {
29- key = cachedDecamelize ( key ) ;
23+ return transform ( input ) ;
3024
31- if ( isObject ( value ) ) {
32- const path = parentPath === undefined ? key : `${ parentPath } .${ key } ` ;
25+ function transform ( value : any , parentPath ?: string ) : any {
26+ if ( ! isObject ( value ) ) {
27+ return value ;
28+ }
3329
34- // @ts -ignore
35- value = mapObject ( value , makeMapper ( path ) ) ;
36- } else {
37- value = cachedValueParser ( key , parentPath , value ) ;
30+ if ( Array . isArray ( value ) ) {
31+ const length = value . length ;
32+ const result = new Array ( length ) ;
33+
34+ for ( let i = 0 ; i < length ; i += 1 ) {
35+ const item = value [ i ] ;
36+ result [ i ] = isObject ( item ) ? transformObject ( item , parentPath ) : item ;
37+ }
38+
39+ return result ;
3840 }
3941
40- return [ key , value ] ;
41- } ;
42+ return transformObject ( value , parentPath ) ;
43+ }
4244
43- // @ts -ignore
44- return mapObject ( input , makeMapper ( ) ) ;
45+ function transformObject ( obj : Record < string , any > , parentPath ?: string ) {
46+ const output : Record < string , any > = { } ;
47+
48+ for ( const key of Object . keys ( obj ) ) {
49+ const rawValue = obj [ key ] ;
50+ const newKey = cachedDecamelize ( key ) ;
51+
52+ if ( isObject ( rawValue ) ) {
53+ const nextParentPath =
54+ parentPath === undefined ? newKey : `${ parentPath } .${ newKey } ` ;
55+ output [ newKey ] = transform ( rawValue , nextParentPath ) ;
56+ } else {
57+ output [ newKey ] = cachedValueParser ( newKey , parentPath , rawValue ) ;
58+ }
59+ }
60+
61+ return output ;
62+ }
4563} ;
4664
4765const cachedDecamelize = ( key : string ) => {
0 commit comments