# mergeIntervalsUnix

Industry: Core

## Signature

```ts
mergeIntervalsUnix(intervals: { start: string | number; end: string | number; }[]): { start: number; end: number; }[]
```

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

- List-form generalization of `intervalUnionUnix`, which is pairwise only.
- Intervals are half-open `[start, end)`, the same rule as `mergeIntervals`. They are merged when they overlap or touch (one's `end` equals the other's `start`), so the runs returned never touch.
- An empty interval (`start === end`) holds no instant: it is absorbed when it touches or lies inside a run and dropped otherwise, so it never bridges a gap. A list of only empty intervals returns `[]`, the same value as invalid input — use `isValidUnixInterval` to tell them apart.
- 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 safe-integer (or numeric-string) values — fractions, empty strings and values beyond ±(2^53 − 1) are invalid — or when any element has `start > end`.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `intervals` | `{ start: string \| number; end: string \| number; }[]` | array of `{ start, end }` records |

## Returns

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

**Members of each item**

| Member | Type | Description |
| --- | --- | --- |
| `start` | `number` | The instant the interval begins at, as a Unix epoch number in the unit the arguments share. |
| `end` | `number` | The first instant after the interval, in the same unit 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 { mergeIntervalsUnix } from "@northguild/gmt/unix/interval";

mergeIntervalsUnix([{ start: 0, end: 1000000 }, { start: 500000, end: 1500000 }]) // [{ start: 0, end: 1500000 }]
```

## Source

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