-
Notifications
You must be signed in to change notification settings - Fork 0
/
shredder.html
executable file
Β·162 lines (148 loc) Β· 4.46 KB
/
shredder.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Shredder</title>
<link rel="stylesheet" href="shredder.css">
<link rel="shortcut icon" type="image/png" href="shredder-favicon.png">
<script src="lib/json2csv.js"></script>
<script src="lib/lodash.min.js"></script>
<script src="lib/faa-grp.js"></script>
<script src="lib/atlas-grp.js"></script>
<script src="lib/avianis-grp.js"></script>
<script src="lib/console.js"></script>
<script>
/*
* Shreds fleets and returns the shreds
*
* @param aircraft has arbitrary shape but should contain a tail number
* @param fleet = {
* aircraft: array of aircraft json objects,
* matchProp: tail number property name in aircraft
* }
*
*/
// filters `fleet` by `matchProp` values of `xFleet`
function match(fleet, xFleet) {
let matches = [];
const matchProp = fleet.matchProp;
const xMatch = _.map(xFleet.collection, xFleet.matchProp);
xMatch.forEach (matchVal => {matches
.push(_.filter(fleet.collection, _.matches({[matchProp]: matchVal})))
});
// flatten and remove falsy
matches = _.compact(matches.flat());
return matches;
}
// wraps an array of `aircraft` with `fleet` adapter
function wrap(aircraft, fleet) {
return result = {
collection: aircraft,
matchProp: fleet.matchProp
}
}
// splits `fleet` by `matchProp` values of `xFleet` into matches and diffs
// matches and diffs json are wrapped with `fleet` shape
// csv is also provided
function split(fleet, xFleet) {
const aircraft = fleet.collection;
let result;
let matches = match(fleet, xFleet);
if (matches.length > 0) {
result = {
matchesJson: wrap(matches, fleet),
matchesCsv: csv(matches)
}
}
let diffs = _.difference(aircraft, matches);
if ( diffs.length > 0) {
result = {...result,
diffsJson: wrap(diffs, fleet),
diffsCsv: csv(diffs)
}
}
return result;
}
function csv(val) {
return json2csv.parse(val);
}
function display(id, val) {
const ele = document.getElementById(id);
ele.innerHTML = csv(val);
ele.after(`rows: ${val.collection.length}`);
}
// laziness
const s = split.bind(split);
const w = wrap.bind(wrap);
const fa = faa;
const at = atlas;
const av = avianis;
</script>
</head>
<body>
<div class="title">
<img src="shredder.png" alt="">
<h1>Shredder</h1>
</div>
<hr>
<div class="instruction">
<ul>
<li>Splits, matches, or diffs two arbitrary arrays of json objects by specifying the key name to match from in each set</li>
<li>Sample datasets are in lib/</li>
<li>π Shred even harder in the JavaScript console π</li>
</ul>
</div>
<div class="container">
<div class="faa">
<h2>FAA β© Atlas</h2>
<textarea id="faa-atlas-grp"></textarea>
/ Tails in FAA also in Atlas
<hr>
<h2>FAA β Atlas</h2>
<textarea id="faa-atlas-diff"></textarea>
/ Tails in FAA not in Atlas
<hr>
<h2>FAA β© Avianis</h2>
<textarea id="faa-avianis-grp"></textarea>
/ Tails in FAA also in Avianis
<hr>
<h2>FAA β Avianis</h2>
<textarea id="faa-avianis-diff"></textarea>
/ Tails in FAA not in Avianis
</div>
<div class="atlas">
<h2>Atlas β© FAA</h2>
<textarea id="atlas-grp"></textarea>
/ Tails in Atlas also in FAA
<hr>
<h2>Atlas β FAA</h2>
<textarea id="atlas-diff"></textarea>
/ Tails in Atlas not in FAA
</div>
<div class="avianis">
<h2>Avianis β© FAA</h2>
<textarea id="avianis-grp"></textarea>
/ Tails in Avianis also in FAA
<hr>
<h2>Avianis β FAA</h2>
<textarea id="avianis-diff"></textarea>
/ Tails in Avianis not in FAA
</div>
</div>
<hr>
<script>
let tmp = split(faa, atlas);
display('faa-atlas-grp', tmp.matchesJson);
display('faa-atlas-diff', tmp.diffsJson);
tmp = split(faa, avianis);
display('faa-avianis-grp', tmp.matchesJson);
display('faa-avianis-diff', tmp.diffsJson);
tmp = split(atlas, faa);
display('atlas-grp', tmp.matchesJson);
display('atlas-diff', tmp.diffsJson);
tmp = split(avianis, faa);
display('avianis-grp', tmp.matchesJson);
display('avianis-diff', tmp.diffsJson);
</script>
</body>
</html>