db-vendo-client/lib/slice-leg.js
Kristjan ESPERANTO 66d9fb5194
apply linting rules
follow-up of 228c7253
2024-02-10 16:50:12 +01:00

63 lines
1.6 KiB
JavaScript

const findById = (needle) => {
const needleStopId = needle.id;
const needleStationId = needle.station
? needle.station.id
: null;
return (stop) => {
if (needleStopId === stop.id) {
return true;
}
const stationId = stop.station
? stop.station.id
: null;
if (needleStationId && stationId && needleStationId === stationId) {
return true;
}
// todo: `needleStationId === stop.id`? `needleStopId === stationId`?
return false;
};
};
const sliceLeg = (leg, from, to) => {
if (!Array.isArray(leg.stopovers)) {
throw new Error('leg.stopovers must be an array.');
}
const stops = leg.stopovers.map(st => st.stop);
const fromI = stops.findIndex(findById(from));
if (fromI === -1) {
throw new Error('from not found in stopovers');
}
const fromStopover = leg.stopovers[fromI];
const toI = stops.findIndex(findById(to));
if (toI === -1) {
throw new Error('to not found in stopovers');
}
const toStopover = leg.stopovers[toI];
if (fromI === 0 && toI === leg.stopovers.length - 1) {
return leg;
}
const newLeg = Object.assign({}, leg);
newLeg.stopovers = leg.stopovers.slice(fromI, toI + 1);
newLeg.origin = fromStopover.stop;
newLeg.departure = fromStopover.departure;
newLeg.departureDelay = fromStopover.departureDelay;
newLeg.scheduledDeparture = fromStopover.scheduledDeparture;
newLeg.departurePlatform = fromStopover.departurePlatform;
newLeg.destination = toStopover.stop;
newLeg.arrival = toStopover.arrival;
newLeg.arrivalDelay = toStopover.arrivalDelay;
newLeg.scheduledArrival = toStopover.scheduledArrival;
newLeg.arrivalPlatform = toStopover.arrivalPlatform;
return newLeg;
};
export {
sliceLeg,
};