Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 50x 3x 99x 9x 9x 8x 7x 5x 11x 1x 10x 1x 9x 9x 9x 9x 9x 9x 9x 9x 23x 23x 23x 23x 23x 25x 25x 3x 3x 3x 22x 33x 1x 1x 32x 32x 32x 20x 20x 20x 1x 20x 12x 12x 9x 38x 5x 9x 31x 31x 9x 22x 31x 90x 15x 1x | import type {
RouteDataResultSet,
RouteDirectionStop,
TrimetRoute,
} from "./interfaces/routes";
const STOP_MODES = ["Bus", "Rail", "Streetcar", "Aerial tram"] as const;
export type StopMode = typeof STOP_MODES[number];
interface StopCatalogRoute {
id: number;
label: string;
color?: string;
}
export interface StopCatalogStop extends RouteDirectionStop {
aliases: string[];
modes?: StopMode[];
routes?: StopCatalogRoute[];
}
export interface StopCatalog {
stops: StopCatalogStop[];
routeCount: number;
directionCount: number;
stopReferenceCount: number;
incompleteRouteCount: number;
incompleteDirectionCount: number;
incompleteRouteNames: string[];
invalidStopCount: number;
}
function routeName(route: TrimetRoute): string {
return `${route.desc || "Unknown route"} (${route.route})`;
}
export function stopMode(route: Pick<TrimetRoute, "type" | "route" | "desc">): StopMode | undefined {
if (route.type === "B" || route.type === "bus") return "Bus";
Iif (route.type === "S") return "Streetcar";
if (route.type === "R") {
if (route.route === 208) return "Aerial tram";
if (/^Portland Streetcar\b/i.test(route.desc)) return "Streetcar";
return "Rail";
}
}
export function buildStopCatalog(
data: RouteDataResultSet,
routeColor?: (route: TrimetRoute, mode: StopMode | undefined) => string | undefined,
): StopCatalog {
if (!data || data.errorMessage) {
throw new Error(data?.errorMessage || "TriMet returned no route data");
}
if (!Array.isArray(data.route) || data.route.length === 0) {
throw new Error("TriMet returned no route configuration");
}
const stopsById = new Map<number, StopCatalogStop & {
modes: StopMode[];
routesById: Map<number, StopCatalogRoute>;
}>();
const incompleteRoutes = new Set<string>();
let directionCount = 0;
let stopReferenceCount = 0;
let incompleteRouteCount = 0;
let incompleteDirectionCount = 0;
let invalidStopCount = 0;
data.route.forEach((route) => {
const mode = route ? stopMode(route) : undefined;
const color = route ? routeColor?.(route, mode) : undefined;
Iif (!route || !Array.isArray(route.dir)) {
incompleteRouteCount += 1;
if (route) {
incompleteRoutes.add(routeName(route));
}
return;
}
const service = Number.isSafeInteger(route.route) && route.route > 0
? { id: route.route, label: route.desc?.trim() || `Route ${route.route}`, color }
: undefined;
route.dir.forEach((direction) => {
directionCount += 1;
if (!direction || !Array.isArray(direction.stop)) {
incompleteDirectionCount += 1;
incompleteRoutes.add(routeName(route));
return;
}
direction.stop.forEach((stop) => {
if (
!stop ||
!Number.isSafeInteger(stop.locid) ||
stop.locid <= 0 ||
typeof stop.desc !== "string" ||
!stop.desc.trim() ||
!Number.isFinite(stop.lat) ||
!Number.isFinite(stop.lng)
) {
invalidStopCount += 1;
return;
}
stopReferenceCount += 1;
const existing = stopsById.get(stop.locid);
if (existing) {
if (mode && !existing.modes.includes(mode)) existing.modes.push(mode);
Eif (service) existing.routesById.set(service.id, service);
if (
stop.desc !== existing.desc &&
!existing.aliases.includes(stop.desc)
) {
existing.aliases.push(stop.desc);
}
return;
}
stopsById.set(stop.locid, {
...stop,
aliases: [],
modes: mode ? [mode] : [],
routesById: new Map(service ? [[service.id, service]] : []),
});
});
});
});
const stops = Array.from(stopsById.values(), ({ modes, routesById, ...stop }) => ({
...stop,
modes: modes.sort((a, b) => STOP_MODES.indexOf(a) - STOP_MODES.indexOf(b)),
routes: Array.from(routesById.values()).sort((a, b) => a.id - b.id),
})).sort(
(a, b) =>
a.desc.toLowerCase().localeCompare(b.desc.toLowerCase()) ||
a.locid - b.locid,
);
return {
stops,
routeCount: data.route.length,
directionCount,
stopReferenceCount,
incompleteRouteCount,
incompleteDirectionCount,
incompleteRouteNames: Array.from(incompleteRoutes).sort(),
invalidStopCount,
};
}
export function searchStopCatalog(
stops: StopCatalogStop[],
search: string,
{ includeRouteNames = false } = {},
): StopCatalogStop[] {
const query = search.trim().toLowerCase();
if (!query) {
return [];
}
const searchRoutes = includeRouteNames && !/^\d+$/.test(query);
return stops.filter(
(stop) =>
stop.locid.toString().includes(query) ||
stop.desc.toLowerCase().includes(query) ||
stop.aliases.some((alias) => alias.toLowerCase().includes(query)) ||
(searchRoutes && stop.routes?.some(({ label }) => label.toLowerCase().includes(query))),
);
}
|