-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBaseEvent.js
More file actions
224 lines (209 loc) · 6.82 KB
/
BaseEvent.js
File metadata and controls
224 lines (209 loc) · 6.82 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*global Collection: true*/
(function (toExport) {
"use strict";
/**
* Создает оболочку над массивом событий, предоставляющую "sql" подобные операции
*
* @class Оболочка над массивом событий.
* @augments Collection
*/
var BaseEvent = function BaseEvent(events) {
"use strict";
Collection.call(this, events);
}
toExport.BaseEvent = BaseEvent;
BaseEvent.prototype = Object.create(Collection.prototype, {
constructor: {
value: BaseEvent,
enumerable: false,
writable: true,
configurable: true
}
});
/**
*
*@field {BaseEvent} - ссылка на "родной" конструктор
*/
BaseEvent.prototype.constructor = BaseEvent;
/**
*@function Возвращает новую оболочку, но уже только с прошедшими событиями
*
*@return {BaseEvent}
*/
BaseEvent.prototype.pastEventBase = function () {
var currentDate = new Date();
return this.filter(function (event) {
return event.end.getTime() < currentDate.getTime();
});
};
/**
* @function Возвращает новую оболочку, но уже только с ненаступившими событиями
*
* @return {BaseEvent}
*/
BaseEvent.prototype.nextEventBase = function () {
var currentDate = new Date();
return this.filter(function (event) {
return event.start.getTime() > currentDate.getTime();
});
};
/**
* @function Возвращает новую оболочку, но уже с событиями, которые идут в данный момент
*
* @return
*/
BaseEvent.prototype.nowEventBase = function () {
var currentDate = new Date();
return this.filter(function (event) {
return (event.start.getTime() <= currentDate.getTime() && event.end.getTime() >= currentDate.getTime());
});
};
/**
* @function Возвращает новую оболочку, но уже с событиями, в которых участвует определенный человек
*
* @return
*/
BaseEvent.prototype.withFriend = function (myFriend) {
return this.filter(function (event) {
return event.parties.some(function (party) {
return party.name === myFriend.name;
});
});
};
/**
* @function Увеличивает дату на день
* @private
*
* @field {Date} currentDate дата, которую будем увеличивать
*
* @return {Date}
*/
var addDay = function (currentDate) {
return new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
}
/**
* @function Возвращает новую оболочку, но уже с событиями, которые будут через день
*
* @return {BaseEvent}
*/
BaseEvent.prototype.getEventAfterDay = function () {
var currentDate = addDay(new Date());
return this.filter(function (event) {
return event.start.getTime() > currentDate.getTime();
});
};
/**
* @function Увеличивает дату на неделю
* @private
*
* @field {Date} currentDate дата, которую будем увеличивать
*
* @return {Date}
*/
var addWeek = function (currentDate) {
return new Date(currentDate.getTime() + 7 * 24 * 60 * 60 * 1000);
}
/**
* @function Возвращает новую оболочку, но уже с событиями, которые будут через неделю
*
* @return {BaseEvent}
*/
BaseEvent.prototype.getEventAfterWeek = function () {
var currentDate = addWeek(new Date());
return this.filter(function (event) {
return event.start.getTime() > currentDate.getTime();
});
};
/**
* @function Увеличивает дату на месяц
* @private
*
* @field {Date} currentDate дата, которую будем увеличивать
*
* @return {Date}
*/
var addMonth = function (currentDate) {
if (currentDate.getMonth() === 11) {
currentDate = new Date(currentDate.getFullYear() + 1, 0, currentDate.getDay());
} else {
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, currentDate.getDay());
}
return currentDate;
}
/**
* @function Возвращает новую оболочку, но уже с событиями, которые будут через месяц
*
* @return {BaseEvent}
*/
BaseEvent.prototype.getEventAfterMonth = function () {
var currentDate = addMonth(new Date());
return this.filter(function (event) {
return event.start.getTime() > currentDate.getTime();
});
};
/**
* @function Возвращает новую оболочку, но уже с событиями, которые будут в определенный период
*
* @param {Date} fromDate - начала периода
* @param {Date} toDate - конец периода
*
* @return
*/
BaseEvent.prototype.getEventFromPeriod = function (fromDate, toDate) {
return this.filter(function (event) {
return (event.start.getTime() > fromDate.getTime() && event.end.getTime() < toDate.getTime());
});
};
/**
* @function Компаратор рейтинга по убыванию
* @private
*
* @field {Date} a
* @field {Date} b
*
* @return {Numeric}
*/
var starsComparer = function compare(a, b) {
if (a.stars > b.stars) {
return -1;
}
if (a.stars < b.stars) {
return 1;
}
return 0;
};
/**
* @function Возвращает новую оболочку c теми же событиями, но отсортированными по уменьшению количества звезд
*
* @return {BaseEvent}
*/
BaseEvent.prototype.sortByStars = function (ascending) {
return this.sortBy(starsComparer, ascending);
};
/**
* @function Компаратор дат по возрастанию
* @private
*
* @field {Date} a
* @field {Date} b
*
* @return {Numeric}
*/
var dateComparer = function (a, b) {
if (a.start.getTime() < b.start.getTime()) {
return -1;
}
if (a.start.getTime() > b.start.getTime()) {
return 1;
}
return 0;
};
/**
* @function Возвращает новую оболочку c теми же событиями, но отсортированными по дате
*
* @return {BaseEvent}
*/
BaseEvent.prototype.sortByDate = function (ascending) {
return this.sortBy(dateComparer, ascending);
};
}(window));