-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
executable file
·174 lines (118 loc) · 4.63 KB
/
index.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Created by sudhir.m on 30/11/16.
*/
'use strict';
const {convertByKey} = require('./utils/helper');
const {getObjectOrArrayFromStringKey, isNonEmptyArray} = require('./utils/utilities');
const {IDENTIFIERS} = require('./utils/constants');
let rootData = {};
let configuration = {};
const transform = (data, transformation) => {
let {mapping, config} = transformation;
// make copy
rootData = data;
if (config) {
configuration = config;
}
return transformData(data, mapping);
};
const transformData = (data, mapping) => {
if (typeof mapping.list !== "undefined" || typeof mapping.objectify !== "undefined" || typeof mapping.collect !== "undefined") {
return transformArray(mapping, data);
} else if (typeof mapping.flat !== "undefined") {
let extractedData = extractArrayIndexObject(mapping.flat, data);
return transformObject(mapping.item, extractedData);
} else {
return transformObject(mapping.item, data);
}
};
const collectArrayElements = (items, data) => {
let arr = [];
items.forEach((mappingKey) => {
if (typeof mappingKey === "string") {
let returnedVal = convertByKey(mappingKey, data, rootData, configuration);
// if returned value is Array, concat array elements
if (isNonEmptyArray(returnedVal)) {
arr = arr.concat(returnedVal);
} else {
arr.push(returnedVal);
}
} else if (Array.isArray(mappingKey)) {
// collect array elements from different arrays
arr = arr.concat(convertByArray(mappingKey[0], data))
} else if (typeof mappingKey === "object") {
arr.push(transformObject(mappingKey, data))
}
});
return arr;
};
const extractArrayIndexObject = (key, data) => {
if (key.indexOf(IDENTIFIERS.ARRAY_INDEX) > -1) {
const index = key.substring(key.lastIndexOf(IDENTIFIERS.ARRAY_START) + 1, key.lastIndexOf(IDENTIFIERS.ARRAY_END));
const keyVal = key.substring(0, key.lastIndexOf(IDENTIFIERS.ARRAY_INDEX));
let extractedData = data;
if (keyVal) {
extractedData = getObjectOrArrayFromStringKey(keyVal, data);
}
return extractedData[index];
}
return {};
};
const convertByArray = (mapping, data) => {
const key = mapping.list;
if (typeof key === "undefined") {
return transformArray(mapping, data);
}
if (key.startsWith(IDENTIFIERS.ARRAY_INDEX) && key.indexOf(IDENTIFIERS.ARRAY_START) > -1 && key.indexOf(IDENTIFIERS.ARRAY_END) > -1) {
const index = key.substring(key.lastIndexOf(IDENTIFIERS.ARRAY_START) + 1, key.lastIndexOf(IDENTIFIERS.ARRAY_END));
return [transformObject(mapping.item, data)];
} else {
return transformArray(mapping, data);
}
};
const specialObjectTransformation = (mappingKey, data) => {
if (typeof mappingKey.flat !== "undefined") {
let extractedData = extractArrayIndexObject(mappingKey.flat, data);
return transformObject(mappingKey.item, extractedData);
} else if (typeof mappingKey.objectify !== "undefined") {
return transformArray(mappingKey, data);
}
return transformObject(mappingKey, data);
};
const transformObject = (obj, data) => {
let transformedObj = {};
let keys = Object.keys(obj);
keys.forEach((key) => {
const mappingKey = obj[key];
if (typeof mappingKey === "string") {
// check for hard coded values, etc..
transformedObj[key] = convertByKey(mappingKey, data, rootData, configuration);
} else if (Array.isArray(mappingKey) && mappingKey.length > 0) {
transformedObj[key] = convertByArray(mappingKey[0], data);
} else if (typeof mappingKey === "object") {
transformedObj[key] = specialObjectTransformation(mappingKey, data);
}
});
return transformedObj;
};
const transformArray = (mapping, data) => {
let sourceArray;
if (typeof mapping.list !== "undefined") {
sourceArray = getObjectOrArrayFromStringKey(mapping.list, data);
} else if (typeof mapping.objectify !== "undefined") {
sourceArray = [getObjectOrArrayFromStringKey(mapping.objectify, data)];
} else if (typeof mapping.collect !== "undefined") {
return collectArrayElements(mapping.item, data);
}
if (!isNonEmptyArray(sourceArray)) {
return [];
}
let itemTransformation = mapping.item;
return sourceArray.map((val, key) => {
return transformObject(itemTransformation, val);
});
};
let transformHelper = {
transform
};
module.exports = transformHelper;