-
Notifications
You must be signed in to change notification settings - Fork 3
/
read-services-and-exceptions.js
200 lines (178 loc) · 5.06 KB
/
read-services-and-exceptions.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
193
194
195
196
197
198
199
200
'use strict'
const {eq: arrEq, has: arrHas, add: arrInsert} = require('sorted-array-functions')
const expectSorting = require('./lib/expect-sorting')
const joinIteratively = require('./lib/join-iteratively')
const isNotFoundError = require('./lib/is-not-found-error')
const datesBetween = require('./lib/dates-between')
const parseDate = require('./lib/parse-date')
const REMOVED = '2'
const ADDED = '1'
const readServicesAndExceptions = async function* (readFile, timezone, filters = {}, opt = {}) {
if (typeof readFile !== 'function') {
throw new TypeError('readFile must be a function')
}
if ('string' !== typeof timezone || !timezone) {
throw new Error('timezone must be a non-empty string.')
}
const {
service: serviceFilter,
serviceException: serviceExceptionFilter,
} = {
service: () => true,
serviceException: () => true,
...filters
}
if (typeof serviceFilter !== 'function') {
throw new TypeError('filters.service must be a function')
}
if (typeof serviceExceptionFilter !== 'function') {
throw new TypeError('filters.serviceException must be a function')
}
const {
exposeStats,
weekdaysMap,
} = {
exposeStats: false,
weekdaysMap: new Map(),
...opt,
}
await new Promise(r => setTimeout(r, 0))
let servicesFileExists = true
let services = (async function* () {})()
try {
services = await readFile('calendar')
} catch (err) {
if (err) { // wtf
if (isNotFoundError(err)) servicesFileExists = false
else throw err
}
}
let exceptionsFileExists = true
let exceptions = (async function* () {})()
try {
exceptions = await readFile('calendar_dates')
} catch (err) {
if (err) { // wtf
if (isNotFoundError(err)) exceptionsFileExists = false
else throw err
}
}
if (!servicesFileExists && !exceptionsFileExists) {
// todo: throw proper error
throw new Error('Both calendar & calendar_dates are missing, at least one is required.')
}
const checkServicesSorting = expectSorting('calendar', (a, b) => {
if (a.service_id === b.service_id) return 0
return a.service_id < b.service_id ? -1 : 1
})
const checkExceptionsSorting = expectSorting('calendar_dates', (a, b) => {
if (a.service_id < b.service_id) return -1
if (a.service_id > b.service_id) return 1
if (a.date > b.date) return 1
return a.date < b.date ? -1 : 1
})
const matchException = (svc, ex) => {
if (svc.service_id === ex.service_id) return 0
return svc.service_id < ex.service_id ? -1 : 1
}
const pairs = joinIteratively(matchException, services, exceptions, {
filterA: serviceFilter,
filterB: serviceExceptionFilter,
})
const weekdayOf = (date) => {
if (weekdaysMap.has(date)) return weekdaysMap.get(date)
const weekday = new Date(date + 'T00:00Z').getDay()
weekdaysMap.set(date, weekday)
return weekday
}
const {NONE} = joinIteratively
let dates = []
let svc = {service_id: NaN}
// todo: default to null? perf?
let nrOfDates = new Array(7).fill(0)
let removedDates = []
for await (const [s, ex] of pairs) {
let _svc = {service_id: NaN}
if (ex !== NONE) {
checkExceptionsSorting(ex)
_svc = {
service_id: ex.service_id,
monday: '0',
tuesday: '0',
wednesday: '0',
thursday: '0',
friday: '0',
saturday: '0',
sunday: '0',
start_date: null, end_date: null,
}
}
if (s !== NONE) {
checkServicesSorting(s)
_svc = s
}
if (_svc.service_id !== svc.service_id) {
// todo [breaking]: remove serviceId (idx 0), move svc first
if (dates.length > 0) {
if (svc.start_date === null) svc.start_date = dates[0]
if (svc.end_date === null) svc.end_date = dates[dates.length - 1]
yield [svc.service_id, dates, svc, nrOfDates, removedDates]
}
svc = _svc
if (s !== NONE) {
const wdm = exposeStats ? weekdaysMap : null
dates = datesBetween(
s.start_date, s.end_date,
{
monday: s.monday === '1',
tuesday: s.tuesday === '1',
wednesday: s.wednesday === '1',
thursday: s.thursday === '1',
friday: s.friday === '1',
saturday: s.saturday === '1',
sunday: s.sunday === '1',
},
timezone,
wdm,
)
} else {
dates = []
}
if (exposeStats) {
nrOfDates = new Array(7).fill(0)
for (const date of dates) {
nrOfDates[weekdayOf(date)]++
}
removedDates = []
}
}
if (ex !== NONE) {
checkServicesSorting(ex)
const date = parseDate(ex.date)
if (ex.exception_type === REMOVED) {
const i = arrEq(dates, date)
if (i >= 0) {
dates.splice(i, 1) // delete
if (exposeStats) {
nrOfDates[weekdayOf(date)]--
removedDates.push(date)
}
}
} else if (ex.exception_type === ADDED) {
if (!arrHas(dates, date)) {
arrInsert(dates, date)
if (exposeStats) {
nrOfDates[weekdayOf(date)]++
}
}
} // todo: else emit error
}
}
// todo [breaking]: remove serviceId (idx 0), move svc first
if (dates.length > 0) {
if (svc.start_date === null) svc.start_date = dates[0]
if (svc.end_date === null) svc.end_date = dates[dates.length - 1]
yield [svc.service_id, dates, svc, nrOfDates, removedDates]
}
}
module.exports = readServicesAndExceptions