code style 👕

- rename variables
- more consistent indentation
- give crucial anonymous functions a name
- add more TODOs
This commit is contained in:
Jannis R 2017-11-11 23:56:09 +01:00
parent e0d9c28ef2
commit fb43a4e43b
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
8 changed files with 93 additions and 82 deletions

View file

@ -2,14 +2,14 @@
const moment = require('moment-timezone')
const parseDateTime = (tz, date, time) => {
const parseDateTime = (timezone, date, time) => {
let offset = 0 // in days
if (time.length > 6) {
offset = +time.slice(0, -6)
time = time.slice(-6)
}
return moment.tz(date + 'T' + time, tz)
return moment.tz(date + 'T' + time, timezone)
.add(offset, 'days')
}

View file

@ -8,27 +8,28 @@ const parseDateTime = require('./date-time')
// todo: what is d.jny.dirFlg?
// todo: d.stbStop.dProgType
// tz = timezone, s = stations, ln = lines, r = remarks
const createParseDeparture = (tz, s, ln, r) => {
const createParseDeparture = (timezone, stations, lines, remarks) => {
const findRemark = rm => remarks[parseInt(rm.remX)] || null
const parseDeparture = (d) => {
const when = parseDateTime(tz, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS)
const result = {
ref: d.jid
, station: s[parseInt(d.stbStop.locX)]
, when: when.format()
, direction: d.dirTxt
, line: ln[parseInt(d.prodX)]
, remarks: d.remL ? d.remL.map((rm) => r[parseInt(rm.remX)]) : null
, trip: +d.jid.split('|')[1]
const when = parseDateTime(timezone, d.date, d.stbStop.dTimeR || d.stbStop.dTimeS)
const res = {
ref: d.jid,
station: stations[parseInt(d.stbStop.locX)], // todo: default to null
when: when.format(),
direction: d.dirTxt,
line: lines[parseInt(d.prodX)], // todo: default to null
remarks: d.remL ? d.remL.map(findRemark) : [],
trip: +d.jid.split('|')[1] // todo: this seems brittle
}
if (d.stbStop.dTimeR && d.stbStop.dTimeS) {
const realtime = parseDateTime(tz, d.date, d.stbStop.dTimeR)
const planned = parseDateTime(tz, d.date, d.stbStop.dTimeS)
result.delay = Math.round((realtime - planned) / 1000)
} else result.delay = null
const realtime = parseDateTime(timezone, d.date, d.stbStop.dTimeR)
const planned = parseDateTime(timezone, d.date, d.stbStop.dTimeS)
res.delay = Math.round((realtime - planned) / 1000)
} else res.delay = null
return result
return res
}
return parseDeparture

View file

@ -2,18 +2,19 @@
const parseDateTime = require('./date-time')
// s = stations, ln = lines, r = remarks, c = connection
const createParseStopover = (tz, s, ln, r, c) => {
const clone = obj => Object.assign({}, obj)
const createParseStopover = (tz, stations, lines, remarks, j) => { // j = journey
const parseStopover = (st) => {
const res = {
station: s[parseInt(st.locX)]
station: stations[parseInt(st.locX)]
}
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()
}
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()
}
return res
@ -22,32 +23,31 @@ const createParseStopover = (tz, s, ln, r, c) => {
return parseStopover
}
// s = stations, ln = lines, r = remarks, c = connection
const createApplyRemark = (s, ln, r, c) => {
const createApplyRemark = (stations, lines, remarks, j) => { // j = journey
// todo: finish parse/remark.js first
const applyRemark = (rm) => {}
return applyRemark
}
// tz = timezone, s = stations, ln = lines, r = remarks, c = connection
const createParsePart = (tz, s, ln, r, c) => {
const createParsePart = (tz, stations, lines, remarks, j) => { // j = journey
// todo: pt.sDays
// todo: pt.dep.dProgType, pt.arr.dProgType
// todo: what is pt.jny.dirFlg?
// todo: how does pt.freq work?
const parsePart = (pt) => {
const dep = parseDateTime(tz, c.date, pt.dep.dTimeR || pt.dep.dTimeS)
const arr = parseDateTime(tz, c.date, pt.arr.aTimeR || pt.arr.aTimeS)
const dep = parseDateTime(tz, j.date, pt.dep.dTimeR || pt.dep.dTimeS)
const arr = parseDateTime(tz, j.date, pt.arr.aTimeR || pt.arr.aTimeS)
const res = {
origin: Object.assign({}, s[parseInt(pt.dep.locX)]) // todo: what about null?
, destination: Object.assign({}, s[parseInt(pt.arr.locX)]) // todo: what about null?
, departure: dep.format()
, arrival: dep.format()
// todo: what about null?
origin: clone(stations[parseInt(pt.dep.locX)]),
destination: clone(stations[parseInt(pt.arr.locX)]),
departure: dep.format(),
arrival: arr.format()
}
if (pt.dep.dTimeR && pt.dep.dTimeS) {
const realtime = parseDateTime(tz, c.date, pt.dep.dTimeR)
const planned = parseDateTime(tz, c.date, pt.dep.dTimeS)
const realtime = parseDateTime(tz, j.date, pt.dep.dTimeR)
const planned = parseDateTime(tz, j.date, pt.dep.dTimeS)
res.delay = Math.round((realtime - planned) / 1000)
}
@ -55,26 +55,27 @@ const createParsePart = (tz, s, ln, r, c) => {
res.mode = 'walking'
} else if (pt.type === 'JNY') {
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
if (pt.dep.dPlatfS) res.departurePlatform = pt.dep.dPlatfS
if (pt.arr.aPlatfS) res.arrivalPlatform = pt.arr.aPlatfS
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)) {
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) {
const parseAlternative = (a) => ({
line: ln[parseInt(a.prodX)], // todo: default null
when: parseDateTime(tz, c.date, a.stopL[0].dTimeS).format() // todo: realtime
line: lines[parseInt(a.prodX)], // todo: default null
when: parseDateTime(tz, j.date, a.stopL[0].dTimeS).format() // todo: realtime
})
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)
}
}
@ -84,21 +85,20 @@ const createParsePart = (tz, s, ln, r, c) => {
return parsePart
}
// s = stations, ln = lines, r = remarks, p = createParsePart
const createParseJourney = (tz, s, ln, r, p = createParsePart) => {
const createParseJourney = (tz, stations, lines, remarks, p = createParsePart) => {
// todo: c.sDays
// todo: c.dep.dProgType, c.arr.dProgType
// todo: c.conSubscr
// todo: c.trfRes x vbb-parse-ticket
// todo: use computed information from part
const parseJourney = (c) => {
const parts = c.secL.map(p(tz, s, ln, r, c))
const parseJourney = (journey) => {
const parts = journey.secL.map(p(tz, stations, lines, remarks, journey))
return {
parts
, origin: parts[0].origin
, destination: parts[parts.length - 1].destination
, departure: parts[0].departure
, arrival: parts[parts.length - 1].arrival
parts,
origin: parts[0].origin,
destination: parts[parts.length - 1].destination,
departure: parts[0].departure,
arrival: parts[parts.length - 1].arrival
}
}

View file

@ -4,15 +4,16 @@
// todo: what is p.icoX?
// todo: what is p.oprX?
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) result.class = p.cls
if (p.cls) res.class = p.cls
if (p.prodCtx) {
result.productCode = +p.prodCtx.catCode
result.productName = p.prodCtx.catOutS
res.productCode = +p.prodCtx.catCode
res.productName = p.prodCtx.catOutS
}
return result
return res
}
module.exports = parseLine

View file

@ -16,8 +16,10 @@ const parseLocation = (l) => {
longitude: l.crd.x / 1000000
} : null
}
if (type === 'poi' || type === 'station') result.id = l.extId
if ('pCls' in l) result.products = l.pCls
return result
}

View file

@ -2,8 +2,7 @@
const parseDateTime = require('./date-time')
// tz = timezone, l = locations, ln = lines, r = remarks
const createParseMovement = (tz, l, ln, r) => {
const createParseMovement = (tz, locations, lines, remarks) => {
// todo: what is m.dirGeo? maybe the speed?
// todo: what is m.stopL?
// 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: how does m.ani.poly work?
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 = {
direction: m.dirTxt
, line: ln[m.prodX]
, coordinates: m.pos ? {
direction: m.dirTxt,
line: lines[m.prodX], // default to null
coordinates: m.pos ? {
latitude: m.pos.y / 1000000,
longitude: m.pos.x / 1000000
} : null
, nextStops: m.stopL.map((s) => ({
station: l[s.locX]
, 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: []
} : null,
nextStops: m.stopL.map(parseNextStop),
frames: []
}
if (m.ani && Array.isArray(m.ani.mSec)) {
for (let i = 0; i < m.ani.mSec.length; i++) {
res.frames.push({
origin: l[m.ani.fLocX[i]],
destination: l[m.ani.tLocX[i]],
origin: locations[m.ani.fLocX[i]], // todo: default to null
destination: locations[m.ani.tLocX[i]], // todo: default to null
t: m.ani.mSec[i]
})
}

View file

@ -8,9 +8,9 @@ const parseLocation = require('./location')
// todo: what is s.wt?
// todo: what is s.dur?
const parseNearby = (n) => {
const result = location(n)
result.distance = n.dist
return result
const res = parseLocation(n)
res.distance = n.dist
return res
}
module.exports = parseNearby

View file

@ -1,17 +1,18 @@
'use strict'
// s = stations, ln = lines, r = remarks, c = connection
const createParseStopover = (tz, s, ln, r, c) => {
const parseDateTime = require('./date-time')
const createParseStopover = (tz, stations, lines, remarks, connection) => {
const parseStopover = (st) => {
const res = {
station: s[parseInt(st.locX)]
station: stations[parseInt(st.locX)] // default to null
}
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()
}
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()
}
return res