-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparse.js
100 lines (87 loc) · 2.12 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
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
'use strict'
const moment = require('moment-timezone')
const slugg = require('slugg')
const minBy = require('lodash.minby')
const price = (string) => parseFloat(string.replace(',', '.'))
const offer = (data, notes) => {
const offer = {
// todo: sel, t, c, arq, ff, aix, risids
name: null,
description: null,
discount: data.tt === 'SP', // are there others than SP & NP?
price: price(data.p),
routes: data.sids,
anyTrain: data.zb !== 'Y'
}
if (notes[data.pky]) Object.assign(offer, notes[data.pky])
return offer
}
const when = (data) => {
// todo: timezone
return moment.tz(parseInt(data.m), 'Europe/Berlin').toISOString()
}
// see https://github.com/public-transport/friendly-public-transport-format/blob/5ba55e2/docs/readme.md#modes
const modesByProduct = {
S: 'train',
IC: 'train',
ICE: 'train',
RE: 'train',
VBG: 'train', // http://www.laenderbahn.com/vogtlandbahn/
ALX: 'train' // http://www.laenderbahn.com/alex
}
const leg = (data) => ({
// todo: rp, re, sp
origin: {
type: 'station',
id: data.s,
name: data.sn
// todo: coordinates
},
start: when(data.dep),
departurePlatform: data.pd || null,
destination: {
type: 'station',
id: data.d,
name: data.dn
// todo: coordinates
},
end: when(data.arr),
arrivalPlatform: data.pa || null,
line: {
type: 'line',
id: slugg(data.tn),
name: data.tn,
mode: modesByProduct[data.eg] || null,
product: data.eg
}
})
const journey = (_, offers) => {
const legs = _.trains.map(leg)
let offer = minBy(offers.filter((o) => o.routes.includes(_.sid)), (o) => o.price)
const price = offer ? {
currency: 'EUR',
amount: offer.price,
discount: offer.discount,
name: offer.name,
description: offer.description,
anyTrain: offer.anyTrain
} : null
return {
// todo: sel, dir
type: 'journey',
id: _.sid,
origin: legs[0].origin,
destination: legs[legs.length - 1].destination,
legs,
price,
nightTrain: _.NZVerb // todo: why here?
}
}
const notes = (data) => {
const notes = {}
for (let ref in data) {
notes[ref] = {name: data[ref].name, description: data[ref].hinweis}
}
return notes
}
module.exports = {notes, offer, journey}