All files / applications/go-by-transit/src/view/nearby/util stopNavigation.ts

100% Statements 16/16
100% Branches 14/14
100% Functions 2/2
100% Lines 15/15

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 3015x 15x     65x 65x       95x 95x 31x   31x 31x   19x 19x 19x   13x           18x 18x    
const DEFAULT_STOP_BACK = "/nearby/stops";
const LOCAL_ORIGIN = "https://local.invalid";
 
export function stopDetailUrl(stopId: number, referrer: string): string {
  const query = new URLSearchParams({ referrer });
  return `/nearby/stops/${stopId}?${query.toString()}`;
}
 
export function stopBackUrl(search: string): string {
  const referrers = new URLSearchParams(search).getAll("referrer");
  if (!referrers.length) return DEFAULT_STOP_BACK;
  const referrer = referrers[0];
  // oxlint-disable-next-line no-control-regex -- Reject controls before URL parsing normalizes them.
  const hasUnsafeCharacters = /[\\\u0000-\u001f\u007f]/.test(referrer);
  if (referrers.length === 1 && referrer.startsWith("/") &&
      !referrer.startsWith("//") && !hasUnsafeCharacters) {
    try {
      const url = new URL(referrer, LOCAL_ORIGIN);
      if (url.origin === LOCAL_ORIGIN &&
          (url.pathname === "/" || url.pathname === "/nearby" || url.pathname.startsWith("/nearby/"))) {
        return `${url.pathname}${url.search}${url.hash}`;
      }
    } catch {
      // Invalid referrers fall back to the stop list.
    }
  }
  console.warn("Invalid stop referrer; returning to nearby stops.");
  return DEFAULT_STOP_BACK;
}