From ef7d965523fd32c4c586c77065eef23fb3561b76 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 14 Sep 2023 21:30:56 +0100 Subject: [PATCH] Refactor 'classGenerator()' --- .../ldb/staff/table/table-generator.svelte | 49 +++++-------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/src/lib/ldb/staff/table/table-generator.svelte b/src/lib/ldb/staff/table/table-generator.svelte index bd036cd..87cdd31 100644 --- a/src/lib/ldb/staff/table/table-generator.svelte +++ b/src/lib/ldb/staff/table/table-generator.svelte @@ -37,47 +37,20 @@ platArr.push('nonPass'); } - if (service.sta !== undefined) { - if (service.eta !== undefined) { - if (service.sta < service.eta) { - arrArr.push('late'); - } - } else if (service.ata !== undefined) { - if (service.sta < service.ata) { - arrArr.push('late'); - } - } - if (service.eta !== undefined) { - if (service.sta > service.eta) { - arrArr.push('early'); - } - } else if (service.ata !== undefined) { - if (service.sta > service.ata) { - arrArr.push('early'); + function checkLateEarly(originalTime: Date | undefined, comparedTime: Date | undefined, arr: string[]) { + if (originalTime !== undefined && comparedTime instanceof Date) { + if (originalTime < comparedTime) { + arr.push('late'); + } else if (originalTime > comparedTime) { + arr.push('early'); } } } - if (service.std !== undefined) { - if (service.etd !== undefined) { - if (service.std < service.etd) { - depArr.push('late'); - } - } else if (service.atd !== undefined) { - if (service.std < service.atd) { - depArr.push('late'); - } - } - if (service.etd !== undefined) { - if (service.std > service.etd) { - depArr.push('early'); - } - } else if (service.atd !== undefined) { - if (service.std > service.atd) { - depArr.push('early'); - } - } - } + checkLateEarly(service.sta, service.eta, arrArr); + checkLateEarly(service.sta, service.ata, arrArr); + checkLateEarly(service.std, service.etd, depArr); + checkLateEarly(service.std, service.atd, depArr); return { other: otherArr.join(' '), @@ -88,7 +61,7 @@ } function fmtTime(date: Date | string | undefined): string | false { - if (date === 'RT' || date === "CANC" || date === "LATE") return date; + if (typeof date === 'string') return date; if (date instanceof Date) { const hours = date.getHours().toString().padStart(2, '0'); const minutes = date.getMinutes().toString().padStart(2, '0');