All files / util/journeys/src endpoints.ts

92.3% Statements 12/13
100% Branches 14/14
83.33% Functions 5/6
91.66% Lines 11/12

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                      113x       41x               354x 354x 60x   294x 416x 284x     10x   16x     10x    
type Endpoint =
  | { kind: "stop"; stop: { locid: number } }
  | { kind: "place" | "coord"; value: string };
 
interface StopReference {
  locid: number;
  desc: string;
  aliases: readonly string[];
}
 
export function endpointLabelForRequest(endpoint: Endpoint) {
  return endpoint.kind === "stop" ? String(endpoint.stop.locid) : endpoint.value;
}
 
export function sameEndpoint(from: Endpoint, to: Endpoint) {
  return from.kind === to.kind &&
    endpointLabelForRequest(from).toLowerCase() === endpointLabelForRequest(to).toLowerCase();
}
 
export function resolveStopReference<T extends StopReference>(
  stops: readonly T[],
  reference: string,
): { matches: T[]; stop?: T } {
  const query = reference.trim().toLowerCase();
  if (!query) {
    return { matches: [] };
  }
  if (/^\d+$/.test(query)) {
    const stop = stops.find((item) => item.locid === Number(query));
    return { matches: stop ? [stop] : [], stop };
  }
 
  const matches = stops.filter(
    (stop) =>
      stop.desc.toLowerCase() === query ||
      stop.aliases.some((alias) => alias.toLowerCase() === query),
  );
  return { matches, stop: matches.length === 1 ? matches[0] : undefined };
}