parse fns: individual params -> data object 💥

From now on, all higher-level parse fns will be able to access more
of the response data while keeping their signature.

- parseLine
- parseDeparture
- parseJourney
- parseJourneyLeg
- parseLocation
- parseMovement
- parsePolyline
- parseNearby
- parseStopover
This commit is contained in:
Jannis R 2018-06-13 18:59:56 +02:00
parent 47155cdb83
commit 8881d8a1a4
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
16 changed files with 76 additions and 54 deletions

View file

@ -37,8 +37,8 @@ Assuming the endpoint returns all lines names prefixed with `foo `, We can strip
// get the default line parser // get the default line parser
const createParseLine = require('hafas-client/parse/line') const createParseLine = require('hafas-client/parse/line')
const createParseLineWithoutFoo = (profile, operators) => { const createParseLineWithoutFoo = (profile, data) => {
const parseLine = createParseLine(profile, operators) const parseLine = createParseLine(profile, data)
// wrapper function with additional logic // wrapper function with additional logic
const parseLineWithoutFoo = (l) => { const parseLineWithoutFoo = (l) => {

View file

@ -51,7 +51,11 @@ const createClient = (profile, request = _request) => {
}) })
.then((d) => { .then((d) => {
if (!Array.isArray(d.jnyL)) return [] // todo: throw err? if (!Array.isArray(d.jnyL)) return [] // todo: throw err?
const parse = profile.parseDeparture(profile, d.locations, d.lines, d.remarks) const parse = profile.parseDeparture(profile, {
locations: d.locations,
lines: d.lines,
remarks: d.remarks
})
return d.jnyL.map(parse) return d.jnyL.map(parse)
.sort((a, b) => new Date(a.when) - new Date(b.when)) .sort((a, b) => new Date(a.when) - new Date(b.when))
}) })
@ -163,8 +167,12 @@ const createClient = (profile, request = _request) => {
.then((d) => { .then((d) => {
if (!Array.isArray(d.outConL)) return [] if (!Array.isArray(d.outConL)) return []
const polylines = opt.polylines && d.common.polyL || [] const parse = profile.parseJourney(profile, {
const parse = profile.parseJourney(profile, d.locations, d.lines, d.remarks, polylines) locations: d.locations,
lines: d.lines,
remarks: d.remarks,
polylines: opt.polylines && d.common.polyL || []
})
if (!journeys.earlierRef) journeys.earlierRef = d.outCtxScrB if (!journeys.earlierRef) journeys.earlierRef = d.outCtxScrB
@ -217,7 +225,7 @@ const createClient = (profile, request = _request) => {
.then((d) => { .then((d) => {
if (!d.match || !Array.isArray(d.match.locL)) return [] if (!d.match || !Array.isArray(d.match.locL)) return []
const parse = profile.parseLocation const parse = profile.parseLocation
return d.match.locL.map(loc => parse(profile, loc, d.lines)) return d.match.locL.map(loc => parse(profile, {lines: d.lines}, loc))
}) })
} }
@ -237,7 +245,7 @@ const createClient = (profile, request = _request) => {
// todo: proper stack trace? // todo: proper stack trace?
throw new Error('invalid response') throw new Error('invalid response')
} }
return profile.parseLocation(profile, d.locL[0], d.lines) return profile.parseLocation(profile, {lines: d.lines}, d.locL[0])
}) })
} }
@ -309,8 +317,12 @@ const createClient = (profile, request = _request) => {
} }
}) })
.then((d) => { .then((d) => {
const polylines = opt.polyline && d.common.polyL || [] const parse = profile.parseJourneyLeg(profile, {
const parse = profile.parseJourneyLeg(profile, d.locations, d.lines, d.remarks, polylines) locations: d.locations,
lines: d.lines,
remarks: d.remarks,
polylines: opt.polyline && d.common.polyL || []
})
const leg = { // pretend the leg is contained in a journey const leg = { // pretend the leg is contained in a journey
type: 'JNY', type: 'JNY',
@ -362,8 +374,12 @@ const createClient = (profile, request = _request) => {
.then((d) => { .then((d) => {
if (!Array.isArray(d.jnyL)) return [] if (!Array.isArray(d.jnyL)) return []
const polylines = opt.polyline && d.common.polyL || [] const parse = profile.parseMovement(profile, {
const parse = profile.parseMovement(profile, d.locations, d.lines, d.remarks, polylines) locations: d.locations,
lines: d.lines,
remarks: d.remarks,
polylines: opt.polyline && d.common.polyL || []
})
return d.jnyL.map(parse) return d.jnyL.map(parse)
}) })
} }

View file

@ -88,11 +88,13 @@ const request = (profile, data) => {
d.operators = c.opL.map(op => profile.parseOperator(profile, op)) d.operators = c.opL.map(op => profile.parseOperator(profile, op))
} }
if (Array.isArray(c.prodL)) { if (Array.isArray(c.prodL)) {
const parse = profile.parseLine(profile, d.operators) const parse = profile.parseLine(profile, {
operators: d.operators
})
d.lines = c.prodL.map(parse) d.lines = c.prodL.map(parse)
} }
if (Array.isArray(c.locL)) { if (Array.isArray(c.locL)) {
const parse = loc => profile.parseLocation(profile, loc, d.lines) const parse = loc => profile.parseLocation(profile, {lines: d.lines}, loc)
d.locations = c.locL.map(parse) d.locations = c.locL.map(parse)
} }
return d return d

View file

@ -34,8 +34,8 @@ const transformJourneysQuery = (query, opt) => {
return query return query
} }
const createParseJourney = (profile, stations, lines, remarks, polylines) => { const createParseJourney = (profile, data) => {
const parseJourney = _createParseJourney(profile, stations, lines, remarks, polylines) const parseJourney = _createParseJourney(profile, data)
// todo: j.sotRating, j.conSubscr, j.isSotCon, j.showARSLink, k.sotCtxt // todo: j.sotRating, j.conSubscr, j.isSotCon, j.showARSLink, k.sotCtxt
// todo: j.conSubscr, j.showARSLink, j.useableTime // todo: j.conSubscr, j.showARSLink, j.useableTime

View file

@ -21,8 +21,8 @@ const transformReqBody = (body) => {
return body return body
} }
const parseLocation = (profile, l, lines) => { const parseLocation = (profile, data, l) => {
const res = _parseLocation(profile, l, lines) const res = _parseLocation(profile, data, l)
// weird fix for empty lines, e.g. IC/EC at Flensburg Hbf // weird fix for empty lines, e.g. IC/EC at Flensburg Hbf
if (res.lines) { if (res.lines) {
res.lines = res.lines.filter(x => x.id && x.name) res.lines = res.lines.filter(x => x.id && x.name)
@ -36,8 +36,8 @@ const parseLocation = (profile, l, lines) => {
return res return res
} }
const createParseJourney = (profile, stations, lines, remarks) => { const createParseJourney = (profile, data) => {
const parseJourney = _createParseJourney(profile, stations, lines, remarks) const parseJourney = _createParseJourney(profile, data)
const parseJourneyWithTickets = (j) => { const parseJourneyWithTickets = (j) => {
const res = parseJourney(j) const res = parseJourney(j)
@ -78,8 +78,8 @@ const createParseJourney = (profile, stations, lines, remarks) => {
return parseJourneyWithTickets return parseJourneyWithTickets
} }
const createParseMovement = (profile, locations, lines, remarks) => { const createParseMovement = (profile, data) => {
const _parseMovement = _createParseMovement(profile, locations, lines, remarks) const _parseMovement = _createParseMovement(profile, data)
const parseMovement = (m) => { const parseMovement = (m) => {
const res = _parseMovement(m) const res = _parseMovement(m)
// filter out empty nextStops entries // filter out empty nextStops entries

View file

@ -25,10 +25,10 @@ const transformReqBody = (body) => {
return body return body
} }
const parseLocation = (profile, l, lines) => { const parseLocation = (profile, data, l) => {
// ÖBB has some 'stations' **in austria** with no departures/products, // ÖBB has some 'stations' **in austria** with no departures/products,
// like station entrances, that are actually POIs. // like station entrances, that are actually POIs.
const res = _parseLocation(profile, l, lines) const res = _parseLocation(profile, data, l)
if ( if (
res.type === 'station' && res.type === 'station' &&
!res.products && !res.products &&
@ -44,8 +44,8 @@ const parseLocation = (profile, l, lines) => {
return res return res
} }
const createParseMovement = (profile, locations, lines, remarks) => { const createParseMovement = (profile, data) => {
const _parseMovement = _createParseMovement(profile, locations, lines, remarks) const _parseMovement = _createParseMovement(profile, data)
const parseMovement = (m) => { const parseMovement = (m) => {
const res = _parseMovement(m) const res = _parseMovement(m)
// filter out POIs // filter out POIs

View file

@ -23,8 +23,8 @@ const transformReqBody = (body) => {
return body return body
} }
const createParseLine = (profile, operators) => { const createParseLine = (profile, data) => {
const parseLine = _createParseLine(profile, operators) const parseLine = _createParseLine(profile, data)
const parseLineWithMoreDetails = (l) => { const parseLineWithMoreDetails = (l) => {
const res = parseLine(l) const res = parseLine(l)
@ -42,8 +42,8 @@ const createParseLine = (profile, operators) => {
return parseLineWithMoreDetails return parseLineWithMoreDetails
} }
const parseLocation = (profile, l, lines) => { const parseLocation = (profile, data, l) => {
const res = _parseLocation(profile, l, lines) const res = _parseLocation(profile, data, l)
if (res.type === 'station') { if (res.type === 'station') {
res.name = shorten(res.name) res.name = shorten(res.name)
@ -56,8 +56,8 @@ const parseLocation = (profile, l, lines) => {
return res return res
} }
const createParseJourney = (profile, stations, lines, remarks, polylines) => { const createParseJourney = (profile, data) => {
const parseJourney = _createParseJourney(profile, stations, lines, remarks, polylines) const parseJourney = _createParseJourney(profile, data)
const parseJourneyWithTickets = (j) => { const parseJourneyWithTickets = (j) => {
const res = parseJourney(j) const res = parseJourney(j)
@ -86,8 +86,8 @@ const createParseJourney = (profile, stations, lines, remarks, polylines) => {
return parseJourneyWithTickets return parseJourneyWithTickets
} }
const createParseDeparture = (profile, stations, lines, remarks) => { const createParseDeparture = (profile, data) => {
const parseDeparture = _createParseDeparture(profile, stations, lines, remarks) const parseDeparture = _createParseDeparture(profile, data)
const ringbahnClockwise = /^ringbahn s\s?41$/i const ringbahnClockwise = /^ringbahn s\s?41$/i
const ringbahnAnticlockwise = /^ringbahn s\s?42$/i const ringbahnAnticlockwise = /^ringbahn s\s?42$/i

View file

@ -6,7 +6,7 @@
// todo: d.stbStop.dProgType // todo: d.stbStop.dProgType
// todo: d.freq, d.freq.jnyL, see https://github.com/public-transport/hafas-client/blob/9203ed1481f08baacca41ac5e3c19bf022f01b0b/parse.js#L115 // todo: d.freq, d.freq.jnyL, see https://github.com/public-transport/hafas-client/blob/9203ed1481f08baacca41ac5e3c19bf022f01b0b/parse.js#L115
const createParseDeparture = (profile, stations, lines, remarks) => { const createParseDeparture = (profile, {stations, lines, remarks}) => {
const findRemark = rm => remarks[parseInt(rm.remX)] || null const findRemark = rm => remarks[parseInt(rm.remX)] || null
const parseDeparture = (d) => { const parseDeparture = (d) => {

View file

@ -4,7 +4,9 @@ const parseDateTime = require('./date-time')
const clone = obj => Object.assign({}, obj) const clone = obj => Object.assign({}, obj)
const createParseJourneyLeg = (profile, stations, lines, remarks, polylines) => { const createParseJourneyLeg = (profile, data) => {
const {locations, lines, remarks, polylines} = data
// todo: finish parse/remark.js first // todo: finish parse/remark.js first
const applyRemark = (j, rm) => {} const applyRemark = (j, rm) => {}
@ -17,8 +19,8 @@ const createParseJourneyLeg = (profile, stations, lines, remarks, polylines) =>
const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeR || pt.dep.dTimeS) const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeR || pt.dep.dTimeS)
const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeR || pt.arr.aTimeS) const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeR || pt.arr.aTimeS)
const res = { const res = {
origin: clone(stations[parseInt(pt.dep.locX)]) || null, origin: clone(locations[parseInt(pt.dep.locX)]) || null,
destination: clone(stations[parseInt(pt.arr.locX)]), destination: clone(locations[parseInt(pt.arr.locX)]),
departure: dep.toISO(), departure: dep.toISO(),
arrival: arr.toISO() arrival: arr.toISO()
} }
@ -40,7 +42,7 @@ const createParseJourneyLeg = (profile, stations, lines, remarks, polylines) =>
let p = pt.jny.polyG.polyXL let p = pt.jny.polyG.polyXL
p = Array.isArray(p) && polylines[p[0]] p = Array.isArray(p) && polylines[p[0]]
// todo: there can be >1 polyline // todo: there can be >1 polyline
const parse = profile.parsePolyline(stations) const parse = profile.parsePolyline(data)
res.polyline = p && parse(p) || null res.polyline = p && parse(p) || null
} }
@ -57,7 +59,7 @@ const createParseJourneyLeg = (profile, stations, lines, remarks, polylines) =>
if (pt.arr.aPlatfS) res.arrivalPlatform = pt.arr.aPlatfS if (pt.arr.aPlatfS) res.arrivalPlatform = pt.arr.aPlatfS
if (passed && pt.jny.stopL) { if (passed && pt.jny.stopL) {
const parse = profile.parseStopover(profile, stations, lines, remarks, j.date) const parse = profile.parseStopover(profile, data, j.date)
const passedStations = pt.jny.stopL.map(parse) const passedStations = pt.jny.stopL.map(parse)
// filter stations the train passes without stopping, as this doesn't comply with fptf (yet) // filter stations the train passes without stopping, as this doesn't comply with fptf (yet)
res.passed = passedStations.filter((x) => !x.passBy) res.passed = passedStations.filter((x) => !x.passBy)

View file

@ -2,8 +2,8 @@
const clone = obj => Object.assign({}, obj) const clone = obj => Object.assign({}, obj)
const createParseJourney = (profile, stations, lines, remarks, polylines) => { const createParseJourney = (profile, data) => {
const parseLeg = profile.parseJourneyLeg(profile, stations, lines, remarks, polylines) const parseLeg = profile.parseJourneyLeg(profile, data)
// todo: c.sDays // todo: c.sDays
// todo: c.conSubscr // todo: c.conSubscr

View file

@ -2,7 +2,7 @@
const slugg = require('slugg') const slugg = require('slugg')
const createParseLine = (profile, operators) => { const createParseLine = (profile, {operators}) => {
const byBitmask = [] const byBitmask = []
for (let product of profile.products) { for (let product of profile.products) {
for (let bitmask of product.bitmasks) { for (let bitmask of product.bitmasks) {

View file

@ -7,8 +7,8 @@ const ADDRESS = 'A'
// todo: what is s.rRefL? // todo: what is s.rRefL?
// todo: is passing in profile necessary? // todo: is passing in profile necessary?
// todo: [breaking] change to createParseLocation(profile, lines) => (l) => loc // todo: [breaking] change to createParseLocation(profile, data) => (l) => loc
const parseLocation = (profile, l, lines) => { const parseLocation = (profile, {lines}, l) => {
const res = {type: 'location'} const res = {type: 'location'}
if (l.crd) { if (l.crd) {
res.latitude = l.crd.y / 1000000 res.latitude = l.crd.y / 1000000

View file

@ -1,6 +1,8 @@
'use strict' 'use strict'
const createParseMovement = (profile, locations, lines, remarks, polylines = []) => { const createParseMovement = (profile, data) => {
const {locations, lines, remarks, polylines} = data
// 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?
@ -8,7 +10,7 @@ const createParseMovement = (profile, locations, lines, remarks, polylines = [])
// todo: what is m.ani.dirGeo[n]? maybe the speed? // todo: what is m.ani.dirGeo[n]? maybe the speed?
// todo: what is m.ani.proc[n]? wut? // todo: what is m.ani.proc[n]? wut?
const parseMovement = (m) => { const parseMovement = (m) => {
const pStopover = profile.parseStopover(profile, locations, lines, remarks, m.date) const pStopover = profile.parseStopover(profile, data, m.date)
const res = { const res = {
direction: profile.parseStationName(m.dirTxt), direction: profile.parseStationName(m.dirTxt),
@ -36,13 +38,13 @@ const createParseMovement = (profile, locations, lines, remarks, polylines = [])
} }
if (m.ani.poly) { if (m.ani.poly) {
const parse = profile.parsePolyline(locations) const parse = profile.parsePolyline(data)
res.polyline = parse(m.ani.poly) res.polyline = parse(m.ani.poly)
} else if (m.ani.polyG) { } else if (m.ani.polyG) {
let p = m.ani.polyG.polyXL let p = m.ani.polyG.polyXL
p = Array.isArray(p) && polylines[p[0]] p = Array.isArray(p) && polylines[p[0]]
// todo: there can be >1 polyline // todo: there can be >1 polyline
const parse = profile.parsePolyline(locations) const parse = profile.parsePolyline(data)
res.polyline = p && parse(p) || null res.polyline = p && parse(p) || null
} }
} }

View file

@ -6,9 +6,9 @@
// todo: what is s.wt? // todo: what is s.wt?
// todo: what is s.dur? // todo: what is s.dur?
// todo: [breaking] change to createParseNearby(profile, lines) => (n) => nearby // todo: [breaking] change to createParseNearby(profile, data) => (n) => nearby
const parseNearby = (profile, n, lines) => { const parseNearby = (profile, data, n) => {
const res = profile.parseLocation(profile, n, lines) const res = profile.parseLocation(profile, data, n)
res.distance = n.dist res.distance = n.dist
return res return res
} }

View file

@ -3,7 +3,7 @@
const {toGeoJSON} = require('@mapbox/polyline') const {toGeoJSON} = require('@mapbox/polyline')
const distance = require('gps-distance') const distance = require('gps-distance')
const createParsePolyline = (locations) => { const createParsePolyline = ({locations}) => {
// todo: what is p.delta? // todo: what is p.delta?
// todo: what is p.type? // todo: what is p.type?
// todo: what is p.crdEncS? // todo: what is p.crdEncS?

View file

@ -2,10 +2,10 @@
// todo: arrivalDelay, departureDelay or only delay ? // todo: arrivalDelay, departureDelay or only delay ?
// todo: arrivalPlatform, departurePlatform // todo: arrivalPlatform, departurePlatform
const createParseStopover = (profile, stations, lines, remarks, date) => { const createParseStopover = (profile, {locations, lines, remarks}, date) => {
const parseStopover = (st) => { const parseStopover = (st) => {
const res = { const res = {
station: stations[parseInt(st.locX)] || null, station: locations[parseInt(st.locX)] || null,
arrival: null, arrival: null,
arrivalDelay: null, arrivalDelay: null,
departure: null, departure: null,