@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.
88 lines (86 loc) • 4.74 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { calendarDateOf } from "../../internal/calendarDateString.js";
import { calendarOfAllDateValues } from "../../internal/calendarValueOfDate.js";
import { formatDateInCalendar } from "../../internal/formatDateInCalendar.js";
import { dateIntervalOf } from "../../internal/intervalChecks.js";
// src/plain/interval/intervalSplitAtDate.ts
/**
* Split a date 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; `intervalDivideEquallyDate`'s zero-length case is the
* deliberate way to express one, not `intervalSplitAtDate`.
* - 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 PlainDate
* string, or on invalid input (unparseable start/end, `start > end`).
* - Accepts RFC 9557 calendar-annotated PlainDate strings — E5 (issue #78). `start`, `end`, and
* every element of `points` must carry the *same* calendar tag (or all be bare ISO); any
* mismatch returns `[]` — this also makes `.equals()`'s dedup of
* duplicate points safe, since same-calendar `PlainDate`s compare equal correctly.
*
* @param start ISO PlainDate string for the interval start, optionally calendar-annotated
* @param end ISO PlainDate string for the interval end, optionally calendar-annotated
* @param points array of ISO PlainDate strings to split at, optionally calendar-annotated (must match `start`/`end`'s calendar)
* @returns array of `{ start, end }` records, or `[]` on invalid input / mismatched calendars
*
* @example intervalSplitAtDate("2024-01-01", "2024-01-10", ["2024-01-05"]) // [{ start: "2024-01-01", end: "2024-01-05" }, { start: "2024-01-05", end: "2024-01-10" }]
* @example intervalSplitAtDate("2024-01-01", "2024-01-10", ["2024-01-07", "2024-01-03"]) // [{ start: "2024-01-01", end: "2024-01-03" }, { start: "2024-01-03", end: "2024-01-07" }, { start: "2024-01-07", end: "2024-01-10" }]
* @example intervalSplitAtDate("2024-01-01", "2024-01-10", ["2024-01-01", "2024-01-10", "2024-06-01"]) // [{ start: "2024-01-01", end: "2024-01-10" }] (start, end, and out-of-range points all drop)
* @example intervalSplitAtDate("2024-01-01", "2024-01-10", []) // [{ start: "2024-01-01", end: "2024-01-10" }]
* @example intervalSplitAtDate("invalid", "2024-01-10", ["2024-01-05"]) // []
* @example intervalSplitAtDate("2024-01-01", "2024-01-10", ["not-a-date"]) // []
*/
function intervalSplitAtDate(start, end, points) {
try {
if (!Array.isArray(points)) return [];
const bounds = dateIntervalOf(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 = calendarDateOf(point);
if (parsedPoint === null) return [];
parsedPoints[index] = parsedPoint;
}
const calendar = calendarOfAllDateValues([
start,
end,
...points
]);
if (!calendar) return [];
try {
const [startVal, endVal] = bounds;
const inRangePoints = parsedPoints.filter((point) => Temporal.PlainDate.compare(point, startVal) > 0 && Temporal.PlainDate.compare(point, endVal) < 0);
inRangePoints.sort(Temporal.PlainDate.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: formatDateInCalendar(boundaries[i], calendar),
end: formatDateInCalendar(boundaries[i + 1], calendar)
});
return result;
} catch {
return [];
}
} catch {
return [];
}
}
export { intervalSplitAtDate };