@@ -8,7 +8,7 @@ function defaultAssignEntity(normalized, key, entity) {
8
8
normalized [ key ] = entity ;
9
9
}
10
10
11
- function visitObject ( obj , schema , bag , options ) {
11
+ function visitObject ( obj , schema , bag , options , collectionKey ) {
12
12
const { assignEntity = defaultAssignEntity } = options ;
13
13
14
14
const defaults = schema && schema . getDefaults && schema . getDefaults ( ) ;
@@ -17,7 +17,7 @@ function visitObject(obj, schema, bag, options) {
17
17
for ( let key in obj ) {
18
18
if ( obj . hasOwnProperty ( key ) ) {
19
19
const resolvedSchema = typeof schema [ key ] === 'function' ? schema [ key ] . call ( null , obj ) : schema [ key ] ;
20
- const entity = visit ( obj [ key ] , resolvedSchema , bag , options ) ;
20
+ const entity = visit ( obj [ key ] , resolvedSchema , bag , options , collectionKey ) ;
21
21
assignEntity . call ( null , normalized , key , entity , obj , schema ) ;
22
22
if ( schemaAssignEntity ) {
23
23
schemaAssignEntity . call ( null , normalized , key , entity , obj , schema ) ;
@@ -28,13 +28,13 @@ function visitObject(obj, schema, bag, options) {
28
28
}
29
29
30
30
function defaultMapper ( iterableSchema , itemSchema , bag , options ) {
31
- return ( obj ) => visit ( obj , itemSchema , bag , options ) ;
31
+ return ( obj , key ) => visit ( obj , itemSchema , bag , options , key ) ;
32
32
}
33
33
34
34
function polymorphicMapper ( iterableSchema , itemSchema , bag , options ) {
35
- return ( obj ) => {
35
+ return ( obj , key ) => {
36
36
const schemaKey = iterableSchema . getSchemaKey ( obj ) ;
37
- const result = visit ( obj , itemSchema [ schemaKey ] , bag , options ) ;
37
+ const result = visit ( obj , itemSchema [ schemaKey ] , bag , options , key ) ;
38
38
return { id : result , schema : schemaKey } ;
39
39
} ;
40
40
}
@@ -47,7 +47,7 @@ function visitIterable(obj, iterableSchema, bag, options) {
47
47
return obj . map ( curriedItemMapper ) ;
48
48
} else {
49
49
return Object . keys ( obj ) . reduce ( function ( objMap , key ) {
50
- objMap [ key ] = curriedItemMapper ( obj [ key ] ) ;
50
+ objMap [ key ] = curriedItemMapper ( obj [ key ] , key ) ;
51
51
return objMap ;
52
52
} , { } ) ;
53
53
}
@@ -76,11 +76,11 @@ function defaultMergeIntoEntity(entityA, entityB, entityKey) {
76
76
}
77
77
}
78
78
79
- function visitEntity ( entity , entitySchema , bag , options ) {
79
+ function visitEntity ( entity , entitySchema , bag , options , collectionKey ) {
80
80
const { mergeIntoEntity = defaultMergeIntoEntity } = options ;
81
81
82
82
const entityKey = entitySchema . getKey ( ) ;
83
- const id = entitySchema . getId ( entity ) ;
83
+ const id = entitySchema . getId ( entity , collectionKey ) ;
84
84
85
85
if ( ! bag . hasOwnProperty ( entityKey ) ) {
86
86
bag [ entityKey ] = { } ;
@@ -91,28 +91,35 @@ function visitEntity(entity, entitySchema, bag, options) {
91
91
}
92
92
93
93
let stored = bag [ entityKey ] [ id ] ;
94
- let normalized = visitObject ( entity , entitySchema , bag , options ) ;
94
+ let normalized = visitObject ( entity , entitySchema , bag , options , collectionKey ) ;
95
95
mergeIntoEntity ( stored , normalized , entityKey ) ;
96
96
97
97
return id ;
98
98
}
99
99
100
- function visit ( obj , schema , bag , options ) {
100
+ function visit ( obj , schema , bag , options , collectionKey ) {
101
101
if ( ! isObject ( obj ) || ! isObject ( schema ) ) {
102
102
return obj ;
103
103
}
104
104
105
105
if ( schema instanceof EntitySchema ) {
106
- return visitEntity ( obj , schema , bag , options ) ;
106
+ return visitEntity ( obj , schema , bag , options , collectionKey ) ;
107
107
} else if ( schema instanceof IterableSchema ) {
108
108
return visitIterable ( obj , schema , bag , options ) ;
109
109
} else if ( schema instanceof UnionSchema ) {
110
110
return visitUnion ( obj , schema , bag , options ) ;
111
111
} else {
112
- return visitObject ( obj , schema , bag , options ) ;
112
+ return visitObject ( obj , schema , bag , options , collectionKey ) ;
113
113
}
114
114
}
115
115
116
+ function normalizeResult ( result ) {
117
+ if ( isObject ( result ) && isEqual ( Object . keys ( result ) , Object . keys ( result ) . map ( key => result [ key ] ) ) ) {
118
+ return Object . keys ( result ) ;
119
+ }
120
+ return result ;
121
+ }
122
+
116
123
export function arrayOf ( schema , options ) {
117
124
return new IterableSchema ( schema , options ) ;
118
125
}
@@ -139,8 +146,9 @@ export function normalize(obj, schema, options = {}) {
139
146
let bag = { } ;
140
147
let result = visit ( obj , schema , bag , options ) ;
141
148
149
+
142
150
return {
143
151
entities : bag ,
144
- result
152
+ result : normalizeResult ( result )
145
153
} ;
146
154
}
0 commit comments