104 lines
2.4 KiB
TypeScript
104 lines
2.4 KiB
TypeScript
import type {
|
|
Service,
|
|
OB_TrainTT_service,
|
|
OB_Pis_SimpleObject,
|
|
OB_TrainTT_stopDetail,
|
|
Stop,
|
|
} from "@owlboard/ts-types";
|
|
import { logger } from "../../logger.utils";
|
|
|
|
export function formatTimetableDetail(
|
|
service: Service,
|
|
pis: OB_Pis_SimpleObject | null
|
|
): OB_TrainTT_service {
|
|
const formattedService: OB_TrainTT_service = {
|
|
stpIndicator: service.stpIndicator,
|
|
operator: service.operator,
|
|
trainUid: service.trainUid,
|
|
headcode: service.headcode,
|
|
powerType: service.powerType,
|
|
planSpeed: convertStringToNumber(service.planSpeed),
|
|
scheduleStart: service.scheduleStartDate,
|
|
scheduleEnd: service.scheduleEndDate,
|
|
daysRun: service.daysRun,
|
|
stops: formatStops(service.stops),
|
|
vstp: service.vstp,
|
|
firstClass: service.firstClass,
|
|
catering: service.catering,
|
|
sleeper: service.sleeper,
|
|
};
|
|
|
|
if (pis) {
|
|
formattedService.pis = pis;
|
|
}
|
|
|
|
return formattedService;
|
|
}
|
|
|
|
function formatStops(stops: Stop[]): OB_TrainTT_stopDetail[] {
|
|
if (!stops) {
|
|
return []
|
|
}
|
|
|
|
if (!stops.length) {
|
|
return []
|
|
}
|
|
|
|
// Cleanly coerce Stop[] to OB_TrainTT_stopDetail[]
|
|
const formattedStops: OB_TrainTT_stopDetail[] = [];
|
|
|
|
for (const stop of stops) {
|
|
formattedStops.push(formatStopTimes(stop));
|
|
}
|
|
|
|
return formattedStops;
|
|
}
|
|
|
|
function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail {
|
|
// Cleanly converts a single stop to a stopdetail object
|
|
let formattedStop: OB_TrainTT_stopDetail = {
|
|
tiploc: stop.tiploc,
|
|
isPublic: false,
|
|
};
|
|
if (stop.publicArrival) {
|
|
formattedStop.publicArrival = stop.publicArrival;
|
|
formattedStop.isPublic = true;
|
|
}
|
|
if (stop.publicDeparture) {
|
|
formattedStop.publicDeparture = stop.publicDeparture;
|
|
formattedStop.isPublic = true;
|
|
}
|
|
if (stop.wttArrival) {
|
|
formattedStop.wttArrival = stop.wttArrival;
|
|
}
|
|
if (stop.wttDeparture) {
|
|
formattedStop.wttDeparture = stop.wttDeparture;
|
|
}
|
|
|
|
if (stop.platform) {
|
|
formattedStop.platform = stop.platform;
|
|
}
|
|
|
|
if (stop.pass) {
|
|
formattedStop.pass = stop.pass;
|
|
}
|
|
|
|
if (stop.arrLine) {
|
|
formattedStop.arrLine = stop.arrLine;
|
|
}
|
|
|
|
if (stop.depLine) {
|
|
formattedStop.depLine = stop.depLine;
|
|
}
|
|
return formattedStop;
|
|
}
|
|
|
|
function convertStringToNumber(str: string): number {
|
|
const number = parseFloat(str);
|
|
if (isNaN(number)) {
|
|
return 0;
|
|
} else {
|
|
return number;
|
|
}
|
|
}
|