-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
95 lines (95 loc) · 2.8 KB
/
filter.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
const queryJson = (input, filters) => {
const results = [];
input.map((a) => {
const fields = getFields(a);
const potentialMatches = depthSearch(null, a, filters);
potentialMatches.map((pm) => {
let totalFound = 0;
filters.map((filter) => {
const found = pm.predicates.find((pmFilter) => { return pmFilter === filter; });
if (found !== undefined) {
totalFound += 1;
}
});
if (totalFound === filters.length) {
results.push(pm.pairs);
}
});
});
return results;
};
const depthSearch = (prefix, obj, filters) => {
let result = [];
const baseFields = [];
const basePredicates = [];
if (obj == undefined || obj == null) {
return [];
}
Object.keys(obj).map((k) => {
const v = obj[k];
const fullpath = (prefix === null) ? k : `${prefix}.${k}`;
if (typeof v !== "object") {
baseFields.push({
key: fullpath,
value: v,
});
filters.map((filter) => {
if (filter(fullpath, v)) {
basePredicates.push(filter);
}
});
}
});
Object.keys(obj).map((k) => {
const v = obj[k];
const fullpath = (prefix === null) ? k : `${prefix}.${k}`;
if (typeof v === "object" && Array.isArray(v)) {
v.map((element) => {
const search = depthSearch(fullpath, element, filters);
result = result.concat(search);
});
}
else if (typeof v === "object") {
const search = depthSearch(fullpath, v, filters);
result = result.concat(search);
}
});
if (result.length > 0) {
result.map((pm) => {
pm.pairs = pm.pairs.concat(baseFields);
pm.predicates = pm.predicates.concat(basePredicates);
});
}
else if (basePredicates.length > 0) {
const pm = {
pairs: baseFields,
predicates: basePredicates,
};
result.push(pm);
}
return result;
};
const getFields = (source) => {
const result = [];
const flat = (obj, stack) => {
if (obj == undefined || obj == null) {
return [];
}
Object.keys(obj).forEach((k) => {
const s = stack.concat([k]);
const v = obj[k];
if (typeof v === 'object' && Array.isArray(v)) {
flat(v[0], s);
}
else if (typeof v === 'object') {
flat(v, s);
}
else {
result.push(s.join("."));
}
});
};
flat(source, []);
return result;
};
//# sourceMappingURL=filter.js.map