All files / src/utils location.ts

100% Statements 25/25
92.85% Branches 13/14
100% Functions 4/4
100% Lines 23/23

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            5x 5x 5x     5x         5x 5x       21x   4x 4x 4x   4x 4x     4x   3x 2x 2x 2x 2x           3x 1x           2x 1x           1x    
export function calculateDistance(
  lat1: number,
  lon1: number,
  lat2: number,
  lon2: number,
): number {
  const R = 6371000;
  const dLat = ((lat2 - lat1) * Math.PI) / 180;
  const dLon = ((lon2 - lon1) * Math.PI) / 180;
 
  const a =
    Math.sin(dLat / 2) ** 2 +
    Math.cos((lat1 * Math.PI) / 180) *
      Math.cos((lat2 * Math.PI) / 180) *
      Math.sin(dLon / 2) ** 2;
 
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  return R * c;
}
 
export function getOpenStatusText(openingHours: any): string {
  if (!openingHours?.periods) return "";
 
  const now = new Date();
  const todayJS = now.getDay();
  const today = todayJS === 0 ? 6 : todayJS - 1;
 
  const todayPeriod = openingHours.periods.find(
    (p: any) => p.open.day === today,
  );
 
  if (!todayPeriod) return "";
 
  const formatTime = (hour: number, minute: number) => {
    const date = new Date();
    date.setHours(hour);
    date.setMinutes(minute);
    return date.toLocaleTimeString([], {
      hour: "numeric",
      minute: "2-digit",
    });
  };
 
  if (openingHours.openNow && todayPeriod.close) {
    return `Closes at ${formatTime(
      todayPeriod.close.hour,
      todayPeriod.close.minute,
    )}`;
  }
 
  if (!openingHours.openNow && todayPeriod.open) {
    return `Opens at ${formatTime(
      todayPeriod.open.hour,
      todayPeriod.open.minute,
    )}`;
  }
 
  return "";
}