-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrival-departure.js
76 lines (69 loc) · 2.13 KB
/
arrival-departure.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
import {matrix} from './lib/helpers.js'
import {versionedId, idWithoutVersion as unversionedId} from './lib/versioned-id.js'
// todo: use these IDs without version prefix
const createGetStableArrivalDepartureIds = (stopIds, tripIds, routeIds, lineIds, normalizePlatform) => (type, _) => {
const when = Math.round(Date.parse(_.plannedWhen) / 1000)
const platform = _.plannedPlatform
? normalizePlatform(_.plannedPlatform, _)
: null
// todo: arr time
const byStopAndTrip = matrix(stopIds, tripIds)
.map(([[stopId, stopSpecif], [tripId, tripSpecif]]) => [
versionedId([
type.slice(0, 3),
unversionedId(stopId),
unversionedId(tripId),
].join(':')),
stopSpecif + tripSpecif + 20,
])
// This assumes that there are no two vehicles
// - running for the same route,
// - stopping at the same stop/station,
// - at the same time.
const fuzzyByRoute = !Number.isNaN(when)
? matrix(stopIds, routeIds).map(([[stopId, stopSpecif], [routeId, routeSpecif]]) => [
versionedId([
type.slice(0, 3),
unversionedId(stopId),
unversionedId(routeId),
when,
].join(':')),
stopSpecif + routeSpecif + 30,
])
: []
// This assumes that there are no two vehicles
// - running for the same line,
// - stopping at the same stop/station,
// - at the same platform,
// - at the same time.
const fuzzyByLine = !Number.isNaN(when) && platform
? matrix(stopIds, lineIds).map(([[stopId, stopSpecif], [lineId, lineSpecif]]) => [
versionedId([
type.slice(0, 3),
unversionedId(stopId),
unversionedId(lineId),
when,
platform,
].join(':')),
stopSpecif + lineSpecif + 30,
])
: []
return [
...byStopAndTrip,
...fuzzyByRoute,
...fuzzyByLine
]
}
const createGetStableArrivalIds = (...args) => {
const getStableArrivalIds = createGetStableArrivalDepartureIds(...args).bind(null, 'arrival')
return getStableArrivalIds
}
const createGetStableDepartureIds = (...args) => {
const getStableDepartureIds = createGetStableArrivalDepartureIds(...args).bind(null, 'departure')
return getStableDepartureIds
}
export {
createGetStableArrivalDepartureIds,
createGetStableArrivalIds,
createGetStableDepartureIds,
}