mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
code style 👕
- rename variables - more consistent indentation - give crucial anonymous functions a name - add more TODOs
This commit is contained in:
parent
e0d9c28ef2
commit
fb43a4e43b
8 changed files with 93 additions and 82 deletions
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
const moment = require('moment-timezone')
|
const moment = require('moment-timezone')
|
||||||
|
|
||||||
const parseDateTime = (tz, date, time) => {
|
const parseDateTime = (timezone, date, time) => {
|
||||||
let offset = 0 // in days
|
let offset = 0 // in days
|
||||||
if (time.length > 6) {
|
if (time.length > 6) {
|
||||||
offset = +time.slice(0, -6)
|
offset = +time.slice(0, -6)
|
||||||
time = time.slice(-6)
|
time = time.slice(-6)
|
||||||
}
|
}
|
||||||
|
|
||||||
return moment.tz(date + 'T' + time, tz)
|
return moment.tz(date + 'T' + time, timezone)
|
||||||
.add(offset, 'days')
|
.add(offset, 'days')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,27 +8,28 @@ const parseDateTime = require('./date-time')
|
||||||
// todo: what is d.jny.dirFlg?
|
// todo: what is d.jny.dirFlg?
|
||||||
// todo: d.stbStop.dProgType
|
// todo: d.stbStop.dProgType
|
||||||
|
|
||||||
// tz = timezone, s = stations, ln = lines, r = remarks
|
const createParseDeparture = (timezone, stations, lines, remarks) => {
|
||||||
const createParseDeparture = (tz, s, ln, r) => {
|
const findRemark = rm => remarks[parseInt(rm.remX)] || null
|
||||||
|
|
||||||
const parseDeparture = (d) => {
|
const parseDeparture = (d) => {
|
||||||
const when = parseDateTime(tz, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS)
|
const when = parseDateTime(timezone, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS)
|
||||||
const result = {
|
const res = {
|
||||||
ref: d.jid
|
ref: d.jid,
|
||||||
, station: s[parseInt(d.stbStop.locX)]
|
station: stations[parseInt(d.stbStop.locX)], // todo: default to null
|
||||||
, when: when.format()
|
when: when.format(),
|
||||||
, direction: d.dirTxt
|
direction: d.dirTxt,
|
||||||
, line: ln[parseInt(d.prodX)]
|
line: lines[parseInt(d.prodX)], // todo: default to null
|
||||||
, remarks: d.remL ? d.remL.map((rm) => r[parseInt(rm.remX)]) : null
|
remarks: d.remL ? d.remL.map(findRemark) : [],
|
||||||
, trip: +d.jid.split('|')[1]
|
trip: +d.jid.split('|')[1] // todo: this seems brittle
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d.stbStop.dTimeR && d.stbStop.dTimeS) {
|
if (d.stbStop.dTimeR && d.stbStop.dTimeS) {
|
||||||
const realtime = parseDateTime(tz, d.date, d.stbStop.dTimeR)
|
const realtime = parseDateTime(timezone, d.date, d.stbStop.dTimeR)
|
||||||
const planned = parseDateTime(tz, d.date, d.stbStop.dTimeS)
|
const planned = parseDateTime(timezone, d.date, d.stbStop.dTimeS)
|
||||||
result.delay = Math.round((realtime - planned) / 1000)
|
res.delay = Math.round((realtime - planned) / 1000)
|
||||||
} else result.delay = null
|
} else res.delay = null
|
||||||
|
|
||||||
return result
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
return parseDeparture
|
return parseDeparture
|
||||||
|
|
|
@ -2,18 +2,19 @@
|
||||||
|
|
||||||
const parseDateTime = require('./date-time')
|
const parseDateTime = require('./date-time')
|
||||||
|
|
||||||
// s = stations, ln = lines, r = remarks, c = connection
|
const clone = obj => Object.assign({}, obj)
|
||||||
const createParseStopover = (tz, s, ln, r, c) => {
|
|
||||||
|
const createParseStopover = (tz, stations, lines, remarks, j) => { // j = journey
|
||||||
const parseStopover = (st) => {
|
const parseStopover = (st) => {
|
||||||
const res = {
|
const res = {
|
||||||
station: s[parseInt(st.locX)]
|
station: stations[parseInt(st.locX)]
|
||||||
}
|
}
|
||||||
if (st.aTimeR || st.aTimeS) {
|
if (st.aTimeR || st.aTimeS) {
|
||||||
const arr = parseDateTime(tz, c.date, st.aTimeR || st.aTimeS)
|
const arr = parseDateTime(tz, j.date, st.aTimeR || st.aTimeS)
|
||||||
res.arrival = arr.format()
|
res.arrival = arr.format()
|
||||||
}
|
}
|
||||||
if (st.dTimeR || st.dTimeS) {
|
if (st.dTimeR || st.dTimeS) {
|
||||||
const dep = parseDateTime(tz, c.date, st.dTimeR || st.dTimeS)
|
const dep = parseDateTime(tz, j.date, st.dTimeR || st.dTimeS)
|
||||||
res.departure = dep.format()
|
res.departure = dep.format()
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
|
@ -22,32 +23,31 @@ const createParseStopover = (tz, s, ln, r, c) => {
|
||||||
return parseStopover
|
return parseStopover
|
||||||
}
|
}
|
||||||
|
|
||||||
// s = stations, ln = lines, r = remarks, c = connection
|
const createApplyRemark = (stations, lines, remarks, j) => { // j = journey
|
||||||
const createApplyRemark = (s, ln, r, c) => {
|
|
||||||
// todo: finish parse/remark.js first
|
// todo: finish parse/remark.js first
|
||||||
const applyRemark = (rm) => {}
|
const applyRemark = (rm) => {}
|
||||||
return applyRemark
|
return applyRemark
|
||||||
}
|
}
|
||||||
|
|
||||||
// tz = timezone, s = stations, ln = lines, r = remarks, c = connection
|
const createParsePart = (tz, stations, lines, remarks, j) => { // j = journey
|
||||||
const createParsePart = (tz, s, ln, r, c) => {
|
|
||||||
// todo: pt.sDays
|
// todo: pt.sDays
|
||||||
// todo: pt.dep.dProgType, pt.arr.dProgType
|
// todo: pt.dep.dProgType, pt.arr.dProgType
|
||||||
// todo: what is pt.jny.dirFlg?
|
// todo: what is pt.jny.dirFlg?
|
||||||
// todo: how does pt.freq work?
|
// todo: how does pt.freq work?
|
||||||
const parsePart = (pt) => {
|
const parsePart = (pt) => {
|
||||||
const dep = parseDateTime(tz, c.date, pt.dep.dTimeR || pt.dep.dTimeS)
|
const dep = parseDateTime(tz, j.date, pt.dep.dTimeR || pt.dep.dTimeS)
|
||||||
const arr = parseDateTime(tz, c.date, pt.arr.aTimeR || pt.arr.aTimeS)
|
const arr = parseDateTime(tz, j.date, pt.arr.aTimeR || pt.arr.aTimeS)
|
||||||
const res = {
|
const res = {
|
||||||
origin: Object.assign({}, s[parseInt(pt.dep.locX)]) // todo: what about null?
|
// todo: what about null?
|
||||||
, destination: Object.assign({}, s[parseInt(pt.arr.locX)]) // todo: what about null?
|
origin: clone(stations[parseInt(pt.dep.locX)]),
|
||||||
, departure: dep.format()
|
destination: clone(stations[parseInt(pt.arr.locX)]),
|
||||||
, arrival: dep.format()
|
departure: dep.format(),
|
||||||
|
arrival: arr.format()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pt.dep.dTimeR && pt.dep.dTimeS) {
|
if (pt.dep.dTimeR && pt.dep.dTimeS) {
|
||||||
const realtime = parseDateTime(tz, c.date, pt.dep.dTimeR)
|
const realtime = parseDateTime(tz, j.date, pt.dep.dTimeR)
|
||||||
const planned = parseDateTime(tz, c.date, pt.dep.dTimeS)
|
const planned = parseDateTime(tz, j.date, pt.dep.dTimeS)
|
||||||
res.delay = Math.round((realtime - planned) / 1000)
|
res.delay = Math.round((realtime - planned) / 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,26 +55,27 @@ const createParsePart = (tz, s, ln, r, c) => {
|
||||||
res.mode = 'walking'
|
res.mode = 'walking'
|
||||||
} else if (pt.type === 'JNY') {
|
} else if (pt.type === 'JNY') {
|
||||||
res.id = pt.jny.jid
|
res.id = pt.jny.jid
|
||||||
res.line = ln[parseInt(pt.jny.prodX)] // todo: default null
|
res.line = lines[parseInt(pt.jny.prodX)] // todo: default null
|
||||||
res.direction = pt.jny.dirTxt // todo: parse this
|
res.direction = pt.jny.dirTxt // todo: parse this
|
||||||
|
|
||||||
if (pt.dep.dPlatfS) res.departurePlatform = pt.dep.dPlatfS
|
if (pt.dep.dPlatfS) res.departurePlatform = pt.dep.dPlatfS
|
||||||
if (pt.arr.aPlatfS) res.arrivalPlatform = pt.arr.aPlatfS
|
if (pt.arr.aPlatfS) res.arrivalPlatform = pt.arr.aPlatfS
|
||||||
|
|
||||||
if (pt.jny.stopL) {
|
if (pt.jny.stopL) {
|
||||||
res.passed = pt.jny.stopL.map(createParseStopover(tz, s, ln, r, c))
|
const parseStopover = createParseStopover(tz, stations, lines, remarks, j)
|
||||||
|
res.passed = pt.jny.stopL.map(parseStopover)
|
||||||
}
|
}
|
||||||
if (Array.isArray(pt.jny.remL)) {
|
if (Array.isArray(pt.jny.remL)) {
|
||||||
pt.jny.remL.forEach(createApplyRemark(s, ln, r, c))
|
pt.jny.remL.forEach(createApplyRemark(stations, lines, remarks, j))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pt.jny.freq && pt.jny.freq.jnyL) {
|
if (pt.jny.freq && pt.jny.freq.jnyL) {
|
||||||
const parseAlternative = (a) => ({
|
const parseAlternative = (a) => ({
|
||||||
line: ln[parseInt(a.prodX)], // todo: default null
|
line: lines[parseInt(a.prodX)], // todo: default null
|
||||||
when: parseDateTime(tz, c.date, a.stopL[0].dTimeS).format() // todo: realtime
|
when: parseDateTime(tz, j.date, a.stopL[0].dTimeS).format() // todo: realtime
|
||||||
})
|
})
|
||||||
res.alternatives = pt.jny.freq.jnyL
|
res.alternatives = pt.jny.freq.jnyL
|
||||||
.filter((a) => a.stopL[0].locX === pt.dep.locX)
|
.filter(a => a.stopL[0].locX === pt.dep.locX)
|
||||||
.map(parseAlternative)
|
.map(parseAlternative)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,21 +85,20 @@ const createParsePart = (tz, s, ln, r, c) => {
|
||||||
return parsePart
|
return parsePart
|
||||||
}
|
}
|
||||||
|
|
||||||
// s = stations, ln = lines, r = remarks, p = createParsePart
|
const createParseJourney = (tz, stations, lines, remarks, p = createParsePart) => {
|
||||||
const createParseJourney = (tz, s, ln, r, p = createParsePart) => {
|
|
||||||
// todo: c.sDays
|
// todo: c.sDays
|
||||||
// todo: c.dep.dProgType, c.arr.dProgType
|
// todo: c.dep.dProgType, c.arr.dProgType
|
||||||
// todo: c.conSubscr
|
// todo: c.conSubscr
|
||||||
// todo: c.trfRes x vbb-parse-ticket
|
// todo: c.trfRes x vbb-parse-ticket
|
||||||
// todo: use computed information from part
|
// todo: use computed information from part
|
||||||
const parseJourney = (c) => {
|
const parseJourney = (journey) => {
|
||||||
const parts = c.secL.map(p(tz, s, ln, r, c))
|
const parts = journey.secL.map(p(tz, stations, lines, remarks, journey))
|
||||||
return {
|
return {
|
||||||
parts
|
parts,
|
||||||
, origin: parts[0].origin
|
origin: parts[0].origin,
|
||||||
, destination: parts[parts.length - 1].destination
|
destination: parts[parts.length - 1].destination,
|
||||||
, departure: parts[0].departure
|
departure: parts[0].departure,
|
||||||
, arrival: parts[parts.length - 1].arrival
|
arrival: parts[parts.length - 1].arrival
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,16 @@
|
||||||
// todo: what is p.icoX?
|
// todo: what is p.icoX?
|
||||||
// todo: what is p.oprX?
|
// todo: what is p.oprX?
|
||||||
const parseLine = (p) => {
|
const parseLine = (p) => {
|
||||||
if (!p) return null
|
if (!p) return null // todo: handle this upstream
|
||||||
|
const res = {type: 'line', name: p.line || p.name}
|
||||||
|
|
||||||
const result = {type: 'line', name: p.line || p.name}
|
if (p.cls) res.class = p.cls
|
||||||
if (p.cls) result.class = p.cls
|
|
||||||
if (p.prodCtx) {
|
if (p.prodCtx) {
|
||||||
result.productCode = +p.prodCtx.catCode
|
res.productCode = +p.prodCtx.catCode
|
||||||
result.productName = p.prodCtx.catOutS
|
res.productName = p.prodCtx.catOutS
|
||||||
}
|
}
|
||||||
return result
|
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = parseLine
|
module.exports = parseLine
|
||||||
|
|
|
@ -16,8 +16,10 @@ const parseLocation = (l) => {
|
||||||
longitude: l.crd.x / 1000000
|
longitude: l.crd.x / 1000000
|
||||||
} : null
|
} : null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'poi' || type === 'station') result.id = l.extId
|
if (type === 'poi' || type === 'station') result.id = l.extId
|
||||||
if ('pCls' in l) result.products = l.pCls
|
if ('pCls' in l) result.products = l.pCls
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
|
|
||||||
const parseDateTime = require('./date-time')
|
const parseDateTime = require('./date-time')
|
||||||
|
|
||||||
// tz = timezone, l = locations, ln = lines, r = remarks
|
const createParseMovement = (tz, locations, lines, remarks) => {
|
||||||
const createParseMovement = (tz, l, ln, r) => {
|
|
||||||
// todo: what is m.dirGeo? maybe the speed?
|
// todo: what is m.dirGeo? maybe the speed?
|
||||||
// todo: what is m.stopL?
|
// todo: what is m.stopL?
|
||||||
// todo: what is m.proc? wut?
|
// todo: what is m.proc? wut?
|
||||||
|
@ -12,30 +11,37 @@ const createParseMovement = (tz, l, ln, r) => {
|
||||||
// todo: what is m.ani.proc[n]? wut?
|
// todo: what is m.ani.proc[n]? wut?
|
||||||
// todo: how does m.ani.poly work?
|
// todo: how does m.ani.poly work?
|
||||||
const parseMovement = (m) => {
|
const parseMovement = (m) => {
|
||||||
|
const parseNextStop = (s) => {
|
||||||
|
const dep = s.dTimeR || s.dTimeS
|
||||||
|
? parseDateTime(tz, m.date, s.dTimeR || s.dTimeS)
|
||||||
|
: null
|
||||||
|
const arr = s.aTimeR || s.aTimeS
|
||||||
|
? parseDateTime(tz, m.date, s.aTimeR || s.aTimeS)
|
||||||
|
: null
|
||||||
|
|
||||||
|
return {
|
||||||
|
station: locations[s.locX],
|
||||||
|
departure: dep ? dep.format() : null,
|
||||||
|
arrival: arr ? arr.format() : null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const res = {
|
const res = {
|
||||||
direction: m.dirTxt
|
direction: m.dirTxt,
|
||||||
, line: ln[m.prodX]
|
line: lines[m.prodX], // default to null
|
||||||
, coordinates: m.pos ? {
|
coordinates: m.pos ? {
|
||||||
latitude: m.pos.y / 1000000,
|
latitude: m.pos.y / 1000000,
|
||||||
longitude: m.pos.x / 1000000
|
longitude: m.pos.x / 1000000
|
||||||
} : null
|
} : null,
|
||||||
, nextStops: m.stopL.map((s) => ({
|
nextStops: m.stopL.map(parseNextStop),
|
||||||
station: l[s.locX]
|
frames: []
|
||||||
, departure: s.dTimeR || s.dTimeS
|
|
||||||
? parseDateTime(tz, m.date, s.dTimeR || s.dTimeS).format()
|
|
||||||
: null
|
|
||||||
, arrival: s.aTimeR || s.aTimeS
|
|
||||||
? parseDateTime(tz, m.date, s.aTimeR || s.aTimeS).format()
|
|
||||||
: null
|
|
||||||
}))
|
|
||||||
, frames: []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m.ani && Array.isArray(m.ani.mSec)) {
|
if (m.ani && Array.isArray(m.ani.mSec)) {
|
||||||
for (let i = 0; i < m.ani.mSec.length; i++) {
|
for (let i = 0; i < m.ani.mSec.length; i++) {
|
||||||
res.frames.push({
|
res.frames.push({
|
||||||
origin: l[m.ani.fLocX[i]],
|
origin: locations[m.ani.fLocX[i]], // todo: default to null
|
||||||
destination: l[m.ani.tLocX[i]],
|
destination: locations[m.ani.tLocX[i]], // todo: default to null
|
||||||
t: m.ani.mSec[i]
|
t: m.ani.mSec[i]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,9 @@ const parseLocation = require('./location')
|
||||||
// todo: what is s.wt?
|
// todo: what is s.wt?
|
||||||
// todo: what is s.dur?
|
// todo: what is s.dur?
|
||||||
const parseNearby = (n) => {
|
const parseNearby = (n) => {
|
||||||
const result = location(n)
|
const res = parseLocation(n)
|
||||||
result.distance = n.dist
|
res.distance = n.dist
|
||||||
return result
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = parseNearby
|
module.exports = parseNearby
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
// s = stations, ln = lines, r = remarks, c = connection
|
const parseDateTime = require('./date-time')
|
||||||
const createParseStopover = (tz, s, ln, r, c) => {
|
|
||||||
|
const createParseStopover = (tz, stations, lines, remarks, connection) => {
|
||||||
const parseStopover = (st) => {
|
const parseStopover = (st) => {
|
||||||
const res = {
|
const res = {
|
||||||
station: s[parseInt(st.locX)]
|
station: stations[parseInt(st.locX)] // default to null
|
||||||
}
|
}
|
||||||
if (st.aTimeR || st.aTimeS) {
|
if (st.aTimeR || st.aTimeS) {
|
||||||
const arr = parseDateTime(tz, c.date, st.aTimeR || st.aTimeS)
|
const arr = parseDateTime(tz, connection.date, st.aTimeR || st.aTimeS)
|
||||||
res.arrival = arr.format()
|
res.arrival = arr.format()
|
||||||
}
|
}
|
||||||
if (st.dTimeR || st.dTimeS) {
|
if (st.dTimeR || st.dTimeS) {
|
||||||
const dep = parseDateTime(tz, c.date, st.dTimeR || st.dTimeS)
|
const dep = parseDateTime(tz, connection.date, st.dTimeR || st.dTimeS)
|
||||||
res.departure = dep.format()
|
res.departure = dep.format()
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
|
|
Loading…
Add table
Reference in a new issue