@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.
37 lines • 2.14 kB
TypeScript
/**
* 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`.
*
* @param intervals array of `{ start, end }` records
* @returns the minimum set of non-overlapping `{ start, end }` records, sorted by start, or `[]` on invalid input
*
* @example mergeIntervalsUnix([{ start: 0, end: 1000000 }, { start: 500000, end: 1500000 }]) // [{ start: 0, end: 1500000 }]
* @example mergeIntervalsUnix([{ start: 0, end: 1000000 }, { start: 1000000, end: 2000000 }]) // [{ start: 0, end: 2000000 }] (touching)
* @example mergeIntervalsUnix([{ start: 5, end: 5 }]) // [] (an empty interval holds no instant)
* @example mergeIntervalsUnix([]) // []
*/
export declare function mergeIntervalsUnix(intervals: Array<{
start: number | string;
end: number | string;
}>): Array<{
/** The instant the interval begins at, as a Unix epoch number in the unit the arguments share. */
start: 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`.
*/
end: number;
}>;