-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
53 lines (44 loc) · 1.25 KB
/
parse.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
var _ = require('lodash')
var moment = require('moment-timezone')
const CAT_CODES = {
2: 'Train',
9: 'Bus',
10: 'Tram'
}
function board (resp) {
resp = _.get(resp, 'svcResL[0].res')
var departures = resp.jnyL
var contexts = resp.common.prodL
var locations = resp.common.locL
return _.map(departures, dep => {
// get the "Context" (train, tram, bus, etc.)
var ctx = contexts[parseInt(dep.stbStop.dProdX, 10)]
// get the departure / arrival location
var loc = locations[parseInt(dep.stbStop.locX, 10)]
// get the transport type
var type = CAT_CODES[parseInt(ctx.prodCtx.catCode, 10)]
var leaves = moment(dep.stbStop.dTimeS, 'HHmmss')
var withDelay = moment(dep.stbStop.dTimeR, 'HHmmss')
// delay van only be calculated for train traffic,
// other transport will produce 'null'
var delay = withDelay.diff(leaves, 'minutes') || 0
return {
to: dep.dirTxt,
delay: delay,
leaves: leaves,
leaves_relative: leaves.fromNow(),
departs: type === 'Train'
? `Platform ${dep.stbStop.dPlatfS}`
: loc.name,
context: {
name: ctx.name,
number: ctx.number,
line: ctx.line,
type: type
}
}
})
}
module.exports = {
board: board
}