From 715541f0609b56a174b6dc54de54e8d24103b452 Mon Sep 17 00:00:00 2001 From: Traines Date: Tue, 14 Jan 2025 18:13:58 +0000 Subject: [PATCH] refactor --- parse/journey-leg.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/parse/journey-leg.js b/parse/journey-leg.js index d0bb39c9..9f31d6de 100644 --- a/parse/journey-leg.js +++ b/parse/journey-leg.js @@ -13,13 +13,14 @@ const locationFallback = (id, name, fallbackLocations) => { const parseJourneyLeg = (ctx, pt, date, fallbackLocations) => { // pt = raw leg const {profile, opt} = ctx; + const stops = pt.halte?.length && pt.halte || pt.stops?.length && pt.stops || []; const res = { - origin: pt.halte?.length > 0 && profile.parseLocation(ctx, pt.halte[0].ort || pt.halte[0]) || pt.abgangsOrt?.name && profile.parseLocation(ctx, pt.abgangsOrt) || locationFallback(pt.abfahrtsOrtExtId, pt.abfahrtsOrt, fallbackLocations), - destination: pt.halte?.length > 0 && profile.parseLocation(ctx, pt.halte[pt.halte.length - 1].ort || pt.halte[pt.halte.length - 1]) || pt.ankunftsOrt?.name && profile.parseLocation(ctx, pt.ankunftsOrt) || locationFallback(pt.ankunftsOrtExtId, pt.ankunftsOrt, fallbackLocations), + origin: stops.length && profile.parseLocation(ctx, stops[0].ort || stops[0].station || stops[0]) || pt.abgangsOrt?.name && profile.parseLocation(ctx, pt.abgangsOrt) || locationFallback(pt.abfahrtsOrtExtId, pt.abfahrtsOrt, fallbackLocations), + destination: stops.length && profile.parseLocation(ctx, stops[stops.length - 1].ort || stops[stops.length - 1].station || stops[stops.length - 1]) || pt.ankunftsOrt?.name && profile.parseLocation(ctx, pt.ankunftsOrt) || locationFallback(pt.ankunftsOrtExtId, pt.ankunftsOrt, fallbackLocations), }; - const cancelledDep = pt.halte?.length > 0 && profile.parseCancelled(pt.halte[0]); - const dep = profile.parseWhen(ctx, date, pt.abfahrtsZeitpunkt || pt.abgangsDatum || pt.halte?.length > 0 && pt.halte[0].abgangsDatum, pt.ezAbfahrtsZeitpunkt || pt.ezAbgangsDatum || pt.halte?.length > 0 && pt.halte[0].ezAbgangsDatum, cancelledDep); + const cancelledDep = stops.length && profile.parseCancelled(stops[0]); + const dep = profile.parseWhen(ctx, date, pt.abfahrtsZeitpunkt || pt.abgangsDatum || stops.length && stops[0].abgangsDatum, pt.ezAbfahrtsZeitpunkt || pt.ezAbgangsDatum || stops.length && stops[0].ezAbgangsDatum, cancelledDep); res.departure = dep.when; res.plannedDeparture = dep.plannedWhen; res.departureDelay = dep.delay; @@ -27,8 +28,8 @@ const parseJourneyLeg = (ctx, pt, date, fallbackLocations) => { // pt = raw leg res.prognosedDeparture = dep.prognosedWhen; } - const cancelledArr = pt.halte?.length > 0 && profile.parseCancelled(pt.halte[pt.halte.length - 1]); - const arr = profile.parseWhen(ctx, date, pt.ankunftsZeitpunkt || pt.ankunftsDatum || pt.halte?.length > 0 && pt.halte[pt.halte.length - 1].ankunftsDatum, pt.ezAnkunftsZeitpunkt || pt.ezAnkunftsDatum || pt.halte?.length > 0 && pt.halte[pt.halte.length - 1].ezAkunftsDatum, cancelledArr); + const cancelledArr = stops.length && profile.parseCancelled(stops[stops.length - 1]); + const arr = profile.parseWhen(ctx, date, pt.ankunftsZeitpunkt || pt.ankunftsDatum || stops.length && stops[stops.length - 1].ankunftsDatum, pt.ezAnkunftsZeitpunkt || pt.ezAnkunftsDatum || stops.length && stops[stops.length - 1].ezAnkunftsDatum, cancelledArr); res.arrival = arr.when; res.plannedArrival = arr.plannedWhen; res.arrivalDelay = arr.delay; @@ -67,8 +68,10 @@ const parseJourneyLeg = (ctx, pt, date, fallbackLocations) => { // pt = raw leg res.direction = pt.verkehrsmittel?.richtung || pt.richtung || null; // TODO res.currentLocation - if (pt.halte?.length > 0) { - const arrPl = profile.parsePlatform(ctx, pt.halte[pt.halte.length - 1].gleis, pt.halte[pt.halte.length - 1].ezGleis, cancelledArr); + // TODO trainStartDate? + + if (stops.length) { + const arrPl = profile.parsePlatform(ctx, stops[stops.length - 1].gleis, stops[stops.length - 1].ezGleis, cancelledArr); res.arrivalPlatform = arrPl.platform; res.plannedArrivalPlatform = arrPl.plannedPlatform; if (arrPl.prognosedPlatform) { @@ -76,7 +79,7 @@ const parseJourneyLeg = (ctx, pt, date, fallbackLocations) => { // pt = raw leg } // res.arrivalPrognosisType = null; // TODO - const depPl = profile.parsePlatform(ctx, pt.halte[0].gleis, pt.halte[0].ezGleis, cancelledDep); + const depPl = profile.parsePlatform(ctx, stops[0].gleis, stops[0].ezGleis, cancelledDep); res.departurePlatform = depPl.platform; res.plannedDeparturePlatform = depPl.plannedPlatform; if (depPl.prognosedPlatform) { @@ -86,7 +89,7 @@ const parseJourneyLeg = (ctx, pt, date, fallbackLocations) => { // pt = raw leg if (opt.stopovers) { - res.stopovers = pt.halte.map(s => profile.parseStopover(ctx, s, date)); + res.stopovers = stops.map(s => profile.parseStopover(ctx, s, date)); // filter stations the train passes without stopping, as this doesn't comply with fptf (yet) res.stopovers = res.stopovers.filter((x) => !x.passBy); }