@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.
76 lines (74 loc) • 3.81 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { plainTimeOf } from "../../internal/inputChecks.js";
import { timeIntervalOf } from "../../internal/intervalChecks.js";
// src/plain/interval/intervalSplitAtTime.ts
/**
* Split a time 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; `intervalDivideEquallyTime`'s zero-length case is the
* deliberate way to express one, not `intervalSplitAtTime`.
* - 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 PlainTime
* string, or on invalid input (unparseable start/end, `start > end`).
*
* @param start ISO PlainTime string for the interval start
* @param end ISO PlainTime string for the interval end
* @param points array of ISO PlainTime strings to split at
* @returns array of `{ start, end }` records, or `[]` on invalid input
*
* @example intervalSplitAtTime("09:00:00", "17:00:00", ["12:00:00"]) // [{ start: "09:00:00", end: "12:00:00" }, { start: "12:00:00", end: "17:00:00" }]
* @example intervalSplitAtTime("09:00:00", "17:00:00", ["15:00:00", "11:00:00"]) // [{ start: "09:00:00", end: "11:00:00" }, { start: "11:00:00", end: "15:00:00" }, { start: "15:00:00", end: "17:00:00" }]
* @example intervalSplitAtTime("09:00:00", "17:00:00", ["09:00:00", "17:00:00", "20:00:00"]) // [{ start: "09:00:00", end: "17:00:00" }]
* @example intervalSplitAtTime("09:00:00", "17:00:00", []) // [{ start: "09:00:00", end: "17:00:00" }]
* @example intervalSplitAtTime("invalid", "17:00:00", ["12:00:00"]) // []
* @example intervalSplitAtTime("09:00:00", "17:00:00", ["not-a-time"]) // []
*/
function intervalSplitAtTime(start, end, points) {
try {
if (!Array.isArray(points)) return [];
const bounds = timeIntervalOf(start, end);
if (bounds === null) return [];
if (!points.every((point) => typeof point === "string")) return [];
const parsedPoints = [];
for (let index = 0; index < points.length; index++) {
if (!(index in points)) continue;
const point = points[index];
const parsedPoint = plainTimeOf(point);
if (parsedPoint === null) return [];
parsedPoints[index] = parsedPoint;
}
try {
const [startVal, endVal] = bounds;
const inRangePoints = parsedPoints.filter((point) => Temporal.PlainTime.compare(point, startVal) > 0 && Temporal.PlainTime.compare(point, endVal) < 0);
inRangePoints.sort(Temporal.PlainTime.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 { intervalSplitAtTime };