-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-encoder.js
More file actions
80 lines (71 loc) · 2.28 KB
/
data-encoder.js
File metadata and controls
80 lines (71 loc) · 2.28 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
const _ = require ('lodash');
const DataMapper = require ('./data-mappers/dataMapper');
class DataEncoder {
/**
* @param data {Array} of Objects
* @param mappingObj {Object}
* */
_tranformData (data, mappingObj) {
const emptyMappingObj = {};
let _mappingObj = {};
const transformedData = {};
let columns = [];
const result = [];
const transformedColumns = [];
//create a empty mapping array
_.each (data[0], function (value, key) {
emptyMappingObj[key] = null;
});
//the columns not in the mappingObj will not be touched
_mappingObj = _.defaults (mappingObj, emptyMappingObj);
//apply the encoder to each column
_.each (data[0], (value, key) => {
const mapping = _mappingObj[key];
if (mapping && mapping instanceof DataMapper) {
const pickedRow = _.map (data, function (row) {
return row[key];
});
transformedData[key] = mapping[this.transformMethod] (pickedRow, key);
}
});
//concat every value in one single row
_.each (transformedData, function (featureData, feature) {
columns = _.concat (columns, featureData.columns);
transformedColumns.push (feature);
_.each (featureData.values, function (row, index) {
if (result[index] === undefined) {
result[index] = [];
}
result[index] = _.concat (result[index], row);
});
});
//append the unmapped columns values to the final result
const unchangedColumns = _.difference (
_.keys (_mappingObj),
transformedColumns
);
_.each (data, function (row, index) {
_.each (unchangedColumns, function (column) {
result[index] = _.concat (result[index], [data[index][column]]);
});
});
return {columns: _.concat (columns, unchangedColumns), values: result};
}
/**
* @param data {Array} of Objects
* @param mappingObj {Object}
* */
fitTransform (data, mappingObj) {
this.transformMethod = 'fitTransform';
return this._tranformData (data, mappingObj);
}
/**
* @param data {Array} of Objects
* @param mappingObj {Object}
* */
transform (data, mappingObj) {
this.transformMethod = 'transform';
return this._tranformData (data, mappingObj);
}
}
module.exports = DataEncoder;