-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOpeningTimes.js
192 lines (157 loc) · 5.72 KB
/
OpeningTimes.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
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
const assert = require('assert');
const util = require('util');
const Moment = require('moment-timezone');
const removePastAlterations = require('./src/lib/removePastAlterations');
const weekdays = Moment.weekdays().map((d) => d.toLowerCase());
class OpeningTimes {
constructor(openingTimes, timeZone, alterations) {
assert(openingTimes, 'parameter \'openingTimes\' undefined/empty');
const parameterWeek = Object.keys(openingTimes).sort();
assert.deepEqual(
parameterWeek, weekdays.sort(),
`parameter 'openingTimes' should have all days of the week (${parameterWeek})`
);
assert(
weekdays.every((d) => Array.isArray(openingTimes[d])),
'parameter \'openingTimes\' should define opening times for each day.'
+ ` (${util.inspect(openingTimes)})`
);
assert(timeZone, 'parameter \'timeZone\' undefined/empty');
assert(Moment.tz.zone(timeZone), 'parameter \'timeZone\' not a valid timezone');
this._openingTimes = openingTimes;
this._timeZone = timeZone;
this._alterations = alterations;
}
/* Private methods - you could use them but they are not part of the API
and therefore any changes to them will not trigger a major version number */
_getDayName(moment) {
return moment.format('dddd').toLowerCase();
}
_getTime(moment, hour, minute) {
const returnDate = moment.clone().tz(this._timeZone);
returnDate.startOf('hour').hour(hour).minute(minute);
return returnDate;
}
_getTimeFromString(timeString) {
const timeSplit = timeString.split(':');
return {
hours: parseInt(timeSplit[0], 10),
minutes: parseInt(timeSplit[1], 10),
};
}
_createDateTime(moment, timeString) {
const time = this._getTimeFromString(timeString);
return this._getTime(moment, time.hours, time.minutes);
}
_isClosedAllDay(daysOpeningTimes) {
return (daysOpeningTimes.length === 0);
}
_formatTime(timeString, formatString = 'h:mm a') {
if (timeString === '00:00' || timeString === '23:59') {
return 'midnight';
}
const aDate = Moment();
const time = this._getTimeFromString(timeString);
return this._getTime(aDate, time.hours, time.minutes).format(formatString);
}
_getOpeningTimesForDate(moment) {
const alterations = removePastAlterations(this._alterations, moment);
if (alterations) {
// TODO: decide what to do if there is only >1 match
const momentDate = moment.format('YYYY-MM-DD');
const todaysAlteration = Object.keys(alterations).find((a) => a === momentDate);
return todaysAlteration
? alterations[todaysAlteration]
: this._openingTimes[this._getDayName(moment)];
}
return this._openingTimes[this._getDayName(moment)];
}
_getOpeningTimesSessionForMoment(moment, daysLookAhead) {
for (let day = daysLookAhead - 1; day >= -1; day -= 1) {
const aMoment = moment.clone().add(day, 'day');
const openingTimes = this._getOpeningTimesForDate(aMoment);
for (let j = 0; j < openingTimes.length; j += 1) {
const t = openingTimes[j];
const from = this._createDateTime(aMoment, t.opens);
const to = this._createDateTime(aMoment, t.closes);
if (to < from) {
to.add(1, 'day');
}
if (moment.isBetween(from, to, null, '[)')) {
return { from, to };
}
}
}
return undefined;
}
_getOpenSessions(moment, days) {
// Get sessions for week (including yesterday for the cases where opening hours span midnight)
return days.map((d) => {
const aDate = moment.clone().add(d, 'day');
const openingTimes = this._getOpeningTimesForDate(aDate);
return openingTimes
.map((t) => {
const from = this._createDateTime(aDate, t.opens);
const to = this._createDateTime(aDate, t.closes);
if (to < from) {
to.add(1, 'day');
}
return { from, to };
});
});
}
_getDateInSessionFinder(moment) {
return (session) => (moment.isBetween(session.from, session.to, null, '[)'));
}
_getDateBeforeSessionFinder(moment) {
return (session) => (moment.isBefore(session.from));
}
_findMomentInSessions(moment, sessions) {
return sessions.some((day) => (day.some(this._getDateInSessionFinder(moment))));
}
_findNextOpeningTime(moment, sessions) {
const nextDay = sessions
.filter((day) => (!this._isClosedAllDay(day)))
.find((day) => (day.some(this._getDateBeforeSessionFinder(moment))));
if (nextDay) {
return nextDay.find(this._getDateBeforeSessionFinder(moment)).from;
}
return undefined;
}
_nextOpen(moment) {
const allSessions = this._getOpenSessions(moment, [0, 1, 2, 3, 4, 5, 6]);
if (this._findMomentInSessions(moment, allSessions)) {
return moment;
}
return this._findNextOpeningTime(moment, allSessions);
}
/* Public API */
getStatus(moment, options = {}) {
const returnValue = { moment };
const session = this._getOpeningTimesSessionForMoment(moment, 1);
returnValue.isOpen = (session !== undefined);
if (options.next) {
if (returnValue.isOpen) {
returnValue.nextClosed = session.to;
returnValue.nextOpen = moment;
} else {
returnValue.nextClosed = moment;
returnValue.nextOpen = this._nextOpen(moment);
}
}
return returnValue;
}
getFormattedOpeningTimes(formatString) {
const openingTimes = {};
Moment.weekdays().forEach((d) => {
const day = d.toLowerCase();
openingTimes[day] = this._openingTimes[day].map((t) => (
{
closes: this._formatTime(t.closes, formatString),
opens: this._formatTime(t.opens, formatString),
}));
});
return openingTimes;
}
}
module.exports = OpeningTimes;