# mergeIntervalsDate

Industry: Core

## Signature

```ts
mergeIntervalsDate(intervals: { start: string; end: string; }[]): { start: string; end: string; }[]
```

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 `[]`.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `intervals` | `{ start: string; end: string; }[]` | 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

**Members of each item**

| Member | Type | Description |
| --- | --- | --- |
| `start` | `string` | 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. |
| `end` | `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`. |

## Examples

```ts
import { mergeIntervalsDate } from "@northguild/gmt/plain/interval";

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" }]
```

## Source

[plain/interval/mergeIntervalsDate.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/plain/interval/mergeIntervalsDate.ts)
