db-vendo-client/parse/arrival-or-departure.js

63 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

const ARRIVAL = 'a';
const DEPARTURE = 'd';
const createParseArrOrDep = (prefix) => {
if (prefix !== ARRIVAL && prefix !== DEPARTURE) {
throw new Error('invalid prefix');
}
2018-06-26 15:49:50 +02:00
const parseArrOrDep = (ctx, d) => { // d = raw arrival/departure
const {profile, opt} = ctx;
2025-01-02 14:00:45 +00:00
const cancelled = profile.parseCancelled(d);
2018-06-26 15:49:50 +02:00
const res = {
tripId: d.journeyID || d.journeyId || d.train?.journeyId || d.zuglaufId,
stop: profile.parseLocation(ctx, d.station || d.abfrageOrt || {bahnhofsId: d.bahnhofsId}),
2025-01-02 14:00:45 +00:00
...profile.parseWhen(
ctx,
null,
d.timeSchedule || d.time || d.zeit || d.abgangsDatum || d.ankunftsDatum,
d.timeType != 'SCHEDULE' ? d.timePredicted || d.time || d.ezZeit || d.ezAbgangsDatum || d.ezAnkunftsDatum : null,
2025-01-02 14:00:45 +00:00
cancelled),
...profile.parsePlatform(ctx, d.platformSchedule || d.platform || d.gleis, d.platformPredicted || d.platform || d.ezGleis, cancelled),
2024-12-08 21:42:57 +00:00
// prognosisType: TODO
direction: d.transport?.direction?.stopPlaces?.length > 0 && profile.parseStationName(ctx, d.transport?.direction?.stopPlaces[0].name) || profile.parseStationName(ctx, d.destination?.name || d.richtung || d.terminus) || null,
2025-01-02 14:00:45 +00:00
provenance: profile.parseStationName(ctx, d.transport?.origin?.name || d.origin?.name || d.abgangsOrt?.name) || null,
2024-12-07 22:46:04 +00:00
line: profile.parseLine(ctx, d) || null,
remarks: [],
2024-12-10 17:51:20 +00:00
origin: profile.parseLocation(ctx, d.transport?.origin || d.origin) || null,
destination: profile.parseLocation(ctx, d.transport?.destination || d.destination) || null,
2024-12-21 15:26:49 +00:00
// loadFactor: profile.parseArrOrDepWithLoadFactor(ctx, d)
};
2024-12-07 22:46:04 +00:00
// TODO pos
2025-01-02 14:00:45 +00:00
if (cancelled) {
res.cancelled = true;
Object.defineProperty(res, 'canceled', {value: true});
2018-06-26 15:49:50 +02:00
}
2018-06-28 13:45:56 +02:00
if (opt.remarks) {
2024-12-21 15:26:49 +00:00
res.remarks = profile.parseRemarks(ctx, d);
2018-06-28 13:45:56 +02:00
}
if ((opt.stopovers || opt.direction) && Array.isArray(d.ueber)) {
const stopovers = d.ueber
.map(viaName => profile.parseStopover(ctx, {name: viaName}, null));
if (prefix === ARRIVAL) {
res.previousStopovers = stopovers;
} else if (prefix === DEPARTURE) {
res.nextStopovers = stopovers;
}
}
return res;
};
2018-06-26 15:49:50 +02:00
return parseArrOrDep;
};
2018-06-26 15:49:50 +02:00
2022-05-07 16:17:37 +02:00
export {
createParseArrOrDep,
};