Convert eta,ata,sta,std to "RT" if on-time

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-09-14 20:48:16 +01:00
parent ad1afc10c7
commit 465874d512
3 changed files with 1670 additions and 1132 deletions

2749
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -31,7 +31,7 @@
"zlib": "^1.0.5" "zlib": "^1.0.5"
}, },
"devDependencies": { "devDependencies": {
"@owlboard/ts-types": "^0.0.8", "@owlboard/ts-types": "^0.0.9",
"@types/jest": "^29.5.3", "@types/jest": "^29.5.3",
"eslint": "^8.39.0", "eslint": "^8.39.0",
"jest": "^29.6.2", "jest": "^29.6.2",

View File

@ -74,6 +74,7 @@ function transformTrainServices(input: any): TrainServices[] {
services = [input.service]; services = [input.service];
} }
for (const service of services) { for (const service of services) {
const times = parseTimes(service)
const trainService: TrainServices = { const trainService: TrainServices = {
rid: service?.rid, rid: service?.rid,
uid: service?.uid, uid: service?.uid,
@ -90,12 +91,12 @@ function transformTrainServices(input: any): TrainServices[] {
delayReason: service?.delayReason, delayReason: service?.delayReason,
arrivalType: service?.arrivalType, arrivalType: service?.arrivalType,
departureType: service?.departureType, departureType: service?.departureType,
sta: transformUnspecifiedDateTime(service?.sta), sta: times.sta,
eta: transformUnspecifiedDateTime(service?.eta), eta: times.eta,
ata: transformUnspecifiedDateTime(service?.ata), ata: times.ata,
std: transformUnspecifiedDateTime(service?.std), std: times.std,
etd: transformUnspecifiedDateTime(service?.etd), etd: times.etd,
atd: transformUnspecifiedDateTime(service?.atd), atd: times.atd,
}; };
Object.keys(trainService).forEach( Object.keys(trainService).forEach(
(key) => trainService[key] === undefined && delete trainService[key] (key) => trainService[key] === undefined && delete trainService[key]
@ -146,3 +147,41 @@ function transformUnspecifiedDateTime(input: string): Date | undefined {
const date = tz(input, "Europe/London"); const date = tz(input, "Europe/London");
return date.toDate(); return date.toDate();
} }
function parseTimes(service: TrainServices) {
let {sta, eta, ata, std, etd, atd} = Object.fromEntries(
Object.entries(service).map(([key, value]) => [key, transformUnspecifiedDateTime(value)])
)
let etaResult: Date | undefined | string = eta;
let ataResult: Date | undefined | string = ata;
let etdResult: Date | undefined | string = etd;
let atdResult: Date | undefined | string = atd;
if (sta) {
if (eta !== undefined && Math.abs(eta.getTime() - sta.getTime()) / 60000 <= 1.5) {
etaResult = "RT";
}
if (ata !== undefined && Math.abs(ata.getTime() - sta.getTime()) / 60000 <= 1.5) {
ataResult = "RT";
}
}
if (std) {
if (etd !== undefined && Math.abs(etd.getTime() - std.getTime()) / 60000 <= 1.5) {
etdResult = "RT";
}
if (atd !== undefined && Math.abs(atd.getTime() - std.getTime()) / 60000 <= 1.5) {
atdResult = "RT";
}
}
return {
sta: sta,
eta: etaResult,
ata: ataResult,
std: std,
etd: etdResult,
atd: atdResult,
}
}