Functional Weave
Code in TypeScript

health.appointment-slots Unreviewed

Bookable appointment slots from clinic sessions, a slot length, breaks and existing bookings.

1.0.1 · published 2026-10-03 by charlie · Anterra

Pinned by 20 tests, run in TypeScript, Python and Rust.

Unreviewed. This capability’s implementations agree in every language and pass its published test vectors, which were worked out from the official sources cited. But no qualified clinician has yet checked those vectors, or confirmed that the capability covers the cases it claims. Treat it as a draft. Do not use it for real people, money or decisions without your own expert review. Once a qualified reviewer signs off, this notice is replaced with their name, qualification and the date. Each new version needs fresh sign-off.

Not professional advice. This capability calculates health figures from published rules. It is a software component for developers, not medical advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed in its README, and have a clinician review how you use it, before anyone relies on the output. Provided “as is” under its licence, without warranty.

Not a medical device. It is not intended to diagnose, treat or support clinical decisions about any individual. Anyone building it into clinical software is responsible for that software’s regulatory status, and must validate it under their own clinical governance.

What it does

Lists the appointment slots that can still be booked. It takes clinic sessions (a date with a start and end time), a slot length, breaks and the bookings already made. It is pure scheduling arithmetic on wall-clock times: no time zones, no clock reads, and the same input always gives the same list.

## How the slots are laid out

For example

  • appointmentSlots(sessions ×1, 15, , ) → ×12 a 09:00-12:00 morning in 15-minute slots is twelve slots
  • appointmentSlots(sessions ×1, 20, breaks ×1, ) → ×20 a daily 12:30-13:30 lunch splits 09:00-17:00; 20-minute slots restart at 13:30 and the 10 minutes left before lunch and at the end are dropped
  • appointmentSlots(sessions ×2, 30, breaks ×1, ) → ×3 a break with a date applies to that day only

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

export function appointmentSlots(sessions: readonly ClinicSession[], slotMinutes: number, breaks: readonly ClinicBreak[], bookings: readonly Booking[]): readonly AppointmentSlot[]
sessionsClinicSession[]clinic sessions in any order; sessions on one date must not overlap
slotMinutesintthe length of every slot, 1 to 480
breaksClinicBreak[]times the clinic is closed inside a session; a null date means every day
bookingsBooking[]appointments already booked; a slot they overlap is not offered
returnsAppointmentSlot[]the free slots, by date then start time

The types it declares, generated into your project

/** One clinic session on one day; it cannot cross midnight. */
export interface ClinicSession {
  readonly date: string;
  /** HH:MM, 24-hour */
  readonly start: string;
  /** HH:MM, after start */
  readonly end: string;
}

/** A break inside sessions, such as lunch; slots restart after it. */
export interface ClinicBreak {
  /** null for a break on every session day */
  readonly date: string | null;
  /** HH:MM */
  readonly start: string;
  /** HH:MM, after start */
  readonly end: string;
}

/** An existing appointment. */
export interface Booking {
  readonly date: string;
  /** HH:MM */
  readonly start: string;
  /** HH:MM, after start */
  readonly end: string;
}

/** One bookable slot. */
export interface AppointmentSlot {
  readonly date: string;
  /** HH:MM */
  readonly start: string;
  /** HH:MM */
  readonly end: string;
}

Your code names it in one line, in the file that uses it

import { appointmentSlots } from "#fune/health.appointment-slots@^1";
impl/typescript.ts · 85 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { minutesBetween } from "./time_minutes_between.ts";  ← from time.minutes-between ^1.0.0 · built alongside by fune
import { type AppointmentSlot, type Booking, type ClinicBreak, type ClinicSession } from "./health_appointment_slots_types.ts";

/** A break without a date still needs a real date for the time check. */
const ANY_DATE = "2000-01-01";

interface Interval {
  date: string;
  start: number;
  end: number;
}

/** Minute of the day, 0 to 1439; throws on a malformed date or time. */
function minuteOfDay(date: string, time: string): number {
  return minutesBetween(date, "00:00", date, time);
}

