Functional Weave
Code in TypeScript

health.waiting-time@1.0.1

impl/typescript.ts

1,454 bytes · the TypeScript implementation · view raw

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

import { addDays, daysBetween, parseIsoDate } from "./dates_add_days.ts";  ← from dates.add-days ^1.0.0 · built alongside by fune
import { type RttWait } from "./health_waiting_time_types.ts";

/** 18 weeks: a wait of exactly 126 days is still within the standard. */
const EIGHTEEN_WEEKS_DAYS = 126;

/**
 * The length of a referral-to-treatment pathway, counted from the clock start
 * to the clock stop, or to onDate while the patient is still waiting. There
 * are no pauses: NHS England removed them in 2015.
 */
export function rttWaitingTime(clockStart: string, clockStop: string | null, onDate: string): RttWait {
  parseIsoDate(clockStart);
  parseIsoDate(onDate);
  if (onDate < clockStart) {
    throw new RangeError(`onDate ${onDate} is before clockStart ${clockStart}`);
  }
  if (clockStop !== null) {
    parseIsoDate(clockStop);
    if (clockStop < clockStart) {
      throw new RangeError(`clockStop ${clockStop} is before clockStart ${clockStart}`);
    }
    if (clockStop > onDate) {
      throw new RangeError(`clockStop ${clockStop} is after onDate ${onDate}`);
    }
  }
  const days = daysBetween(clockStart, clockStop ?? onDate);
  return {
    days,
    completedWeeks: Math.floor(days / 7),
    // The 0-1 week band holds eight days (0 to 7), every later band seven.
    weekBand: days <= 7 ? 1 : Math.floor((days + 6) / 7),
    breached18Weeks: days > EIGHTEEN_WEEKS_DAYS,
    eighteenWeekDate: addDays(clockStart, EIGHTEEN_WEEKS_DAYS),
    clockStopped: clockStop !== null,
  };
}