Utilities
These helpers let you check, combine, and save slots you already have.
They work with Date objects; timestamps in the examples below use UTC (Z).
| I want to… | Use |
|---|---|
| Build a slot from two dates | createTimeslot |
| Check whether two meetings conflict | overlaps |
| Check whether a moment falls inside a slot | contains |
| Combine bookings into continuous blocks | mergeSlots |
| Find open time between bookings | findGaps |
| Send slots as JSON and restore them | timeslotToJSON / timeslotFromJSON |
createTimeslot
createTimeslot(start: Date, end: Date): TimeslotUse this when you already have the start and end of a meeting. It creates a
validated Timeslot with copied dates and no metadata. Throws TypeError if either date is invalid, or
RangeError if end is not after start.
import { createTimeslot } from 'timeslottr'
const slot = createTimeslot(
new Date('2024-03-15T09:00:00Z'),
new Date('2024-03-15T09:30:00Z'),
)overlaps
overlaps(a: Timeslot, b: Timeslot): booleanUse this to check for a conflict between two meetings. It returns true if
they share any time. Uses half-open comparison, so slots that
merely touch at a boundary do not overlap.
import { overlaps, createTimeslot } from 'timeslottr'
const a = createTimeslot(new Date('2024-03-15T09:00:00Z'), new Date('2024-03-15T10:00:00Z'))
const b = createTimeslot(new Date('2024-03-15T09:30:00Z'), new Date('2024-03-15T10:30:00Z'))
overlaps(a, b) // truecontains
contains(slot: Timeslot, date: Date): booleanUse this to check whether a moment is during a meeting. It returns true at
the start and during the slot, and false at the exact end.
import { contains, createTimeslot } from 'timeslottr'
const slot = createTimeslot(
new Date('2024-03-15T09:00:00Z'),
new Date('2024-03-15T10:00:00Z'),
)
contains(slot, new Date('2024-03-15T09:30:00Z')) // true
contains(slot, new Date('2024-03-15T10:00:00Z')) // false (end is exclusive)mergeSlots
mergeSlots(slots: Timeslot[]): Timeslot[]Use this to turn a list of bookings into a smaller list of occupied blocks. It sorts slots by start time and merges any overlapping or adjacent slots into continuous blocks. Merged slots do not carry metadata from the originals.
import { mergeSlots, createTimeslot } from 'timeslottr'
const merged = mergeSlots([
createTimeslot(new Date('2024-03-15T09:00:00Z'), new Date('2024-03-15T10:00:00Z')),
createTimeslot(new Date('2024-03-15T09:30:00Z'), new Date('2024-03-15T11:00:00Z')),
createTimeslot(new Date('2024-03-15T13:00:00Z'), new Date('2024-03-15T14:00:00Z')),
])
// result: [09:00 to 11:00, 13:00 to 14:00]findGaps
findGaps(slots: Timeslot[], range: { start: Date; end: Date }): Timeslot[]Use this when you have bookings and want the open windows between them.
It returns gaps of any length, not fixed-duration meeting options. An empty
booking list leaves the whole range free. For 30-minute options around bookings,
use generateAvailableTimeslots.
import { findGaps, createTimeslot } from 'timeslottr'
const booked = [
createTimeslot(new Date('2024-03-15T09:00:00Z'), new Date('2024-03-15T10:00:00Z')),
createTimeslot(new Date('2024-03-15T11:00:00Z'), new Date('2024-03-15T12:00:00Z')),
]
const gaps = findGaps(booked, {
start: new Date('2024-03-15T08:00:00Z'),
end: new Date('2024-03-15T13:00:00Z'),
})
// result: [08:00 to 09:00, 10:00 to 11:00, 12:00 to 13:00]timeslotToJSON
timeslotToJSON(slot: Timeslot): TimeslotJSONConverts a Timeslot to a JSON-safe object with ISO 8601 string dates. Preserves
metadata if present.
import { createTimeslot, timeslotToJSON } from 'timeslottr'
const slot = createTimeslot(
new Date('2024-03-15T09:00:00Z'),
new Date('2024-03-15T09:30:00Z'),
)
const json = timeslotToJSON(slot)
// { start: '2024-03-15T09:00:00.000Z', end: '2024-03-15T09:30:00.000Z' }
const body = JSON.stringify(json) // ready for storage or an API requesttimeslotFromJSON
timeslotFromJSON(json: TimeslotJSON): TimeslotParses a TimeslotJSON back into a Timeslot with real Date instances.
Validates the dates and checks that the end is after the start. Use this when
reading a saved slot: JSON.parse by itself leaves the dates as strings.
import { timeslotFromJSON } from 'timeslottr'
const slot = timeslotFromJSON({
start: '2024-03-15T09:00:00.000Z',
end: '2024-03-15T09:30:00.000Z',
metadata: { index: 0, durationMinutes: 30 },
})