Skip to content

mergeIntervalsUnix

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.
ParameterTypeDescription
intervals{ start: string | number; end: string | number; }[]array of { start, end } records

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

Members of each item

MemberTypeDescription
startnumberThe instant the interval begins at, as a Unix epoch number in the unit the arguments share.
endnumberThe 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.
import { mergeIntervalsUnix } from "@northguild/gmt/unix/interval";
mergeIntervalsUnix([{ start: 0, end: 1000000 }, { start: 500000, end: 1500000 }]) // [{ start: 0, end: 1500000 }]
intervals
output 

unix/interval/mergeIntervalsUnix.ts