← back to Stayclaim

src/lib/geo.ts

31 lines

/**
 * Geographic helpers — currently just a single coord-validation guard, but
 * created as its own module so future haversine / bbox / projection helpers
 * have a clear home (mirrors the lib/geo.ts pattern in directory-core).
 *
 * Created 2026-05-05 by the autonomous YOLO loop refactor pass — the
 * `isValidCoord(lat, lng)` predicate appeared in 8 different files
 * (page.tsx, neighborhoods, restaurants, restaurants/[slug], films, film/[slug],
 * entity/[slug], houses, api/stars/nearby) with the same Number.isFinite +
 * non-zero shape. Single source of truth now.
 */

/**
 * True iff both `lat` and `lng` are finite numbers AND not the (0,0)
 * "Null Island" sentinel (which usually indicates missing geocoding rather
 * than a real coordinate). Use this before plotting on a map or running
 * a haversine calculation.
 */
export function isValidCoord(
  lat: unknown,
  lng: unknown
): lat is number {
  return (
    typeof lat === 'number' &&
    typeof lng === 'number' &&
    Number.isFinite(lat) &&
    Number.isFinite(lng) &&
    !(lat === 0 && lng === 0)
  );
}