From 39636f12bf9fde8c245bba886903d88bfd2d2262 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 26 Jan 2018 16:25:13 +0100 Subject: [PATCH] parse lines at a location --- index.js | 2 +- lib/request.js | 7 ++++--- p/oebb/index.js | 4 ++-- p/vbb/index.js | 4 ++-- parse/location.js | 14 +++++++++++++- parse/nearby.js | 6 ++++-- test/util.js | 5 +++++ 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 5ca98593..aff36d06 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,7 @@ const createClient = (profile, request = _request) => { .then((d) => { if (!d.match || !Array.isArray(d.match.locL)) return [] const parse = profile.parseLocation - return d.match.locL.map(loc => parse(profile, loc)) + return d.match.locL.map(loc => parse(profile, loc, d.lines)) }) } diff --git a/lib/request.js b/lib/request.js index 1aefd2b4..27838806 100644 --- a/lib/request.js +++ b/lib/request.js @@ -54,9 +54,6 @@ const request = (profile, data) => { const d = b.svcResL[0].res const c = d.common || {} - if (Array.isArray(c.locL)) { - d.locations = c.locL.map(loc => profile.parseLocation(profile, loc)) - } if (Array.isArray(c.remL)) { d.remarks = c.remL.map(rem => profile.parseRemark(profile, rem)) } @@ -67,6 +64,10 @@ const request = (profile, data) => { const parse = profile.parseLine(profile, d.operators) d.lines = c.prodL.map(parse) } + if (Array.isArray(c.locL)) { + const parse = loc => profile.parseLocation(profile, loc, d.lines) + d.locations = c.locL.map(parse) + } return d }) } diff --git a/p/oebb/index.js b/p/oebb/index.js index 0063f2d9..13a61284 100644 --- a/p/oebb/index.js +++ b/p/oebb/index.js @@ -48,10 +48,10 @@ const createParseLine = (profile, operators) => { return parseLineWithMode } -const parseLocation = (profile, l) => { +const parseLocation = (profile, l, lines) => { // ÖBB has some 'stations' **in austria** with no departures/products, // like station entrances, that are actually POIs. - const res = _parseLocation(profile, l) + const res = _parseLocation(profile, l, lines) if ( res.type === 'station' && !res.products && diff --git a/p/vbb/index.js b/p/vbb/index.js index a3fb5a94..851647ce 100644 --- a/p/vbb/index.js +++ b/p/vbb/index.js @@ -55,8 +55,8 @@ const createParseLine = (profile, operators) => { return parseLineWithMode } -const parseLocation = (profile, l) => { - const res = _parseLocation(profile, l) +const parseLocation = (profile, l, lines) => { + const res = _parseLocation(profile, l, lines) if (res.type === 'station') { res.name = shorten(res.name) diff --git a/parse/location.js b/parse/location.js index b776b920..c0de893d 100644 --- a/parse/location.js +++ b/parse/location.js @@ -6,7 +6,9 @@ const ADDRESS = 'A' // todo: what is s.rRefL? // todo: is passing in profile necessary? -const parseLocation = (profile, l) => { + +// todo: [breaking] change to createParseLocation(profile, lines) => (l) => loc +const parseLocation = (profile, l, lines) => { const res = {type: 'location'} if (l.crd) { res.latitude = l.crd.y / 1000000 @@ -20,7 +22,17 @@ const parseLocation = (profile, l) => { name: l.name, location: res } + if ('pCls' in l) station.products = profile.parseProducts(l.pCls) + + if (Array.isArray(l.pRefL) && Array.isArray(lines)) { + station.lines = [] + for (let pRef of l.pRefL) { + const line = lines[pRef] + if (line) station.lines.push(line) + } + } + return station } diff --git a/parse/nearby.js b/parse/nearby.js index e009b405..7c5b3b9a 100644 --- a/parse/nearby.js +++ b/parse/nearby.js @@ -5,8 +5,10 @@ // todo: what is s.pCls? // todo: what is s.wt? // todo: what is s.dur? -const parseNearby = (profile, n) => { - const res = profile.parseLocation(profile, n) + +// todo: [breaking] change to createParseNearby(profile, lines) => (n) => nearby +const parseNearby = (profile, n, lines) => { + const res = profile.parseLocation(profile, n, lines) res.distance = n.dist return res } diff --git a/test/util.js b/test/util.js index fbd0d8c5..d304a957 100644 --- a/test/util.js +++ b/test/util.js @@ -20,6 +20,11 @@ const assertValidStation = (t, s, coordsOptional = false) => { t.ok(s.location) assertValidLocation(t, s.location, coordsOptional) } + + if ('lines' in s) { + t.ok(Array.isArray(s.lines)) + for (let l of s.lines) assertValidLine(t, l) + } } const assertValidPoi = (t, p) => {