Skip to content

Commit 6e64d05

Browse files
committed
fix: regression by moving reference to the object near to the lower transformer.
Remove useless code
1 parent 1190474 commit 6e64d05

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

src/morphism.ts

+41-40
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ const aggregator = (paths: any, object: any) => {
44
}, {});
55
};
66

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) {
88
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+
}
1014
});
1115
return target;
1216
}
@@ -78,7 +82,7 @@ export interface Schema {
7882
* @param {Array} items Items to be forwarded to Actions
7983
* @param {} constructed Created tranformed object of a given type
8084
*/
81-
function transformValuesFromObject(object: any, schema: Schema, items: any[], constructed: any) {
85+
function transformValuesFromObject(object: any, schema: Schema, items: any[], objectToCompute: {} | any) {
8286
return Object.entries(schema)
8387
.map(([targetProperty, action]) => {
8488
// iterate on every action of the schema
@@ -87,7 +91,7 @@ function transformValuesFromObject(object: any, schema: Schema, items: any[], co
8791
return { [targetProperty]: get(object, action) };
8892
} else if (isFunction(action)) {
8993
// 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) };
9195
} else if (Array.isArray(action)) {
9296
// Action<Array>: Aggregator - string paths => : [ destination: ['source1', 'source2', 'source3'] ]
9397
return { [targetProperty]: aggregator(action, object) };
@@ -101,7 +105,7 @@ function transformValuesFromObject(object: any, schema: Schema, items: any[], co
101105
} else if (isString(action.path)) {
102106
value = get(object, action.path);
103107
}
104-
result = action.fn.call(undefined, value, object, items, constructed);
108+
result = action.fn.call(undefined, value, object, items, objectToCompute);
105109
} catch (e) {
106110
e.message = `Unable to set target property [${targetProperty}].
107111
\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
112116
return { [targetProperty]: result };
113117
}
114118
})
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);
116134
}
117135

118-
const transformItems = (schema: Schema, customizer: any, constructed: any) => (input: any) => {
136+
const transformItems = (schema: Schema, type?: any) => (input: any) => {
119137
if (!input) {
120138
return input;
121139
}
122140
if (Array.isArray(input)) {
123141
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);
126145
} else {
127-
return transformValuesFromObject(obj, schema, input, null);
146+
const jsObject = {};
147+
return transformValuesFromObject(obj, schema, input, jsObject);
128148
}
129149
});
130150
} else {
131151
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);
134155
} else {
135-
return transformValuesFromObject(object, schema, [object], null);
156+
const jsObject = {};
157+
return transformValuesFromObject(object, schema, [object], jsObject);
136158
}
137159
}
138160
};
@@ -170,41 +192,20 @@ let Morphism: {
170192
* const output = Morphism(schema, input);
171193
*/
172194
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-
};
194195
if (items === undefined && type === undefined) {
195-
return transformItems(schema, null, null);
196+
return transformItems(schema);
196197
} else if (schema && items && type) {
197-
return transformItems(schema, customizer, constructed)(items);
198+
let finalSchema = getSchemaForType(type, schema);
199+
return transformItems(finalSchema, type)(items);
198200
} else if (schema && items) {
199-
return transformItems(schema, null, null)(items);
201+
return transformItems(schema)(items);
200202
} else if (type && items) {
201203
let finalSchema = getSchemaForType(type, schema);
202-
return transformItems(finalSchema, customizer, constructed)(items);
204+
return transformItems(finalSchema, type)(items);
203205
} else if (type) {
204206
let finalSchema = getSchemaForType(type, schema);
205207
return (futureInput: any) => {
206-
constructed = new type();
207-
return transformItems(finalSchema, customizer, constructed)(futureInput);
208+
return transformItems(finalSchema, type)(futureInput);
208209
};
209210
}
210211
};

0 commit comments

Comments
 (0)