From 08357dfa9d8f37b0e6046848173ef2574857a5eb Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 16 Mar 2018 17:00:06 +0100 Subject: [PATCH] parseLine: move mode & products parsing from profiles into main code --- p/db/index.js | 24 +----------------------- p/insa/index.js | 23 +---------------------- p/oebb/index.js | 24 +----------------------- p/vbb/index.js | 15 +++------------ parse/line.js | 27 ++++++++++++++++++++++++--- 5 files changed, 30 insertions(+), 83 deletions(-) diff --git a/p/db/index.js b/p/db/index.js index cd79befb..812cb94d 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -1,6 +1,5 @@ 'use strict' -const _createParseLine = require('../../parse/line') const _createParseJourney = require('../../parse/journey') const _formatStation = require('../../format/station') const {bike} = require('../../format/filters') @@ -35,26 +34,6 @@ const transformJourneysQuery = (query, opt) => { return query } -const createParseLine = (profile, operators) => { - const parseLine = _createParseLine(profile, operators) - - const parseLineWithMode = (l) => { - const res = parseLine(l) - - res.mode = res.product = null - if ('class' in res) { - const data = products.bitmasks[parseInt(res.class)] - if (data) { - res.mode = data.mode - res.product = data.product - } - } - - return res - } - return parseLineWithMode -} - const createParseJourney = (profile, stations, lines, remarks) => { const parseJourney = _createParseJourney(profile, stations, lines, remarks) @@ -112,10 +91,9 @@ const dbProfile = { transformReqBody, transformJourneysQuery, - products: products.allProducts, + products: products, // todo: parseLocation - parseLine: createParseLine, parseJourney: createParseJourney, formatStation diff --git a/p/insa/index.js b/p/insa/index.js index 11709ca1..ed562dee 100644 --- a/p/insa/index.js +++ b/p/insa/index.js @@ -1,6 +1,5 @@ 'use strict' -const _createParseLine = require('../../parse/line') const products = require('./products') const transformReqBody = (body) => { @@ -18,33 +17,13 @@ const transformReqBody = (body) => { return body } -const createParseLine = (profile, operators) => { - const parseLine = _createParseLine(profile, operators) - - const parseLineWithMode = (l) => { - const res = parseLine(l) - - res.mode = res.product = null - if ('class' in res) { - const data = products.bitmasks[parseInt(res.class)] - if (data) { - res.mode = data.mode - res.product = data.product - } - } - - return res - } - return parseLineWithMode -} - const insaProfile = { locale: 'de-DE', timezone: 'Europe/Berlin', endpoint: 'http://reiseauskunft.insa.de/bin/mgate.exe', transformReqBody, - products: products.allProducts, + products: products, journeyLeg: true, radar: true diff --git a/p/oebb/index.js b/p/oebb/index.js index a3c73a0d..99496761 100644 --- a/p/oebb/index.js +++ b/p/oebb/index.js @@ -3,7 +3,6 @@ // todo: https://gist.github.com/anonymous/a5fc856bc80ae7364721943243f934f4#file-haf_config_base-properties-L5 // todo: https://gist.github.com/anonymous/a5fc856bc80ae7364721943243f934f4#file-haf_config_base-properties-L47-L234 -const _createParseLine = require('../../parse/line') const _parseLocation = require('../../parse/location') const _createParseMovement = require('../../parse/movement') @@ -26,26 +25,6 @@ const transformReqBody = (body) => { return body } -const createParseLine = (profile, operators) => { - const parseLine = _createParseLine(profile, operators) - - const parseLineWithMode = (l) => { - const res = parseLine(l) - - res.mode = res.product = null - if ('class' in res) { - const data = products.bitmasks[parseInt(res.class)] - if (data) { - res.mode = data.mode - res.product = data.product - } - } - - return res - } - return parseLineWithMode -} - const parseLocation = (profile, l, lines) => { // ÖBB has some 'stations' **in austria** with no departures/products, // like station entrances, that are actually POIs. @@ -87,9 +66,8 @@ const oebbProfile = { endpoint: 'http://fahrplan.oebb.at/bin/mgate.exe', transformReqBody, - products: products.allProducts, + products: products, - parseLine: createParseLine, parseLocation, parseMovement: createParseMovement, diff --git a/p/vbb/index.js b/p/vbb/index.js index cb926e9d..534c93d6 100644 --- a/p/vbb/index.js +++ b/p/vbb/index.js @@ -27,18 +27,9 @@ const transformReqBody = (body) => { const createParseLine = (profile, operators) => { const parseLine = _createParseLine(profile, operators) - const parseLineWithMode = (l) => { + const parseLineWithMoreDetails = (l) => { const res = parseLine(l) - res.mode = res.product = null - if ('class' in res) { - const data = products.bitmasks[parseInt(res.class)] - if (data) { - res.mode = data.mode - res.product = data.product - } - } - const details = parseLineName(l.name) res.symbol = details.symbol res.nr = details.nr @@ -48,7 +39,7 @@ const createParseLine = (profile, operators) => { return res } - return parseLineWithMode + return parseLineWithMoreDetails } const parseLocation = (profile, l, lines) => { @@ -154,7 +145,7 @@ const vbbProfile = { transformReqBody, - products: products.allProducts, + products: products, parseStationName: shorten, parseLocation, diff --git a/parse/line.js b/parse/line.js index 79300c08..99e8c293 100644 --- a/parse/line.js +++ b/parse/line.js @@ -2,8 +2,23 @@ const slugg = require('slugg') -// todo: are p.number and p.line ever different? const createParseLine = (profile, operators) => { + const byBitmask = [] + for (let product of profile.products) { + if ('string' !== typeof product.product) { + throw new Error('profile.products[].product must be a string.') + } + if (!Array.isArray(product.bitmasks)) { + throw new Error(product.product + '.bitmasks must be an array.') + } + for (let bitmask of product.bitmasks) { + if ('number' !== typeof bitmask) { + throw new Error(product.product + '.bitmasks[] must be a number.') + } + byBitmask[bitmask] = product + } + } + const parseLine = (p) => { if (!p) return null // todo: handle this upstream const res = { @@ -13,8 +28,9 @@ const createParseLine = (profile, operators) => { public: true } // todo: what is p.prodCtx && p.prodCtx.num? + // todo: what is p.number? - // This is terrible, but FPTF demands an ID. Let's pray for VBB to expose an ID. + // This is terrible, but FPTF demands an ID. Let's pray for HaCon to expose an ID. // todo: find a better way if (p.line) res.id = slugg(p.line.trim()) else if (p.name) res.id = slugg(p.name.trim()) @@ -24,7 +40,12 @@ const createParseLine = (profile, operators) => { res.productCode = +p.prodCtx.catCode } - // todo: parse mode, remove from profiles + if ('class' in res) { + // todo: what if `res.class` is the sum of two bitmasks? + const data = byBitmask[parseInt(res.class)] + res.mode = data && data.mode || null + res.product = data && data.product || null + } if ('number' === typeof p.oprX) { res.operator = operators[p.oprX] || null