-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathObjectReader.js
More file actions
37 lines (30 loc) · 968 Bytes
/
ObjectReader.js
File metadata and controls
37 lines (30 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { isObject } from '../common';
export default function ObjectReader(dataTemplate, isNullable, defaultValue) {
this.dataTemplate = dataTemplate;
this.isNullable = isNullable;
this.defaultValue = defaultValue;
};
ObjectReader.prototype.read = function (target, source, path, context) {
var result = null,
isTargetObject = isObject(target),
property,
propertyDataTemplate;
if (!source) {
source = this.isNullable ? null : this.defaultValue;
}
if (isObject(source)) {
result = {};
for (property in this.dataTemplate) {
if (this.dataTemplate.hasOwnProperty(property)) {
propertyDataTemplate = this.dataTemplate[property];
result[property] = propertyDataTemplate.read(isTargetObject ? target[property] : null, source[property], path + "-" + property, context);
}
}
} else {
result = source;
if (target !== source) {
context.isChanged = true;
}
}
return result;
};