Availability primitives
Use these functions when you want to work with free time directly.
subtract answers “what’s left after these bookings?” and intersect answers
“when is everyone free?” Their results are windows of any length, rather than
fixed-length meeting slots.
Both take Interval objects: { start: Date; end: Date }. Convert calendar data
to Date objects before calling them. The examples below use UTC timestamps.
For time-only strings or ready-to-book meeting slots, use
generateAvailableTimeslots.
subtract
subtract(source: Interval[], busy: Interval[]): Interval[]Removes the busy intervals from the source (available) intervals and returns
the free time that is left. The busy list can be unsorted, overlapping, or
touching boundaries. A busy window in the middle of a source splits it in two.
A booking that ends exactly when availability begins leaves that availability
intact. See half-open intervals.
import { subtract } from 'timeslottr'
const free = subtract(
[{ start: new Date('2024-01-01T09:00:00Z'), end: new Date('2024-01-01T17:00:00Z') }],
[{ start: new Date('2024-01-01T12:00:00Z'), end: new Date('2024-01-01T13:00:00Z') }],
)
// result: [09:00 to 12:00, 13:00 to 17:00]intersect
intersect(intervalSets: Interval[][]): Interval[]Given one set of free intervals per person, it returns the windows where all of
them overlap. With no sets it returns []. With one set it returns a merged copy.
If any set is empty the result is [], since a person with no availability blocks
everyone.
import { intersect } from 'timeslottr'
const team = intersect([
[{ start: new Date('2024-01-01T09:00:00Z'), end: new Date('2024-01-01T15:00:00Z') }],
[{ start: new Date('2024-01-01T11:00:00Z'), end: new Date('2024-01-01T17:00:00Z') }],
])
// result: [11:00 to 15:00]Empty lists: free time and busy time mean different things
| Call | Meaning | Result |
|---|---|---|
subtract(availability, []) | No bookings to remove | All original availability |
subtract([], busy) | No availability to start with | [] |
intersect([]) | No participants supplied | [] |
intersect([alice, []]) | One person has no free time | [] |
intersect([alice]) | Only one person’s free time | A merged copy of that person’s windows |
An empty free list blocks shared availability. By comparison, an empty busy
list in participantsBusy means that person has no bookings to block time.
Both return new Interval objects with fresh Date instances, so your inputs are
not mutated. See the Interval type.