This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.js
executable file
·94 lines (87 loc) · 2.16 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
#!/usr/bin/env node
function filter( d, fs ) { // iterative and staging part
var r = JSON.parse( JSON.stringify(d) ); // make a deep copy of the original so as to out-of-place the whole thing
for (var i in fs) { // iterate over all functions to be tested
var f = fs[i]
for (var c in r) { // recursively filter the data and every child (r[c]) for key k being equal to value v
_filter( r, r[c], f );
}
}
return r;
}
function _filter( n, c, f ) { // recursive part
if (typeof n !== "object") { // abort trivial cases
return
}
if ( f(c) ) { // when we find that the function matches on a child
if ( Array.isArray(n) ) { // either we erase it from the array without touching other entries
n[n.indexOf(c)] = "";
n = _clean_array(n);
}
else { // or we delete the entire thing
for (var k in n) {
if (n[k] === c) {
delete n[k];
break;
}
}
}
}
if (typeof c === "object") { // in case the child is itself an object, we must proliferate recursion to all grand children
for (var g in c) {
if (typeof c[g] === "object" && c[g] !== null) {
_filter( c, c[g], f );
}
}
}
}
function _clean_array(a) {
var n = new Array();
for (var i = 0; i < a.length; i++) {
if (a[i]) {
n.push(a[i]);
}
}
return n;
}
//
// boilerplate
//
var fs = [
function(obj) {
return obj["newsletter"] === "Division News Financial Services";
},
function(obj) {
return obj["newsletterEditionArticlesId"] >= 2375252;
}
]
var sdata = require( './data-1.json' );
var mdata = require( './data-40.json' );
var ldata = require( './data-100.json' );
var xdata = require( './data-400.json' );
var edata = require( './data-100000.json' );
var json = edata;
var r = filter( json, fs );
var hits = json.data.hits.hits
for (var i = 0; i < hits.length; i++) {
var hit = hits[i];
var docs = hit._source.newsletterDocuments;
var offset = 0;
for (var j = 0; j < docs.length; j++) {
var jdoc = docs[j].newsletter;
console.log( jdoc );
try {
rdoc = r.data.hits.hits[i]._source.newsletterDocuments[j - offset].newsletter;
if ( jdoc !== rdoc ) {
offset++;
}
else {
console.log( rdoc );
}
}
catch (e) {
offset++;
}
console.log();
}
}