All files / src/utils navigationUtils.ts

97.29% Statements 36/37
89.28% Branches 25/28
100% Functions 5/5
100% Lines 29/29

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 70 71 72 73 74 75 76 77 78          26x   21x 21x   21x 21x 58x 58x 29x 29x 11x 18x 18x         21x   20x   20x               10x 40x 160x 10x         7x   7x 7x     7x 4x     4x   3x     3x   4x               14x                  
import { getDistance, isPointWithinRadius } from "geolib";
import { BUILDINGS } from "../data/buildings";
import { Coordinate } from "../type";
 
export function calculateETA(duration: string) {
  if (!duration || duration === "N/A") return "--:--";
 
  const now = new Date();
  let totalMinutes = 0;
 
  const tokens = duration.toLowerCase().split(/\s+/);
  for (let i = 0; i < tokens.length; i++) {
    const val = Number.parseInt(tokens[i], 10);
    if (!Number.isNaN(val)) {
      const nextToken = tokens[i + 1] || "";
      if (nextToken.includes("hour")) {
        totalMinutes += val * 60;
      } else Eif (nextToken.includes("min")) {
        totalMinutes += val;
      }
    }
  }
 
  if (totalMinutes === 0) return "--:--";
 
  now.setMinutes(now.getMinutes() + totalMinutes);
 
  return now.toLocaleTimeString([], {
    hour: "2-digit",
    minute: "2-digit",
    hour12: false,
  });
}
 
//FUNCTIONS AND VARIABLES TO CHECK IF SHUTTLE IS A VIABLE MODE OF TRANSPORT
const DEFAULT_RADIUS = 1000;
const SGW_STOP = BUILDINGS.find((b) => b.id === "H")?.marker;
const LOYOLA_STOP = BUILDINGS.find((b) => b.id === "VE")?.marker;
const RADIUS = 3000;
export function checkAndGetViableShuttleDestination(
  targetCoords: Coordinate,
  origin: Coordinate,
) {
  Iif (!LOYOLA_STOP || !SGW_STOP) return false;
 
  const distFromLoyola = getDistance(LOYOLA_STOP, targetCoords);
  const distFromSGW = getDistance(SGW_STOP, targetCoords);
  let dest_shuttle: string;
 
  if (distFromLoyola < distFromSGW) {
    dest_shuttle = isWithinRadius(LOYOLA_STOP, targetCoords, DEFAULT_RADIUS)
      ? "LOYOLA"
      : "";
    if (isWithinRadius(LOYOLA_STOP, origin, RADIUS)) return false;
  } else {
    dest_shuttle = isWithinRadius(SGW_STOP, targetCoords, DEFAULT_RADIUS)
      ? "SGW"
      : "";
    if (isWithinRadius(SGW_STOP, origin, RADIUS)) return false;
  }
  return dest_shuttle;
}
 
function isWithinRadius(
  shuttle_stop: Coordinate,
  targetCoords: Coordinate,
  radius: number,
) {
  return isPointWithinRadius(
    {
      latitude: shuttle_stop.latitude,
      longitude: shuttle_stop.longitude,
    },
    { latitude: targetCoords.latitude, longitude: targetCoords.longitude },
    radius,
  );
}