-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.js
More file actions
108 lines (92 loc) · 2.11 KB
/
Copy path8.js
File metadata and controls
108 lines (92 loc) · 2.11 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
module.exports = Service
var moment = require('moment')
var days = [
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday'
]
function Service (id) {
this.id = id
this.days = {
monday: Service.Status.Unknown,
tuesday: Service.Status.Unknown,
wednesday: Service.Status.Unknown,
thursday: Service.Status.Unknown,
friday: Service.Status.Unknown,
saturday: Service.Status.Unknown,
sunday: Service.Status.Unknown
}
this.exceptions = {}
}
/**
* Add an exception for the given date.
* A service can either be added or removed for this date.
* @param {String} date of format YYYYMMDD
* @param {[type]} type one of Service.Exception
*/
Service.prototype.addException = function addException (date, type) {
this.exceptions[date] = type
}
/**
* Check whether the given Service is operating on a given date.
* @param {Date} date
* @return {Bool}
*/
Service.prototype.operating = function operatingOnDate (date) {
date = date || new Date()
date = moment(date)
var day = days[date.day()]
var yyyymmdd = date.format('YYYYMMDD')
// Check start and end
if (this.start && this.start > yyyymmdd) {
return false
}
if (this.end && this.end < yyyymmdd) {
return false
}
// Check Exceptions
if (this.exceptions[yyyymmdd] && this.exceptions[yyyymmdd] === Service.Status.Operating) {
// Operating
return true
}
if (this.exceptions[yyyymmdd] && this.exceptions[yyyymmdd] === Service.Status.NotOperating) {
// Not Operating
return false
}
if (this.days[day] === 1) {
// Operating
return true
}
return false
}
/**
* Following the GTFS semantics for exceptions.
* @type {Object}
*/
Service.Exception = {
Added: 1,
Removed: 2
}
/**
* Get the name of an exception
* @param {Integer} exception
* @return {String}
*/
Service.Exception.toString = function exceptionToString (exception) {
for (var type in Service.Exception) {
if (Service.Exception[type] === exception) {
return type
}
}
return '(unknown)'
}
Service.Status = {
Operating: 1,
NotOperating: -1,
Unknown: 0
}