1
1
import camelCase from 'lodash/camelCase' ;
2
2
import isArray from 'lodash/isArray' ;
3
3
import isNull from 'lodash/isNull' ;
4
- import join from 'lodash/join' ;
5
4
import keys from 'lodash/keys' ;
6
5
import merge from 'lodash/merge' ;
7
6
@@ -13,7 +12,37 @@ function wrap(json) {
13
12
return [ json ] ;
14
13
}
15
14
16
- function extract ( json , { camelizeKeys } ) {
15
+ function extractRelationships ( relationships , { camelizeKeys } ) {
16
+ const ret = { } ;
17
+ keys ( relationships ) . forEach ( ( key ) => {
18
+ const relationship = relationships [ key ] ;
19
+ const name = camelizeKeys ? camelCase ( key ) : key ;
20
+ ret [ name ] = { } ;
21
+
22
+ if ( typeof relationship . data !== 'undefined' ) {
23
+ if ( isArray ( relationship . data ) ) {
24
+ ret [ name ] . data = relationship . data . map ( e => ( {
25
+ id : e . id ,
26
+ type : camelizeKeys ? camelCase ( e . type ) : e . type ,
27
+ } ) ) ;
28
+ } else if ( ! isNull ( relationship . data ) ) {
29
+ ret [ name ] . data = {
30
+ id : relationship . data . id ,
31
+ type : camelizeKeys ? camelCase ( relationship . data . type ) : relationship . data . type ,
32
+ } ;
33
+ } else {
34
+ ret [ name ] . data = relationship . data ;
35
+ }
36
+ }
37
+
38
+ if ( relationship . links ) {
39
+ ret [ name ] . links = relationship . links ;
40
+ }
41
+ } ) ;
42
+ return ret ;
43
+ }
44
+
45
+ function extractEntities ( json , { camelizeKeys } ) {
17
46
const ret = { } ;
18
47
19
48
wrap ( json ) . forEach ( ( elem ) => {
@@ -35,33 +64,8 @@ function extract(json, { camelizeKeys }) {
35
64
}
36
65
37
66
if ( elem . relationships ) {
38
- wrap ( elem . relationships ) . forEach ( ( relationship ) => {
39
- const mp = { } ;
40
-
41
- wrap ( relationship ) . forEach ( ( object ) => {
42
- keys ( object ) . forEach ( ( key ) => {
43
- mp [ camelizeKeys ? camelCase ( key ) : key ] = { } ;
44
-
45
- if ( typeof object [ key ] . data !== 'undefined' && ! isNull ( object [ key ] . data ) ) {
46
- if ( wrap ( object [ key ] . data ) . length > 0 ) {
47
- const ids = wrap ( object [ key ] . data ) . map ( el => el . id ) ;
48
- const relType = wrap ( object [ key ] . data ) [ 0 ] . type ;
49
-
50
- mp [ camelizeKeys ? camelCase ( key ) : key ] = {
51
- id : ids . length === 1 ? ids [ 0 ] . toString ( ) : join ( ids , ',' ) ,
52
- type : camelizeKeys ? camelCase ( relType ) : relType ,
53
- } ;
54
- }
55
- }
56
-
57
- if ( object [ key ] . links ) {
58
- mp [ camelizeKeys ? camelCase ( key ) : key ] . links = object [ key ] . links ;
59
- }
60
- } ) ;
61
- } ) ;
62
-
63
- ret [ type ] [ elem . id ] . relationships = mp ;
64
- } ) ;
67
+ ret [ type ] [ elem . id ] . relationships =
68
+ extractRelationships ( elem . relationships , { camelizeKeys } ) ;
65
69
}
66
70
} ) ;
67
71
@@ -99,21 +103,8 @@ function extractMetaData(json, endpoint, { camelizeKeys, filterEndpoint }) {
99
103
const pObject = { id : object . id , type : camelizeKeys ? camelCase ( object . type ) : object . type } ;
100
104
101
105
if ( object . relationships ) {
102
- keys ( object . relationships ) . forEach ( ( key ) => {
103
- pObject . relationships = pObject . relationships || { } ;
104
-
105
- if ( typeof object . relationships [ key ] . data !== 'undefined' && ! isNull ( object . relationships [ key ] . data ) ) {
106
- if ( wrap ( object . relationships [ key ] . data ) . length > 0 ) {
107
- const ids = wrap ( object . relationships [ key ] . data ) . map ( elem => elem . id ) ;
108
- const type = wrap ( object . relationships [ key ] . data ) [ 0 ] . type ;
109
-
110
- pObject . relationships [ camelizeKeys ? camelCase ( key ) : key ] = {
111
- type : camelizeKeys ? camelCase ( type ) : type ,
112
- id : join ( ids , ',' ) ,
113
- } ;
114
- }
115
- }
116
- } ) ;
106
+ pObject . relationships =
107
+ extractRelationships ( object . relationships , { camelizeKeys } ) ;
117
108
}
118
109
119
110
meta . push ( pObject ) ;
@@ -148,11 +139,11 @@ export default function normalize(json, opts = {}) {
148
139
}
149
140
150
141
if ( json . data ) {
151
- merge ( ret , extract ( json . data , { camelizeKeys } ) ) ;
142
+ merge ( ret , extractEntities ( json . data , { camelizeKeys } ) ) ;
152
143
}
153
144
154
145
if ( json . included ) {
155
- merge ( ret , extract ( json . included , { camelizeKeys } ) ) ;
146
+ merge ( ret , extractEntities ( json . included , { camelizeKeys } ) ) ;
156
147
}
157
148
158
149
if ( endpoint ) {
0 commit comments