forked from ql-io/ql.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudfs.js
79 lines (71 loc) · 1.69 KB
/
udfs.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
var _ = require('underscore');
var j = require('JSONPath');
this.a1 = [
{"name": "Brand-A", "keys": [
{ "name": "G1"},
{"name": "G2"},
{"name": "G3"}
]},
{"name": "Brand-B", "keys": [
{ "name": "G1"},
{"name": "G2"}
]},
{"name": "Brand-C", "keys": [
{ "name": "G4"},
{"name": "G2"}
]}
];
this.a2 = [
{"name": "Brand-A", "details": [
{"name": "G3", "count": 32},
{"name": "G5", "count": 18}
]},
{"name": "Brand-C", "details": [
{"name": "G3", "count": 32},
{"name": "G5", "count": 18}
]}
];
this.keySet = [
["G1", "G2", "G3"],
["G1", "G2"],
["G4", "G2"]
];
this.rows = this.a2;
var foo = [
[ 'Brand-A', [
{ name: 'G3', count: 32 },
{ name: 'G5', count: 18 }
] ],
[ 'Brand-C', [
{ name: 'G3', count: 32 },
{ name: 'G5', count: 18 }
] ]
];
this.index = 0;
this.row = this.rows[this.index];
// Ideally this function should receive the column a1.keys
exports.f1 = function (keys) {
var keys = this.keySet[this.index];
var names = j.eval(this.row.details, '$..name');
var found = false;
for(var i = 0; i < names.length; i++) {
if(keys.indexOf(names[i]) > -1) {
found = true;
break;
}
}
console.log(found);
return found;
}
exports.f2 = function (keys, details) {
var names = j.eval(details, '$..name');
var found = false;
for(var i = 0; i < names.length; i++) {
if(keys.indexOf(names[i]) > -1) {
found = true;
break;
}
}
// if null, this row will be excluded from results
return found ? this.row : null;
}