-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertHelper.js
More file actions
100 lines (83 loc) · 4.5 KB
/
convertHelper.js
File metadata and controls
100 lines (83 loc) · 4.5 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const { isDate } = require("moment");
const { setNestedAttribute } = require("./nestedUtil");
class Converter {
getProperBindMap(bindMapArray, object) {
if (bindMapArray.length == 1){
return bindMapArray[0];
} else { // (bindMapArray.length > 1) {
for (let i = 0; i < bindMapArray.length; i++) {
const bindMap = bindMapArray[parseInt(i)];
let processObjectOnlyIfMap = bindMap.processObjectOnlyIf;
if (!processObjectOnlyIfMap) throw `Attribute processObjectOnlyIf is required when more than one bind is given`;
let isProper = true;
for (let [attribute, valuesList] of processObjectOnlyIfMap) {
if (!object[String(attribute)]) throw `Attribute "${attribute}" defined in mapping.processObjectOnlyIf does not exist in source object`;
if (!valuesList.includes(object[String(attribute)])) {
isProper = false;
break;
}
}
if (isProper) {
return bindMap;
}
}
throw `There is no proper bindmap to object "${JSON.stringify(object, null, 2)}" defined in processObjectOnlyIf in bindMapArray`;
}
}
convert(dataObjects, bindingMapArray, opts) {
let returnObjects = [];
let header = (opts && opts.header != undefined && opts.header != null) ? dataObjects[opts.header] : null;
let footer = (opts && opts.footer != undefined && opts.footer != null) ? dataObjects[dataObjects.length + opts.footer] : null;
//start 1(one) object after the header object
let i = (opts && opts.header != undefined && opts.header != null) ? opts.header + 1 : 0;
//dont process footer object (stop one object before) //footer has negative value
let dataObjectsLength = (opts && opts.footer != undefined && opts.footer != null) ? dataObjects.length + opts.footer : dataObjects.length;
if (Array.isArray(bindingMapArray)) {
if (bindingMapArray.length == 0) throw `BindingMapArray is required`;
} else {
throw `BindingMapArray is required or invalid. it must be an array`;
}
for (; i < dataObjectsLength; i++) {
let sourceObject = dataObjects[parseInt(i)];
let destinationObject = {};
let bindingMap = this.getProperBindMap(bindingMapArray,sourceObject);
let skipObjectIfMap = (bindingMap.skipObjectIf) ? bindingMap.skipObjectIf : null;
if (skipObjectIfMap && this.skipCurrentLine(skipObjectIfMap, sourceObject)) continue;
for (let j = 0; j < bindingMap.bindings.length; j++) {
let bind = bindingMap.bindings[parseInt(j)];
switch (bind.type) {
case 'copy':
setNestedAttribute(destinationObject, bind.destination, sourceObject[bind.source]);
break;
case 'header':
setNestedAttribute(destinationObject, bind.destination, header[bind.source]);
break;
case 'footer':
setNestedAttribute(destinationObject, bind.destination, footer[bind.source]);
break;
case 'fixed':
setNestedAttribute(destinationObject, bind.destination, bind.value);
break;
case 'function':
setNestedAttribute(destinationObject,
bind.destination,
bind.value(sourceObject, header, footer, dataObjects.slice(i+1, dataObjectsLength))
);
break;
default:
throw `Bind type "${bind.type}", declared in bindingMap is not valid or not implemented. Bind destination ${bind.destination}.`;
}
}
returnObjects.push(destinationObject);
}
return returnObjects;
}
skipCurrentLine(skipObjectIfMap, object) {
for (let [attribute, skipValuesList] of skipObjectIfMap) {
if (!object[String(attribute)]) throw new Error(`Attribute "${attribute}" defined in mapping.skipObjectIf does not exist in source object`);
if (skipValuesList.includes(object[String(attribute)])) return true;
}
return false;
}
}
exports.Converter = Converter;