@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
44 lines • 2.7 kB
TypeScript
/**
* Collapse a list of date intervals into the minimum set of non-overlapping intervals.
*
* - List-form generalization of `intervalUnionDate`, which is pairwise only.
* - Half-open: an interval holds every `t` with `start <= t < end`. Overlapping intervals and
* touching intervals (`aEnd === bStart`) join into one run; any gap, even one unit, keeps them
* apart (CORE-6's `mergeIntervals`).
* - An empty interval (`start === end`) holds nothing: it is absorbed by a run it touches or lies
* in, and dropped otherwise, so every returned interval is non-empty.
* - 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 RFC 9557 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 `[]`.
*
* @param intervals array of `{ start, end }` records, optionally calendar-annotated
* @returns the minimum set of non-overlapping `{ start, end }` records, sorted by start, or `[]` on invalid input / mismatched calendars
*
* @example 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" }]
* @example 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" }] (touching)
* @example mergeIntervalsDate([{ start: "2024-01-01", end: "2024-01-05" }, { start: "2024-01-06", end: "2024-01-10" }]) // [{ start: "2024-01-01", end: "2024-01-05" }, { start: "2024-01-06", end: "2024-01-10" }] (01-05 is in neither)
* @example mergeIntervalsDate([{ start: "2024-01-01", end: "2024-01-01" }]) // [] (empty interval)
* @example mergeIntervalsDate([]) // []
* @example mergeIntervalsDate([{ start: "2024-01-10", end: "2024-01-01" }]) // []
*/
export declare function mergeIntervalsDate(intervals: Array<{
start: string;
end: string;
}>): Array<{
/**
* The date the interval begins on, as an ISO 8601 date (`YYYY-MM-DD`). It keeps a calendar
* annotation such as `[u-ca=hebrew]` when the inputs carry one.
*/
start: string;
/**
* The first date after the interval, in the same format as `start`. It is exclusive: the interval
* holds everything from `start` up to but not including this value. It is always later than
* `start`.
*/
end: string;
}>;