Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/components/parser/multiStopParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@ export function multiStopUnifier(
json: EtaPredictionJson,
stopBookmarks: StopBookmark[]
) {
const result = multiStopParser(json);

// phase 2: combine a/c to stop number
const unifiedList: stopBookmarkWithEta[] = stopBookmarks.map(
(stopBookmark) => {
const enabled = stopBookmark.enabled;

return {
stopId: stopBookmark.stopId,
name: stopBookmark.name,
Expand All @@ -73,21 +70,34 @@ export function multiStopUnifier(
}
);

const result = multiStopParser(json);

// Create a Map for fast lookup by stopTag
const resultMap = new Map<number, LineStopEta>();
for (const item of result) {
const matchingStop = unifiedList.findIndex(
(searching) => item.stopTag === searching.ttcId
);
if (item.direction) {
unifiedList[matchingStop].direction = item.direction;
resultMap.set(item.stopTag, item);
}

for (const matchingStop of unifiedList) {
const item = resultMap.get(matchingStop.ttcId);
if (!item) {
continue; // Skip if no match found
}

// Handle direction logic
if (!matchingStop.direction) {
matchingStop.direction = item.direction || "";
} else if (
unifiedList[matchingStop].direction?.toLowerCase() !==
item.direction?.toLowerCase()
item.direction &&
matchingStop.direction.toLowerCase() !== item.direction.toLowerCase()
) {
unifiedList[matchingStop].direction = "multiple";
matchingStop.direction = "multiple";
}
unifiedList[matchingStop].etas = unifiedList[matchingStop].etas
.concat(item.etas)
.sort((a, b) => a.epochTime - b.epochTime);

// Merge and sort etas
matchingStop.etas = [...matchingStop.etas, ...(item.etas ?? [])].sort(
(a, b) => a.epochTime - b.epochTime
);
}
return unifiedList;
}