-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
67 lines (54 loc) · 1.6 KB
/
index.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
import {parseHook} from 'hafas-client/lib/profile-hooks.js'
import {createClient} from 'hafas-client'
import {profile as vbbProfile} from 'hafas-client/p/vbb/index.js'
import colors from 'vbb-line-colors'
import {addTransferInfoToJourney} from './lib/add-transfer-info.js'
const {parseLine: _parseLine} = vbbProfile
const parseLineWithColor = ({parsed}, l) => {
const {product, name} = parsed
const c = colors[product] && colors[product][name]
if (c) parsed.color = c
return parsed
}
const customVbbProfile = {
...vbbProfile,
parseLine: parseHook(_parseLine, parseLineWithColor)
}
const defaults = {
profile: customVbbProfile
}
const createVbbHafas = (userAgent, opt = {}) => {
const {profile} = {...defaults, ...opt}
const hafas = createClient(profile, userAgent)
const origJourneys = hafas.journeys
const journeysWithTransfers = (from, to, opt = {}) => {
if (opt && opt.transferInfo) opt.stopovers = true
const p = origJourneys(from, to, opt)
if (opt && opt.transferInfo) {
return p.then((res) => {
for (let journey of res.journeys) addTransferInfoToJourney(journey)
return res
})
}
return p
}
hafas.journeys = journeysWithTransfers
const origRefreshJourney = hafas.refreshJourney
const refreshJourneyWithTransfers = (refreshToken, opt = {}) => {
if (opt && opt.transferInfo) opt.stopovers = true
const p = origRefreshJourney(refreshToken, opt)
if (opt && opt.transferInfo) {
return p.then(j => {
addTransferInfoToJourney(j)
return j
})
}
return p
}
hafas.refreshJourney = refreshJourneyWithTransfers
return hafas
}
export {
defaults,
createVbbHafas,
}