-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (76 loc) · 1.9 KB
/
index.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
const fs = require("fs");
const perfJsonFile = "perfs.json";
const likesJsonFile = "likes.json";
const bundles = [
{
f: perfJsonFile,
r: {}
},
{
f: likesJsonFile,
r: {}
}
];
let notReady = bundles.length;
class Perfs {
constructor () {
this.perfs = [];
// key is perf id,
// value is perf index's
this.preSortedMap = {};
}
push (perf) {
let index = this.preSortedMap[perf.id];
if (!index) {
this.perfs.push(perf);
this.preSortedMap[perf.id] = index = this.perfs.length - 1;
}
this.perfs[index].likes++;
}
sort () {
return this.perfs.sort((a, b) => {
var diff = b.likes - a.likes;
return diff == 0 ? a.start.localeCompare(b.start) : diff;
});
}
}
bundles.forEach(bundle => {
fs.readFile(bundle.f, (err, data) => {
if (err)
throw err;
notReady--;
bundle.r = JSON.parse(data);
next();
});
})
function next () {
if (notReady > 0)
return;
let perfsById = bundles[0].r;
let peopleLikes = bundles[1].r;
const perfsPref = new Perfs();
Object.values(peopleLikes).forEach(personLikes => {
personLikes.forEach(likeId => {
let perf = perfsById[likeId];
if (perf)
perfsPref.push(perf);
})
});
filterOrderPerfs(perfsPref);
}
function filterOrderPerfs(perfs) {
let sortedPerfs = perfs.sort();
const perDay = {
27: [],
28: [],
29: []
};
sortedPerfs.forEach(p => perDay[getDay(p)].push(p));
console.log("Friday 27", perDay["27"]);
console.log("Saturday 28", perDay["28"]);
console.log("Sunday 29", perDay["29"]);
fs.writeFileSync("result.json", JSON.stringify(perDay));
}
function getDay (perf) {
return perf.start.split("T")[0].split("-")[2];
}