UNPKG

@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.

39 lines • 2.49 kB
/** * Split a Unix epoch interval at arbitrary `points`, producing consecutive sub-intervals. * * - `points` need not be sorted — they are sorted internally before splitting. * - Points before `start` or after `end` are dropped; they cannot introduce a boundary that * isn't inside the interval. * - Points exactly on `start` or `end` are dropped too — they would only produce a * zero-length sub-interval at the edge. * - Duplicate points collapse to a single boundary. * - Returns consecutive half-open `[start, end)` records, the same tiling as `splitIntervalAt`: * each record's `end` is the next record's `start` and belongs only to that next record, so the * pieces share no value and together cover `[start, end)` exactly once. * - Returns `[{ start, end }]` (the whole interval, unsplit) when no valid in-range point remains. * - A piece's `end` equals its `start` only when the interval itself is zero-length (`start` * equal to `end`): the result is then that one empty piece, whatever the points. * - Returns `[]` when `points` is not an array, or when `start`, `end` or any point is not a safe * integer (or numeric string of one) — fractions, empty strings and values beyond ±(2^53 − 1) * are invalid even when they would fall outside the interval — or when `start > end`. * * @param start Unix epoch value, in the one unit all epoch arguments share — interval start * @param end Unix epoch value, in the one unit all epoch arguments share — interval end * @param points array of Unix epoch values to split at * @returns array of `{ start, end }` records, or `[]` on invalid input * * @example intervalSplitAtUnix(0, 100000, [50000]) // [{ start: 0, end: 50000 }, { start: 50000, end: 100000 }] * @example intervalSplitAtUnix(0, 100000, []) // [{ start: 0, end: 100000 }] * @example intervalSplitAtUnix(NaN, 100000, [50000]) // [] * @example intervalSplitAtUnix(0, 100000, [50000.5]) // [] (fractional point) */ export declare function intervalSplitAtUnix(start: number | string, end: number | string, points: Array<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 can equal * `start`, which makes the interval empty. */ end: number; }>;