From 0eb919433ae87e6612cc67f016492aec33862948 Mon Sep 17 00:00:00 2001 From: desmondlsl <68117020+desmondlsl@users.noreply.github.com> Date: Thu, 31 Oct 2024 00:40:37 +0800 Subject: [PATCH 1/2] lightRail route_list is null when there are no ETAs available --- src/lightRail.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lightRail.ts b/src/lightRail.ts index 24d23af..b85decb 100644 --- a/src/lightRail.ts +++ b/src/lightRail.ts @@ -25,7 +25,8 @@ export default function fetchEtas({ platform_list.reduce( (acc: Eta[], { route_list, platform_id }: any) => [ ...acc, - ...route_list + // route_list is null when there are no ETAs available + ...(route_list ?? []) .filter( ({ route_no, dest_ch, dest_en, stop }: any) => route === route_no && From 72b6d951c22fad3ea5c89b83f16c785d7274d586 Mon Sep 17 00:00:00 2001 From: desmondlsl <68117020+desmondlsl@users.noreply.github.com> Date: Thu, 31 Oct 2024 00:41:21 +0800 Subject: [PATCH 2/2] handle lightRail end_service_status --- src/lightRail.ts | 133 +++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 56 deletions(-) diff --git a/src/lightRail.ts b/src/lightRail.ts index b85decb..d66634a 100644 --- a/src/lightRail.ts +++ b/src/lightRail.ts @@ -21,62 +21,83 @@ export default function fetchEtas({ }, ) .then((response) => response.json()) - .then(({ platform_list }) => - platform_list.reduce( - (acc: Eta[], { route_list, platform_id }: any) => [ - ...acc, - // route_list is null when there are no ETAs available - ...(route_list ?? []) - .filter( - ({ route_no, dest_ch, dest_en, stop }: any) => - route === route_no && - (dest_ch === dest.zh || dest_en.includes("Circular")) && - stop === 0, - ) - .map(({ time_en, train_length }: any) => { - let waitTime = 0; - switch (time_en.toLowerCase()) { - case "arriving": - case "departing": - case "-": - waitTime = 0; - break; - default: - waitTime = parseInt(time_en, 10); - break; - } - const etaDate = new Date( - Date.now() + waitTime * 60 * 1000 + 8 * 3600000, - ); - return { - eta: - `${etaDate.getUTCFullYear()}-${`0${ - etaDate.getUTCMonth() + 1 - }`.slice(-2)}-${`0${etaDate.getUTCDate()}`.slice(-2)}` + - `T${`0${etaDate.getUTCHours()}`.slice( - -2, - )}:${`0${etaDate.getMinutes()}`.slice( - -2, - )}:${`0${etaDate.getSeconds()}`.slice(-2)}+08:00`, - remark: { - zh: `${platform_id}號月台 - ${Array(train_length) - .fill("▭") - .join("")}`, - en: `Platform ${platform_id} - ${Array(train_length) - .fill("▭") - .join("")}`, - }, - dest: { - zh: "", - en: "", - }, - co: "lightRail", - }; - }, []), - ], - [], - ), - ) + .then(({ platform_list }) => { + if ( + platform_list.every(({ end_service_status }: any) => end_service_status) + ) { + return [ + { + eta: null, + remark: { + zh: "此站今日服務已經終止", + en: "This stop's service for today has ended", + }, + dest: { + zh: "", + en: "", + }, + co: "lightRail", + }, + ]; + } + // How should `end_service_status` for only some platforms be handled? Would a light rail line stop at different platforms? + return platform_list.reduce( + (acc: Eta[], { route_list, platform_id }: any) => { + return [ + ...acc, + // route_list is null when there are no ETAs available + ...(route_list ?? []) + .filter( + ({ route_no, dest_ch, dest_en, stop }: any) => + route === route_no && + (dest_ch === dest.zh || dest_en.includes("Circular")) && + stop === 0, + ) + .map(({ time_en, train_length }: any) => { + let waitTime = 0; + switch (time_en.toLowerCase()) { + case "arriving": + case "departing": + case "-": + waitTime = 0; + break; + default: + waitTime = parseInt(time_en, 10); + break; + } + const etaDate = new Date( + Date.now() + waitTime * 60 * 1000 + 8 * 3600000, + ); + return { + eta: + `${etaDate.getUTCFullYear()}-${`0${ + etaDate.getUTCMonth() + 1 + }`.slice(-2)}-${`0${etaDate.getUTCDate()}`.slice(-2)}` + + `T${`0${etaDate.getUTCHours()}`.slice( + -2, + )}:${`0${etaDate.getMinutes()}`.slice( + -2, + )}:${`0${etaDate.getSeconds()}`.slice(-2)}+08:00`, + remark: { + zh: `${platform_id}號月台 - ${Array(train_length) + .fill("▭") + .join("")}`, + en: `Platform ${platform_id} - ${Array(train_length) + .fill("▭") + .join("")}`, + }, + dest: { + zh: "", + en: "", + }, + co: "lightRail", + }; + }, []), + ]; + }, + [] + ); + }) .catch((e) => { console.error(e); return [];