-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
118 lines (104 loc) · 2.6 KB
/
util.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var _ = require('underscore');
var Models = require('./../../models'),
Tags = Models.Tags;
function arrtomap(arr) {
var m = {};
arr.forEach(function (a) {
m[a._id.toString()] = a;
});
return m;
}
function torrenticon(torrent) {
var icon;
if (torrent.team && torrent.team.icon) {
icon = '/' + torrent.team.icon;
} else if (torrent.uploader) {
icon = '/avatar/' + torrent.uploader.emailHash;
}
return icon;
}
function getShowList(rbs) {
var weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var showList = [];
var aDays = [];
var tempList = {};
var avdays = {};
var theFirstDay = 0;
rbs.forEach(function (rb) {
if (tempList[rb.showOn]) {
tempList[rb.showOn].push(rb);
} else {
tempList[rb.showOn] = [rb];
}
avdays[rb.showOn] = true;
});
//find the first day
var maxCount = 0;
for (var i = 0; i < weekDays.length; i++) {
var count = 0;
for (var j = i; j < i + 4; j++) {
var k = j % weekDays.length;
if (avdays[k]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
theFirstDay = i;
}
}
for (var j = theFirstDay; j < theFirstDay + 4; j++) {
var k = j % weekDays.length;
aDays.push(weekDays[k]);
showList.push(tempList[k]);
}
return {
availableDays: aDays,
showList: showList
};
}
function getBangumiList(bs) {
var weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var bangumis = [];
var tag_ids = [];
for (var i = 0; i < bs.length; i++) {
tag_ids.push(bs[i].tag_id);
if (bangumis[bs[i].showOn]) {
bangumis[bs[i].showOn].push(bs[i]);
} else {
bangumis[bs[i].showOn] = [bs[i]];
}
}
return {
weekDays: weekDays,
bangumiList: bangumis
};
}
function *getTags(objs) {
var tag_ids = [];
objs.forEach(function (obj) {
if (obj.tag_id) {
var stag_id = obj.tag_id.toString();
if (tag_ids.indexOf(stag_id) === -1) {
tag_ids.push(stag_id);
}
}
});
if (tag_ids.length > 0) {
var tags = yield new Tags().find(tag_ids);
var mtags = arrtomap(tags);
for (var i = 0; i < objs.length; i++) {
var stag_id = objs[i].tag_id.toString();
if (stag_id && mtags[stag_id]) {
objs[i].tag = mtags[stag_id];
}
}
}
return objs;
}
exports.arrtomap = arrtomap;
exports.torrenticon = torrenticon;
/* Bangumis */
exports.getShowList = getShowList;
exports.getBangumiList = getBangumiList;
exports.getTags = getTags;