-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransformations.js
225 lines (213 loc) Β· 5 KB
/
transformations.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
// Get dataset row
// ds - <Object[]>
// Returns: <Array>
//
// Example: row([ { Id: 1, Name: 'Marcus' } ])
// Result: [1, 'Marcus']
const row = ds => {
const result = [];
if (Array.isArray(ds) && ds.length > 0) {
const obj = ds[0];
for (const key in obj) {
result.push(obj[key]);
}
}
return result;
};
// Get dataset column
// Signature: ds[, field]
// ds - <Object[]>
// field - <string>, field name, optional
// Returns: <Array>
//
// Example: col([ { Id: 1 }, { Id: 2 }, { Id: 3 } ])
// Result: [1, 2, 3]
const col = (ds, field) => {
let result = [];
if (Array.isArray(ds) && ds.length > 0) {
field = field || Object.keys(ds[0])[0];
result = ds.map(record => record[field]);
}
return result;
};
// Get dataset header
// ds - <Object[]>
// Returns: <string[]>
//
// Example: header([ { Id: 1, Name: 'Marcus' } ])
// Result: ['Id', 'Name']
const header = ds => {
if (Array.isArray(ds) && ds.length > 0) {
const obj = ds[0];
return Object.keys(obj);
} else {
return [];
}
};
// Dataset projection
// meta - <string[]> | <Object>, projection metadata array of
// field names or object with structure:
// { toKey: [ fromKey, funcs ] }
// ds - <Object[]>
// Returns: <Object[]>
//
// Example: projection({ toKey: [ fromKey, funcs ] },
// [ { Id: 1, Name: 'Marcus' } ]);
//
// Example: projection(['Name'], [ { Id: 1, Name: 'Marcus' } ]);
//
// Result: [ { Name: 'Marcus' } ]
const projection = (meta, ds) => {
const complex = !Array.isArray(meta);
const fields = complex ? Object.keys(meta) : meta;
return ds.map(record => {
const row = {};
fields.forEach(key => {
if (complex) {
const data = meta[key];
const fromKey = data[0];
const value = record[fromKey];
if (value !== undefined) {
row[key] = data.slice(1).reduce((acc, fn) => fn(acc), value);
}
} else {
const value = record[key];
if (value !== undefined) row[key] = record[key];
}
});
return row;
});
};
// Set union
// ds1 - <Object[]>
// ds2 - <Object[]>
// Returns: <Object[]>
//
// Example: union([ { Id: 1 }, { Id: 2 } ], [ { Id: 2 }, { Id: 3 } ]);
// Result: [ { Id: 1 }, { Id: 2 }, { Id: 3 } ]
const union = (ds1, ds2) => {
const ds = [];
const ids = [];
const l1 = ds1.length;
const l2 = ds2.length;
for (let i = 0; i < l1; i++) {
const item = ds1[i];
ids.push(item.Id);
ds.push(item);
}
for (let i = 0; i < l2; i++) {
const item = ds2[i];
if (!ids.includes(item.Id)) {
ids.push(item.Id);
ds.push(item);
}
}
return ds;
};
// Set intersection
// ds1 - <Object[]>
// ds2 - <Object[]>
// Returns: <Object[]>
//
// Example: intersection([ { Id: 1 }, { Id: 2 } ], [ { Id: 2 }, { Id: 3 } ]);
// Result: [ { Id: 2 } ]
const intersection = (ds1, ds2) => {
const ds = [];
const ids = [];
const l1 = ds1.length;
const l2 = ds2.length;
for (let i = 0; i < l1; i++) {
const item = ds1[i];
ids.push(item.Id);
}
for (let i = 0; i < l2; i++) {
const item = ds2[i];
if (ids.includes(item.Id)) {
ds.push(item);
}
}
return ds;
};
// Set difference
// ds1 - <Object[]>
// ds2 - <Object[]>
// Returns: <Object[]>
//
// Example: difference([ { Id: 1 }, { Id: 2 } ], [ { Id: 2 }, { Id: 3 } ]);
// Result: [ { Id: 1 } ]
const difference = (ds1, ds2) => {
const ds = [];
const ids = [];
const l1 = ds1.length;
const l2 = ds2.length;
for (let i = 0; i < l2; i++) {
const item = ds2[i];
ids.push(item.Id);
}
for (let i = 0; i < l1; i++) {
const item = ds1[i];
if (!ids.includes(item.Id)) {
ds.push(item);
}
}
return ds;
};
// Set complement
// ds1 - <Object[]>
// ds2 - <Object[]>
// Returns: <Object[]>
//
// Example: complement([ { Id: 1 }, { Id: 2 } ], [ { Id: 2 }, { Id: 3 } ]);
// Result: [ { Id: 3 } ]
const complement = (ds1, ds2) => difference(ds2, ds1);
const compare = (value, op, data) => {
if (op === '=') return value === data;
if (op === '<') return value < data;
if (op === '>') return value > data;
if (op === '<=') return value <= data;
if (op === '>=') return value >= data;
return false;
};
const condition = def => {
if (typeof def !== 'string') {
return ['=', def];
}
const c0 = def[0];
const eq = c0 === '=';
const nt = c0 === '!';
if (eq || nt) return [c0, def.substr(1).trim()];
const c1 = def[1];
const et = c1 === '=';
const lt = c0 === '<';
const gt = c0 === '>';
if (lt || gt) {
if (et) return [c0 + c1, def.substr(2).trim()];
else return [c0, def.substr(1).trim()];
}
return ['=', def];
};
const constraints = (defs, prepare = condition) => {
const keys = Object.keys(defs);
const prepared = {};
const len = keys.length;
for (let i = 0; i < len; i++) {
const key = keys[i];
const def = defs[key];
prepared[key] = prepare(def);
}
return prepared;
};
module.exports = {
row,
col,
header,
projection,
union,
intersection,
difference,
complement,
compare,
condition,
constraints,
};