mergeIntervalsDate
Signature
Section titled “Signature”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 asintervalUnionDate). - Order of the input list does not matter; the result is sorted by start.
- Returns
[]for an empty list. - Returns
[]whenintervalsis not an array, when any element is not a{ start, end }record of valid ISO PlainDate strings, or when any element hasstart > end. - Accepts GMT calendar-annotated PlainDate strings — E5 (issue #78). Since the result is date values, every
start/endacross the whole list must carry the same calendar tag (or all be bare ISO); any mismatch returns[](E5 decision of record D4).
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
intervals |
{ start: string; end: string; }[] |
array of { start, end } records, optionally calendar-annotated |
Returns
Section titled “Returns”the minimum set of non-overlapping { start, end } records, sorted by start, or [] on invalid input / mismatched calendars
Examples
Section titled “Examples”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" }]) // []