add profile.parseCommon

This commit is contained in:
Jannis R 2018-10-27 14:47:19 +02:00
parent 4f9c3435d6
commit 93814983da
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 60 additions and 78 deletions

View file

@ -89,13 +89,7 @@ const createClient = (profile, userAgent, request = _request) => {
}) })
.then((d) => { .then((d) => {
if (!Array.isArray(d.jnyL)) return [] if (!Array.isArray(d.jnyL)) return []
const parse = parser(profile, opt, { const parse = parser(profile, opt, d)
raw: d,
locations: d.locations,
lines: d.lines,
hints: d.hints,
warnings: d.warnings
})
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))
}) })
@ -224,14 +218,7 @@ const createClient = (profile, userAgent, request = _request) => {
.then((d) => { .then((d) => {
if (!Array.isArray(d.outConL)) return [] if (!Array.isArray(d.outConL)) return []
const parse = profile.parseJourney(profile, opt, { const parse = profile.parseJourney(profile, opt, d)
raw: d,
locations: d.locations,
lines: d.lines,
hints: d.hints,
warnings: d.warnings,
polylines: opt.polylines && d.common.polyL || []
})
if (!earlierRef) earlierRef = d.outCtxScrB if (!earlierRef) earlierRef = d.outCtxScrB
@ -284,14 +271,7 @@ const createClient = (profile, userAgent, request = _request) => {
throw new Error('invalid response') throw new Error('invalid response')
} }
const parse = profile.parseJourney(profile, opt, { const parse = profile.parseJourney(profile, opt, d)
raw: d,
locations: d.locations,
lines: d.lines,
hints: d.hints,
warnings: d.warnings,
polylines: opt.polylines && d.common.polyL || []
})
return parse(d.outConL[0]) return parse(d.outConL[0])
}) })
} }
@ -325,7 +305,7 @@ const createClient = (profile, userAgent, 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, 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? // todo: proper stack trace?
throw new Error('invalid response') 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) => { .then((d) => {
const parse = profile.parseJourneyLeg(profile, opt, { const parse = profile.parseJourneyLeg(profile, opt, d)
raw: d,
locations: d.locations,
lines: d.lines,
hints: d.hints,
warnings: d.warnings,
polylines: opt.polyline && d.common.polyL || []
})
const rawLeg = { // pretend the leg is contained in a journey const rawLeg = { // pretend the leg is contained in a journey
type: 'JNY', type: 'JNY',
@ -477,13 +450,7 @@ const createClient = (profile, userAgent, request = _request) => {
.then((d) => { .then((d) => {
if (!Array.isArray(d.jnyL) || d.jnyL.length === 0) return [] if (!Array.isArray(d.jnyL) || d.jnyL.length === 0) return []
const parse = profile.parseMovement(profile, opt, { const parse = profile.parseMovement(profile, opt, d)
locations: d.locations,
lines: d.lines,
hints: d.hints,
warnings: d.warnings,
polylines: opt.polylines && d.common.polyL || []
})
return d.jnyL.map(parse) return d.jnyL.map(parse)
}) })
} }

View file

@ -7,6 +7,7 @@ const parseJourneyLeg = require('../parse/journey-leg')
const parseJourney = require('../parse/journey') const parseJourney = require('../parse/journey')
const parseLine = require('../parse/line') const parseLine = require('../parse/line')
const parseLocation = require('../parse/location') const parseLocation = require('../parse/location')
const parseCommon = require('../parse/common')
const parsePolyline = require('../parse/polyline') const parsePolyline = require('../parse/polyline')
const parseMovement = require('../parse/movement') const parseMovement = require('../parse/movement')
const parseNearby = require('../parse/nearby') const parseNearby = require('../parse/nearby')
@ -46,6 +47,7 @@ const defaultProfile = {
parseLine, parseLine,
parseStationName: id, parseStationName: id,
parseLocation, parseLocation,
parseCommon,
parsePolyline, parsePolyline,
parseMovement, parseMovement,
parseNearby, parseNearby,

View file

@ -105,43 +105,8 @@ const request = (profile, userAgent, opt, data) => {
addErrorInfo(err, b.svcResL[0].err, b.svcResL[0].errTxt) addErrorInfo(err, b.svcResL[0].err, b.svcResL[0].errTxt)
throw err throw err
} }
const d = b.svcResL[0].res
const c = d.common || {}
d.hints = [] return profile.parseCommon(profile, opt, b.svcResL[0].res)
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
}) })
} }

View file

@ -46,7 +46,7 @@ const createParseArrOrDep = (createParse) => (profile, opt, data) => {
const parseWithLoadFactor = (d) => { const parseWithLoadFactor = (d) => {
const result = parse(d) const result = parse(d)
if (d.stbStop.dTrnCmpSX && Array.isArray(d.stbStop.dTrnCmpSX.tcocX)) { 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) const load = parseLoadFactor(opt, tcocL, d.stbStop.dTrnCmpSX.tcocX)
if (load) result.loadFactor = load if (load) result.loadFactor = load
} }
@ -139,7 +139,7 @@ const createParseJourneyLeg = (profile, opt, data) => {
const parseJourneyLegWithLoadFactor = (j, pt, parseStopovers) => { const parseJourneyLegWithLoadFactor = (j, pt, parseStopovers) => {
const result = parseJourneyLeg(j, pt, parseStopovers) const result = parseJourneyLeg(j, pt, parseStopovers)
if (pt.jny && pt.jny.dTrnCmpSX && Array.isArray(pt.jny.dTrnCmpSX.tcocX)) { 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) const load = parseLoadFactor(opt, tcocL, pt.jny.dTrnCmpSX.tcocX)
if (load) result.loadFactor = load if (load) result.loadFactor = load
} }

48
parse/common.js Normal file
View file

@ -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