@@ -4,9 +4,13 @@ const aggregator = (paths: any, object: any) => {
4
4
} , { } ) ;
5
5
} ;
6
6
7
- function assignInWith ( target : any , source : any , customizer : ( targetValue : any , sourceValue : any ) => any ) {
7
+ function assignInWith ( target : any , source : any , customizer ? : ( targetValue : any , sourceValue : any ) => any ) {
8
8
Object . entries ( source ) . forEach ( ( [ field , value ] ) => {
9
- target [ field ] = customizer ( target [ field ] , value ) ;
9
+ if ( customizer ) {
10
+ target [ field ] = customizer ( target [ field ] , value ) ;
11
+ } else {
12
+ target [ field ] = value ;
13
+ }
10
14
} ) ;
11
15
return target ;
12
16
}
@@ -78,7 +82,7 @@ export interface Schema {
78
82
* @param {Array } items Items to be forwarded to Actions
79
83
* @param { } constructed Created tranformed object of a given type
80
84
*/
81
- function transformValuesFromObject ( object : any , schema : Schema , items : any [ ] , constructed : any ) {
85
+ function transformValuesFromObject ( object : any , schema : Schema , items : any [ ] , objectToCompute : { } | any ) {
82
86
return Object . entries ( schema )
83
87
. map ( ( [ targetProperty , action ] ) => {
84
88
// iterate on every action of the schema
@@ -87,7 +91,7 @@ function transformValuesFromObject(object: any, schema: Schema, items: any[], co
87
91
return { [ targetProperty ] : get ( object , action ) } ;
88
92
} else if ( isFunction ( action ) ) {
89
93
// Action<Function>: Free Computin - a callback called with the current object and collection [ destination: (object) => {...} ]
90
- return { [ targetProperty ] : action . call ( undefined , object , items , constructed ) } ;
94
+ return { [ targetProperty ] : action . call ( undefined , object , items , objectToCompute ) } ;
91
95
} else if ( Array . isArray ( action ) ) {
92
96
// Action<Array>: Aggregator - string paths => : [ destination: ['source1', 'source2', 'source3'] ]
93
97
return { [ targetProperty ] : aggregator ( action , object ) } ;
@@ -101,7 +105,7 @@ function transformValuesFromObject(object: any, schema: Schema, items: any[], co
101
105
} else if ( isString ( action . path ) ) {
102
106
value = get ( object , action . path ) ;
103
107
}
104
- result = action . fn . call ( undefined , value , object , items , constructed ) ;
108
+ result = action . fn . call ( undefined , value , object , items , objectToCompute ) ;
105
109
} catch ( e ) {
106
110
e . message = `Unable to set target property [${ targetProperty } ].
107
111
\n An error occured when applying [${ action . fn . name } ] on property [${ action . path } ]
@@ -112,27 +116,45 @@ function transformValuesFromObject(object: any, schema: Schema, items: any[], co
112
116
return { [ targetProperty ] : result } ;
113
117
}
114
118
} )
115
- . reduce ( ( finalObject , keyValue ) => ( { ...finalObject , ...keyValue } ) , { } ) ;
119
+ . reduce ( ( finalObject , keyValue ) => {
120
+ const undefinedValueCheck = ( destination : any , source : any ) => {
121
+ // Take the Object class value property if the incoming property is undefined
122
+ if ( isUndefined ( source ) ) {
123
+ if ( ! isUndefined ( destination ) ) {
124
+ return destination ;
125
+ } else {
126
+ return ; // No Black Magic Fuckery here, if the source and the destination are undefined, we don't do anything
127
+ }
128
+ } else {
129
+ return source ;
130
+ }
131
+ } ;
132
+ return assignInWith ( finalObject , keyValue , undefinedValueCheck ) ;
133
+ } , objectToCompute ) ;
116
134
}
117
135
118
- const transformItems = ( schema : Schema , customizer : any , constructed : any ) => ( input : any ) => {
136
+ const transformItems = ( schema : Schema , type ? : any ) => ( input : any ) => {
119
137
if ( ! input ) {
120
138
return input ;
121
139
}
122
140
if ( Array . isArray ( input ) ) {
123
141
return input . map ( obj => {
124
- if ( customizer ) {
125
- return customizer ( transformValuesFromObject ( obj , schema , input , constructed ) ) ;
142
+ if ( type ) {
143
+ const classObject = new type ( ) ;
144
+ return transformValuesFromObject ( obj , schema , input , classObject ) ;
126
145
} else {
127
- return transformValuesFromObject ( obj , schema , input , null ) ;
146
+ const jsObject = { } ;
147
+ return transformValuesFromObject ( obj , schema , input , jsObject ) ;
128
148
}
129
149
} ) ;
130
150
} else {
131
151
const object = input ;
132
- if ( customizer ) {
133
- return customizer ( transformValuesFromObject ( object , schema , [ object ] , constructed ) ) ;
152
+ if ( type ) {
153
+ const classObject = new type ( ) ;
154
+ return transformValuesFromObject ( object , schema , [ object ] , classObject ) ;
134
155
} else {
135
- return transformValuesFromObject ( object , schema , [ object ] , null ) ;
156
+ const jsObject = { } ;
157
+ return transformValuesFromObject ( object , schema , [ object ] , jsObject ) ;
136
158
}
137
159
}
138
160
} ;
@@ -170,41 +192,20 @@ let Morphism: {
170
192
* const output = Morphism(schema, input);
171
193
*/
172
194
Morphism = ( schema : Schema , items ?: any , type ?: any ) : typeof type => {
173
- let constructed : typeof type = null ;
174
-
175
- if ( type ) {
176
- constructed = new type ( ) ;
177
- }
178
-
179
- const customizer = ( data : any ) => {
180
- const undefinedValueCheck = ( destination : any , source : any ) => {
181
- // Take the Object class value property if the incoming property is undefined
182
- if ( isUndefined ( source ) ) {
183
- if ( ! isUndefined ( destination ) ) {
184
- return destination ;
185
- } else {
186
- return ; // No Black Magic Fuckery here, if the source and the destination are undefined, we don't do anything
187
- }
188
- } else {
189
- return source ;
190
- }
191
- } ;
192
- return assignInWith ( constructed , data , undefinedValueCheck ) ;
193
- } ;
194
195
if ( items === undefined && type === undefined ) {
195
- return transformItems ( schema , null , null ) ;
196
+ return transformItems ( schema ) ;
196
197
} else if ( schema && items && type ) {
197
- return transformItems ( schema , customizer , constructed ) ( items ) ;
198
+ let finalSchema = getSchemaForType ( type , schema ) ;
199
+ return transformItems ( finalSchema , type ) ( items ) ;
198
200
} else if ( schema && items ) {
199
- return transformItems ( schema , null , null ) ( items ) ;
201
+ return transformItems ( schema ) ( items ) ;
200
202
} else if ( type && items ) {
201
203
let finalSchema = getSchemaForType ( type , schema ) ;
202
- return transformItems ( finalSchema , customizer , constructed ) ( items ) ;
204
+ return transformItems ( finalSchema , type ) ( items ) ;
203
205
} else if ( type ) {
204
206
let finalSchema = getSchemaForType ( type , schema ) ;
205
207
return ( futureInput : any ) => {
206
- constructed = new type ( ) ;
207
- return transformItems ( finalSchema , customizer , constructed ) ( futureInput ) ;
208
+ return transformItems ( finalSchema , type ) ( futureInput ) ;
208
209
} ;
209
210
}
210
211
} ;
0 commit comments