function interval(kind: string, date: string | null, start: string, end: string): Interval {
  const on = date === null ? ANY_DATE : date;
  const from = minuteOfDay(on, start);
  const to = minuteOfDay(on, end);
  if (to <= from) {
    throw new RangeError(`${kind} on ${date === null ? "every day" : date} must end after it starts (${start} to ${end})`);
  }
  return { date: on, start: from, end: to };
}

function clock(minute: number): string {
  const hours = Math.floor(minute / 60);
  const minutes = minute % 60;
  return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
}

/**
 * Free appointment slots. Breaks split a session into segments and the slot
 * grid restarts at the start of each segment, so a 12:30-13:30 lunch gives
 * slots from 13:30 rather than from wherever the morning grid would have
 * landed. Bookings only take slots away: they never move the grid, because
 * they are booked into slots. Intervals are half-open, so a booking ending at
 * 09:15 does not touch the 09:15 slot.
 */
export function appointmentSlots(
  sessions: readonly ClinicSession[],
  slotMinutes: number,
  breaks: readonly ClinicBreak[],
  bookings: readonly Booking[],
): readonly AppointmentSlot[] {
  if (!Number.isInteger(slotMinutes) || slotMinutes < 1 || slotMinutes > 480) {
    throw new RangeError(`slotMinutes must be a whole number from 1 to 480, received ${slotMinutes}`);
  }
  const clinic = sessions.map((s) => interval("session", s.date, s.start, s.end));
  const closed = breaks.map((b) => ({ always: b.date === null, ...interval("break", b.date, b.start, b.end) }));
  const booked = bookings.map((b) => interval("booking", b.date, b.start, b.end));

  clinic.sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : a.start - b.start));
  for (let i = 1; i < clinic.length; i++) {
    if (clinic[i].date === clinic[i - 1].date && clinic[i].start < clinic[i - 1].end) {
      throw new RangeError(`sessions overlap on ${clinic[i].date}`);
    }
  }

  const slots: AppointmentSlot[] = [];
  for (const session of clinic) {
    const pauses = closed
      .filter((b) => b.always || b.date === session.date)
      .sort((a, b) => a.start - b.start || a.end - b.end);
    const segments: [number, number][] = [];
    let cursor = session.start;
    for (const pause of pauses) {
      if (pause.end <= cursor || pause.start >= session.end) continue;
      if (pause.start > cursor) segments.push([cursor, pause.start]);
      cursor = Math.max(cursor, pause.end);
    }
    if (cursor < session.end) segments.push([cursor, session.end]);

    for (const [from, to] of segments) {
      for (let t = from; t + slotMinutes <= to; t += slotMinutes) {
        const end = t + slotMinutes;
        const taken = booked.some((b) => b.date === session.date && b.start < end && b.end > t);
        if (!taken) slots.push({ date: session.date, start: clock(t), end: clock(end) });
      }
    }
  }
  return slots;
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add health.appointment-slots
Download for TypeScript health.appointment-slots-1.0.1-typescript.fune · 19,633 bytes sha256 577c6ff447070065b3161261d8e05525b4d24e66f422371c13427c5b7e224973

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./health.appointment-slots-1.0.1-typescript.fune, or fetch it from a terminal with fune pull health.appointment-slots@1.0.1:typescript.

The whole function, every language, is one file too: health.appointment-slots-1.0.1.fune, 28,395 bytes, sha256 9b81b0c2ed4032adce8b962d58a67032ce0817f7dcf79e05cf4ec899e66e00a1. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

// fune: before health.appointment-slots

after — your function gets the result and the arguments, and returns the final result.

// fune: after health.appointment-slots

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

// fune: replace time.minutes-between in health.appointment-slots

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show health.appointment-slots --steps.

