-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcinemaQuery.js
More file actions
75 lines (66 loc) · 2.37 KB
/
Copy pathcinemaQuery.js
File metadata and controls
75 lines (66 loc) · 2.37 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
'use strict';
var _ = require("lodash"),
query = require("./query"),
dist = require('geo-distance-safe');
var maxDate = new Date(-8640000000000000);
function getSoonestFilmTime(cinema, filmId, date) {
var soonestFilmScheduleItem = cinema.timeTable.reduce(function (prevBest, item) {
if (item.filmId !== filmId || item.date < date) {
return prevBest;
}
if (!prevBest) {
return item;
}
return item.date < prevBest.date ? item : prevBest;
}, null);
return soonestFilmScheduleItem ? soonestFilmScheduleItem.date : maxDate;
}
function nearnessOrderer(coords) {
return function (cinema) {
return dist.between(coords, cinema.coords);
};
}
function soonestFilmDateOrderer(filmId, date) {
return function (cinema) {
return getSoonestFilmTime(cinema, filmId, date);
};
}
function BaseCinemaQuery() {
query.BasicQuery.apply(this, arguments);
}
function OrdFilterCinemaQuery() {
query.OrdFilterQuery.apply(this, arguments);
}
function RangeCinemaQuery() {
query.RangeQuery.apply(this, arguments);
}
BaseCinemaQuery.prototype = Object.create(query.BasicQuery.prototype);
OrdFilterCinemaQuery.prototype = Object.create(query.OrdFilterQuery.prototype);
RangeCinemaQuery.prototype = Object.create(query.RangeQuery.prototype);
[BaseCinemaQuery, OrdFilterCinemaQuery, RangeCinemaQuery].forEach(function (CinemaQuery) {
CinemaQuery.prototype.ctors = {
Basic: BaseCinemaQuery,
OrdFilter: OrdFilterCinemaQuery,
Range: RangeCinemaQuery
};
CinemaQuery.prototype.withFilm = function (filmId, date) {
return this.filter(function (cinema) {
return cinema.timeTable.some(function (entry) {
return entry.filmId === filmId && entry.date >= date;
});
});
};
CinemaQuery.prototype.orderByNearness = function (pos) {
return this.orderBy(nearnessOrderer(pos));
};
CinemaQuery.prototype.thenByNearness = function (pos) {
return this.thenBy(nearnessOrderer(pos));
};
CinemaQuery.prototype.orderBySoonest = function (filmId, date) {
return this.orderBy(soonestFilmDateOrderer(filmId, date));
};
CinemaQuery.prototype.thenBySoonest = function (filmId, date) {
return this.thenBy(soonestFilmDateOrderer(filmId, date));
};
});
exports.BaseCinemaQuery = BaseCinemaQuery;