Skip to content

mergeIntervalsDate

mergeIntervalsDate(intervals: { start: string; end: string; }[]): { start: string; end: string; }[]
import { mergeIntervalsDate } from "@northguild/gmt/plain/interval";

Collapse a list of date intervals into the minimum set of non-overlapping intervals.

  • List-form generalization of intervalUnionDate, which is pairwise only.
  • Intervals are merged when they overlap or share an endpoint exactly (adjacent intervals, e.g. aEnd === bStart, ARE merged — same rule as intervalUnionDate).
  • Order of the input list does not matter; the result is sorted by start.
  • Returns [] for an empty list.
  • Returns [] when intervals is not an array, when any element is not a { start, end } record of valid ISO PlainDate strings, or when any element has start > end.
  • Accepts GMT calendar-annotated PlainDate strings — E5 (issue #78). Since the result is date values, every start/end across the whole list must carry the same calendar tag (or all be bare ISO); any mismatch returns [] (E5 decision of record D4).
Parameter Type Description
intervals { start: string; end: string; }[] array of { start, end } records, optionally calendar-annotated

the minimum set of non-overlapping { start, end } records, sorted by start, or [] on invalid input / mismatched calendars

mergeIntervalsDate([{ start: "2024-01-01", end: "2024-01-10" }, { start: "2024-01-05", end: "2024-01-15" }]) // [{ start: "2024-01-01", end: "2024-01-15" }]
mergeIntervalsDate([{ start: "2024-01-01", end: "2024-01-10" }, { start: "2024-01-10", end: "2024-01-20" }]) // [{ start: "2024-01-01", end: "2024-01-20" }] (adjacent, merged)
mergeIntervalsDate([{ start: "2024-01-01", end: "2024-01-05" }, { start: "2024-01-10", end: "2024-01-15" }]) // [{ start: "2024-01-01", end: "2024-01-05" }, { start: "2024-01-10", end: "2024-01-15" }] (disjoint)
mergeIntervalsDate([]) // []
mergeIntervalsDate([{ start: "2024-01-10", end: "2024-01-01" }]) // []

plain/interval/mergeIntervalsDate.ts