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.

63 lines (61 loc) • 3 kB
import { parseUnixEpochInterval, parseUnixEpochValue } from "../../internal/unixEpochValue.js"; // src/unix/interval/intervalSplitAtUnix.ts /** * 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) */ function intervalSplitAtUnix(start, end, points) { try { if (!Array.isArray(points)) return []; const interval = parseUnixEpochInterval(start, end); if (interval === null) return []; const { start: startMs, end: endMs } = interval; const parsedPoints = []; for (const point of points) { const parsed = parseUnixEpochValue(point); if (parsed === null) return []; parsedPoints.push(parsed); } const inRangePoints = parsedPoints.filter((point) => point > startMs && point < endMs); inRangePoints.sort((a, b) => a - b); const boundaries = [ startMs, ...inRangePoints.filter((point, index) => index === 0 || point !== inRangePoints[index - 1]), endMs ]; const result = []; for (let i = 0; i < boundaries.length - 1; i++) result.push({ start: boundaries[i], end: boundaries[i + 1] }); return result; } catch { return []; } } export { intervalSplitAtUnix };