All files / util/journeys/src plannerEndpoints.ts

100% Statements 16/16
96.42% Branches 27/28
100% Functions 4/4
100% Lines 12/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                    207x 15x 2x       3x           25x 7x             28x 28x 27x 22x 22x   5x    
import { parsePlannerCoordinates } from "@overworld/trimet-client";
import type { StopCatalogStop, TripPlannerLocation } from "@overworld/trimet-client";
import type { InterestPlace } from "@overworld/places/catalog";
 
export type PlannerEndpoint =
  | { kind: "stop"; stop: StopCatalogStop }
  | { kind: "place" | "coord"; value: string; interestPlace?: InterestPlace };
export type EndpointField = "from" | "to";
 
export function endpointLabel(endpoint: PlannerEndpoint, resolved?: TripPlannerLocation) {
  if (endpoint.kind === "stop") return `${endpoint.stop.desc} (${endpoint.stop.locid})`;
  if (endpoint.kind === "coord") return endpoint.interestPlace?.name || endpoint.value;
  return resolved?.description || endpoint.value;
}
 
export function sameEndpointMessage(from: PlannerEndpoint, to: PlannerEndpoint) {
  return from.kind === "stop" && to.kind === "stop"
    ? "Choose two different stops to plan a trip."
    : "Choose two different locations to plan a trip.";
}
 
export function endpointParams(endpoint: PlannerEndpoint, field: EndpointField): Record<string, string> {
  if (endpoint.kind === "stop") return { [field]: String(endpoint.stop.locid) };
  return {
    [`${field}${endpoint.kind === "coord" ? "Coord" : "Place"}`]: endpoint.value,
    ...(endpoint.interestPlace ? { [`${field}PlaceId`]: endpoint.interestPlace.id } : {})
  };
}
 
export function locationEndpoint(value: string, coordinates = false): PlannerEndpoint | undefined {
  const trimmed = value.trim();
  if (!trimmed) return undefined;
  if (coordinates || /^[+\-.\d\s,]+$/.test(trimmed) || /^[+-]?(?:\d+\.?\d*|\.\d+)\s*,/.test(trimmed)) {
    const point = parsePlannerCoordinates(trimmed);
    return point && { kind: "coord", value: point.join(",") };
  }
  return { kind: "place", value: trimmed };
}