All files / src/lib route-layout.ts

100% Statements 47/47
90.9% Branches 10/11
100% Functions 4/4
100% Lines 47/47

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 701x                               1x 21x 21x   1x 10x 10x   10x 5x 5x   10x 10x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x   3x 3x 3x 3x 3x 3x   4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x  
import type { NominatimConfig } from "./nominatim.js";
import { reverseGeocode } from "./nominatim.js";
 
export type RouteLayoutCode = "CIRCULAR" | "LINEAR";
 
export type RouteLayoutResolution = {
  routeLayoutCode: RouteLayoutCode;
  startLocality: string;
  endLocality: string;
};
 
export type TrackGeocodeResult = RouteLayoutResolution & {
  /** Locality старта (для поля формы). */
  locality: string;
};
 
export function normalizeLocality(value: string): string {
  return value.trim().toLowerCase();
}
 
export function classifyRouteLayout(startLocality: string, endLocality: string): RouteLayoutCode {
  const start = normalizeLocality(startLocality);
  const end = normalizeLocality(endLocality);
 
  if (!start || !end) {
    return "LINEAR";
  }
 
  return start === end ? "CIRCULAR" : "LINEAR";
}
 
export async function resolveRouteLayout(
  startLat: number,
  startLon: number,
  endLat: number,
  endLon: number,
  config: NominatimConfig,
): Promise<RouteLayoutResolution> {
  const nominatimConfig: NominatimConfig = {
    baseUrl: config.baseUrl,
    userAgent: config.userAgent,
    ...(config.timeoutMs !== undefined ? { timeoutMs: config.timeoutMs } : {}),
  };
 
  const [start, end] = await Promise.all([
    reverseGeocode(startLat, startLon, nominatimConfig),
    reverseGeocode(endLat, endLon, nominatimConfig),
  ]);
 
  return {
    startLocality: start.locality,
    endLocality: end.locality,
    routeLayoutCode: classifyRouteLayout(start.locality, end.locality),
  };
}
 
export async function resolveTrackGeocode(
  startLat: number,
  startLon: number,
  endLat: number,
  endLon: number,
  config: NominatimConfig,
): Promise<TrackGeocodeResult> {
  const layout = await resolveRouteLayout(startLat, startLon, endLat, endLon, config);
  return {
    locality: layout.startLocality,
    ...layout,
  };
}