@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.
71 lines (69 loc) • 3.3 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { utcInstantOf } from "../../internal/utcInstant.js";
import { utcIntervalOf } from "../../internal/intervalChecks.js";
// src/utc/interval/intervalSplitAtUtc.ts
/**
* Split a UTC 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 `{ start, end }` records, each record's `end` equal to the next
* record's `start`.
* - Every piece is half-open `[start, end)`: a boundary belongs only to the piece that starts
* there, so the pieces share no value and together cover the interval exactly once (the rule
* CORE-6's `splitIntervalAt` uses).
* - 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, when any element is not a valid ISO UTC
* datetime string, or on invalid input (unparseable start/end, `start > end`, leap-second
* strings).
*
* @param start ISO UTC datetime string for the interval start
* @param end ISO UTC datetime string for the interval end
* @param points array of ISO UTC datetime strings to split at
* @returns array of `{ start, end }` records, or `[]` on invalid input
*
* @example intervalSplitAtUtc("2024-01-01T00:00:00Z", "2024-01-10T00:00:00Z", ["2024-01-05T00:00:00Z"]) // [{ start: "2024-01-01T00:00:00Z", end: "2024-01-05T00:00:00Z" }, { start: "2024-01-05T00:00:00Z", end: "2024-01-10T00:00:00Z" }]
* @example intervalSplitAtUtc("2024-01-01T00:00:00Z", "2024-01-10T00:00:00Z", []) // [{ start: "2024-01-01T00:00:00Z", end: "2024-01-10T00:00:00Z" }]
* @example intervalSplitAtUtc("invalid", "2024-01-10T00:00:00Z", ["2024-01-05T00:00:00Z"]) // []
*/
function intervalSplitAtUtc(start, end, points) {
try {
if (!Array.isArray(points)) return [];
const pair = utcIntervalOf(start, end);
if (pair === null) return [];
const parsedPoints = [];
if (!points.every((point) => {
const parsed = utcInstantOf(point);
if (parsed === null) return false;
parsedPoints.push(parsed);
return true;
})) return [];
try {
const [startVal, endVal] = pair;
const inRangePoints = parsedPoints.filter((point) => Temporal.Instant.compare(point, startVal) > 0 && Temporal.Instant.compare(point, endVal) < 0);
inRangePoints.sort(Temporal.Instant.compare);
const boundaries = [
startVal,
...inRangePoints.filter((point, index) => index === 0 || !point.equals(inRangePoints[index - 1])),
endVal
];
const result = [];
for (let i = 0; i < boundaries.length - 1; i++) result.push({
start: boundaries[i].toString(),
end: boundaries[i + 1].toString()
});
return result;
} catch {
return [];
}
} catch {
return [];
}
}
export { intervalSplitAtUtc };