All files / src/data ShuttleSchedule.ts

100% Statements 20/20
80% Branches 8/10
100% Functions 9/9
100% Lines 19/19

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                    1x 1x     36x 36x       22x 22x 22x       8x 8x       9x     10x 10x                 8x   7x       7x   7x 33x 33x   22x                
export type CampusKey = "sgw" | "loyola";
 
export type DepartureComputed = {
  id: string;
  time: string;
  from: string;
  eta: string;
  countdownMin: number;
};
 
const SHUTTLE_TRIP_MINUTES = 30;
const NEXT_COUNT = 4;
 
export function toMinutes24(time: string) {
  const [h, m] = time.split(":").map(Number);
  return h * 60 + m;
}
 
function minutesTo24h(min: number) {
  const h = Math.floor(min / 60);
  const m = min % 60;
  return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
}
 
export function nowMinutes() {
  const d = new Date();
  return d.getHours() * 60 + d.getMinutes();
}
 
export function isFriday() {
  return new Date().getDay() === 5;
}
export function isWeekend() {
  const d = new Date().getDay();
  return d === 0 || d === 6;
}
 
export function computeNextDepartures(
  campusKey: CampusKey,
  campusName: string,
  monThu: Record<CampusKey, string[]>,
  friday: Record<CampusKey, string[]>,
): DepartureComputed[] {
  if (isWeekend()) return [];
 
  const todayTimes = isFriday()
    ? (friday[campusKey] ?? [])
    : (monThu[campusKey] ?? []);
 
  const now = nowMinutes();
 
  return todayTimes
    .map((t) => ({ timeStr: t, depMin: toMinutes24(t) }))
    .filter((x) => x.depMin > now)
    .slice(0, NEXT_COUNT)
    .map((x, i) => ({
      id: `${campusKey}-${i}-${x.timeStr}`,
      time: x.timeStr,
      from: campusName,
      countdownMin: x.depMin - now,
      eta: `${minutesTo24h(x.depMin + SHUTTLE_TRIP_MINUTES)} ETA`,
    }));
}