// fune: step health.appointment-slots after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
a 09:00-12:00 morning in 15-minute slots is twelve slots sessions ×1, 15, , → ×12
a daily 12:30-13:30 lunch splits 09:00-17:00; 20-minute slots restart at 13:30 and the 10 minutes left before lunch and at the end are dropped sessions ×1, 20, breaks ×1, → ×20
a break with a date applies to that day only sessions ×2, 30, breaks ×1, → ×3
a booking straddling two slots removes both sessions ×1, 15, , bookings ×1 → ×2
bookings that only touch a slot's edge do not remove it sessions ×1, 15, , bookings ×2 → ×3
a booking that does not fill its slot does not shift the grid sessions ×1, 20, , bookings ×1 → ×2
bookings on other dates are ignored sessions ×1, 15, , bookings ×1 → ×4
sessions given out of order come back by date and time sessions ×3, 30, , → ×5
a break overlapping the session start moves the first slot to the break's end sessions ×1, 30, breaks ×1, → ×3
a break covering the whole session leaves no slots sessions ×1, 15, breaks ×1, →
Show the other 10 tests
CaseArgumentsExpected
a slot longer than the session gives no slots sessions ×1, 45, , →
no sessions, no slots , 15, , →
sessions that overlap on one day are an error sessions ×2, 15, , → error: sessions overlap on 2026-10-05
a time without a leading zero is an error sessions ×1, 15, , → error: is not a time of day
24:00 is not a time of day sessions ×1, 15, , → error: is not a time of day
a session cannot cross midnight: 22:00-00:00 ends before it starts sessions ×1, 15, , → error: session on 2026-10-05 must end after it starts
a booking that ends before it starts is an error sessions ×1, 15, , bookings ×1 → error: booking on 2026-10-05 must end after it starts
an impossible date is an error sessions ×1, 15, , → error: is not a real calendar date
a zero slot length is an error sessions ×1, 0, , → error: slotMinutes must be a whole number from 1 to 480
a fractional slot length is an error sessions ×1, 7.5, , → error: slotMinutes must be a whole number from 1 to 480

More from the author

- **Breaks reshape a session.** A break (lunch, a team meeting) splits the session into free segments. Slots are laid back to back from the start of each segment, so after a 12:30-13:30 lunch the first slot is at 13:30, not wherever the morning's grid would have landed. A break with a null date applies to every session day. A break with a date applies to that day only. - **Leftovers are dropped.** A slot must fit wholly inside its segment. With 20-minute slots, 09:00-12:30 gives ten slots ending at 12:20, and the last 10 minutes are not offered. - **Bookings only take slots away.** A slot that overlaps any booking on the same date is removed, but the grid does not move. Bookings are made into slots, so a 5-minute booking at 09:05 removes the 09:00 slot and leaves 09:20 where it was. A booking across two slots removes both. - **Touching is not overlapping.** Intervals are half-open (start included, end excluded), so a booking ending at 09:15 leaves the 09:15 slot free.

Sessions may be given in any order. The result is sorted by date, then start time.

## Errors

The function refuses, rather than guessing, when:

- a time is not `HH:MM` from 00:00 to 23:59 (`9:00` and `24:00` are refused) - a date is not a real ISO date - a session, break or booking does not end after it starts - two sessions on the same date overlap - `slotMinutes` is not a whole number from 1 to 480

A session cannot cross midnight. Split an overnight clinic into two sessions, one on each date.

## What it does not do

It does not handle clinicians, rooms or appointment types. Call it once per clinician or resource. It does not handle double-booking capacity (more than one patient per slot) or daylight-saving changes. It does not keep slots in the past out of the list, because it never reads the clock: pass only the sessions still to come.

## Before you rely on this

**Not professional advice.** This capability calculates health figures from published rules. It is a software component for developers, not medical advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed above, and have a clinician review how you use it, before anyone relies on the output. Provided "as is" under its licence, without warranty.

**Not a medical device.** It is not intended to diagnose, treat or support clinical decisions about any individual. Anyone building it into clinical software is responsible for that software's regulatory status, and must validate it under their own clinical governance.

**Unreviewed.** This capability's implementations agree in every language and pass its published test vectors, which were worked out from the official sources cited. But no qualified clinician has yet checked those vectors, or confirmed that the capability covers the cases it claims. Treat it as a draft. Do not use it for real people, money or decisions without your own expert review. Once a qualified reviewer signs off, this notice is replaced with their name, qualification and the date. Each new version needs fresh sign-off.

1.0.1 marks it unreviewed. The code and the tests are unchanged.

Files

PathBytes
README.md3,561
impl/python.py3,310
impl/rust.rs5,132
impl/typescript.ts3,510
vectors.json7,400