From 93814983dafb92079f42d0f74821b05a144551e3 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 27 Oct 2018 14:47:19 +0200 Subject: [PATCH] add profile.parseCommon --- index.js | 47 ++++++----------------------------------- lib/default-profile.js | 2 ++ lib/request.js | 37 +------------------------------- p/db/index.js | 4 ++-- parse/common.js | 48 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 78 deletions(-) create mode 100644 parse/common.js diff --git a/index.js b/index.js index ff9276be..e67319be 100644 --- a/index.js +++ b/index.js @@ -89,13 +89,7 @@ const createClient = (profile, userAgent, request = _request) => { }) .then((d) => { if (!Array.isArray(d.jnyL)) return [] - const parse = parser(profile, opt, { - raw: d, - locations: d.locations, - lines: d.lines, - hints: d.hints, - warnings: d.warnings - }) + const parse = parser(profile, opt, d) return d.jnyL.map(parse) .sort((a, b) => new Date(a.when) - new Date(b.when)) }) @@ -224,14 +218,7 @@ const createClient = (profile, userAgent, request = _request) => { .then((d) => { if (!Array.isArray(d.outConL)) return [] - const parse = profile.parseJourney(profile, opt, { - raw: d, - locations: d.locations, - lines: d.lines, - hints: d.hints, - warnings: d.warnings, - polylines: opt.polylines && d.common.polyL || [] - }) + const parse = profile.parseJourney(profile, opt, d) if (!earlierRef) earlierRef = d.outCtxScrB @@ -284,14 +271,7 @@ const createClient = (profile, userAgent, request = _request) => { throw new Error('invalid response') } - const parse = profile.parseJourney(profile, opt, { - raw: d, - locations: d.locations, - lines: d.lines, - hints: d.hints, - warnings: d.warnings, - polylines: opt.polylines && d.common.polyL || [] - }) + const parse = profile.parseJourney(profile, opt, d) return parse(d.outConL[0]) }) } @@ -325,7 +305,7 @@ const createClient = (profile, userAgent, 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, opt, {lines: d.lines}, loc)) + return d.match.locL.map(loc => parse(profile, opt, d, loc)) }) } @@ -348,7 +328,7 @@ const createClient = (profile, userAgent, request = _request) => { // todo: proper stack trace? throw new Error('invalid response') } - return profile.parseLocation(profile, opt, {lines: d.lines}, d.locL[0]) + return profile.parseLocation(profile, opt, d, d.locL[0]) }) } @@ -414,14 +394,7 @@ const createClient = (profile, userAgent, request = _request) => { } }) .then((d) => { - const parse = profile.parseJourneyLeg(profile, opt, { - raw: d, - locations: d.locations, - lines: d.lines, - hints: d.hints, - warnings: d.warnings, - polylines: opt.polyline && d.common.polyL || [] - }) + const parse = profile.parseJourneyLeg(profile, opt, d) const rawLeg = { // pretend the leg is contained in a journey type: 'JNY', @@ -477,13 +450,7 @@ const createClient = (profile, userAgent, request = _request) => { .then((d) => { if (!Array.isArray(d.jnyL) || d.jnyL.length === 0) return [] - const parse = profile.parseMovement(profile, opt, { - locations: d.locations, - lines: d.lines, - hints: d.hints, - warnings: d.warnings, - polylines: opt.polylines && d.common.polyL || [] - }) + const parse = profile.parseMovement(profile, opt, d) return d.jnyL.map(parse) }) } diff --git a/lib/default-profile.js b/lib/default-profile.js index c350cacb..767d08ca 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -7,6 +7,7 @@ const parseJourneyLeg = require('../parse/journey-leg') const parseJourney = require('../parse/journey') const parseLine = require('../parse/line') const parseLocation = require('../parse/location') +const parseCommon = require('../parse/common') const parsePolyline = require('../parse/polyline') const parseMovement = require('../parse/movement') const parseNearby = require('../parse/nearby') @@ -46,6 +47,7 @@ const defaultProfile = { parseLine, parseStationName: id, parseLocation, + parseCommon, parsePolyline, parseMovement, parseNearby, diff --git a/lib/request.js b/lib/request.js index 954b4df1..8b3cd4d8 100644 --- a/lib/request.js +++ b/lib/request.js @@ -105,43 +105,8 @@ const request = (profile, userAgent, opt, data) => { addErrorInfo(err, b.svcResL[0].err, b.svcResL[0].errTxt) throw err } - const d = b.svcResL[0].res - const c = d.common || {} - d.hints = [] - if (opt.remarks && Array.isArray(c.remL)) { - const icons = opt.remarks && c.icoL || [] - d.hints = c.remL.map(hint => profile.parseHint(profile, hint, icons)) - } - d.warnings = [] - if (opt.remarks && Array.isArray(c.himL)) { - const icons = opt.remarks && c.icoL || [] - d.warnings = c.himL.map(w => profile.parseWarning(profile, w, icons)) - } - if (Array.isArray(c.opL)) { - d.operators = c.opL.map(op => profile.parseOperator(profile, op)) - } - if (Array.isArray(c.prodL)) { - const parse = profile.parseLine(profile, opt, { - operators: d.operators - }) - d.lines = c.prodL.map(parse) - } - if (Array.isArray(c.locL)) { - const data = {lines: d.lines} - const parse = loc => profile.parseLocation(profile, opt, data, loc) - - d.locations = c.locL.map(parse) - for (let i = 0; i < d.locations.length; i++) { - const raw = c.locL[i] - const loc = d.locations[i] - if ('number' === typeof raw.mMastLocX) { - loc.station = Object.assign({}, d.locations[raw.mMastLocX]) - loc.station.type = 'station' - } else if (raw.isMainMast) loc.type = 'station' - } - } - return d + return profile.parseCommon(profile, opt, b.svcResL[0].res) }) } diff --git a/p/db/index.js b/p/db/index.js index 7733e9ba..c0085696 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -46,7 +46,7 @@ const createParseArrOrDep = (createParse) => (profile, opt, data) => { const parseWithLoadFactor = (d) => { const result = parse(d) if (d.stbStop.dTrnCmpSX && Array.isArray(d.stbStop.dTrnCmpSX.tcocX)) { - const {tcocL} = data.raw.common + const {tcocL} = data.common const load = parseLoadFactor(opt, tcocL, d.stbStop.dTrnCmpSX.tcocX) if (load) result.loadFactor = load } @@ -139,7 +139,7 @@ const createParseJourneyLeg = (profile, opt, data) => { const parseJourneyLegWithLoadFactor = (j, pt, parseStopovers) => { const result = parseJourneyLeg(j, pt, parseStopovers) if (pt.jny && pt.jny.dTrnCmpSX && Array.isArray(pt.jny.dTrnCmpSX.tcocX)) { - const {tcocL} = data.raw.common + const {tcocL} = data.common const load = parseLoadFactor(opt, tcocL, pt.jny.dTrnCmpSX.tcocX) if (load) result.loadFactor = load } diff --git a/parse/common.js b/parse/common.js new file mode 100644 index 00000000..b1381e26 --- /dev/null +++ b/parse/common.js @@ -0,0 +1,48 @@ +'use strict' + +const parseCommonData = (profile, opt, raw) => { + const res = Object.assign({}, raw) + const c = raw.common || {} + + if (Array.isArray(c.opL)) { + res.operators = c.opL.map(op => profile.parseOperator(profile, op)) + } + + res.icons = [] + if (Array.isArray(c.icoL)) res.icons = c.icoL + + res.hints = [] + if (opt.remarks && Array.isArray(c.remL)) { + res.hints = c.remL.map(hint => profile.parseHint(profile, hint, res.icons)) + } + res.warnings = [] + if (opt.remarks && Array.isArray(c.himL)) { + res.warnings = c.himL.map(w => profile.parseWarning(profile, w, res.icons)) + } + + if (Array.isArray(c.prodL)) { + const parse = profile.parseLine(profile, opt, res) + res.lines = c.prodL.map(parse) + } + + if (Array.isArray(c.locL)) { + const parse = loc => profile.parseLocation(profile, opt, res, loc) + res.locations = c.locL.map(parse) + + for (let i = 0; i < res.locations.length; i++) { + const raw = c.locL[i] + const loc = res.locations[i] + if ('number' === typeof raw.mMastLocX) { + loc.station = Object.assign({}, res.locations[raw.mMastLocX]) + loc.station.type = 'station' + } else if (raw.isMainMast) loc.type = 'station' + } + } + + res.polylines = [] + if (opt.polylines && Array.isArray(c.polyL)) res.polylines = c.polyL + + return res +} + +module.exports = parseCommonData