Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TypeScript · 22 lines · 841 B · at 2e2b18f

// Bug: strict < comparisons reject a slot that ends exactly at a busy start or at dayEnd, and busy is sorted in place.export type Interval = { start: number; end: number };export class SlotError extends Error {  constructor(public readonly kind: "invalid_input") {    super(kind);  }}export function findFreeSlot(busy: Interval[], dayStart: number, dayEnd: number, durationMin: number): number | undefined {  if (durationMin <= 0 || dayEnd <= dayStart) throw new SlotError("invalid_input");  for (const b of busy) {    if (b.end <= b.start) throw new SlotError("invalid_input");  }  busy.sort((a, b) => a.start - b.start);  let cursor = dayStart;  for (const b of busy) {    if (cursor + durationMin < b.start) break;    cursor = Math.max(cursor, b.end);  }  return cursor + durationMin < dayEnd ? cursor : undefined;}