Skip to content

intervalSplitAtDate

intervalSplitAtDate(start: string, end: string, points: string[]): { start: string; end: string; }[]
import { intervalSplitAtDate } from "@northguild/gmt/plain/interval";

Split a date interval at arbitrary points, producing consecutive sub-intervals.

  • points need not be sorted — they are sorted internally before splitting.
  • Points outside [start, 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, which divideEqually’s zero-length case is the deliberate way to express, not splitAt’s.
  • Duplicate points collapse to a single boundary.
  • Returns [{ start, end }] (the whole interval, unsplit) when no valid in-range point remains.
  • 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 GMT 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 [] (E5 decision of record D4) — this also makes .equals()’s dedup of duplicate points safe, since same-calendar PlainDates compare equal correctly.
Parameter Type Description
start string ISO PlainDate string for the interval start, optionally calendar-annotated
end string ISO PlainDate string for the interval end, optionally calendar-annotated
points string[] array of ISO PlainDate strings to split at, optionally calendar-annotated (must match start/end’s calendar)

array of { start, end } records, or [] on invalid input / mismatched calendars

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" }]
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" }]
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)
intervalSplitAtDate("2024-01-01", "2024-01-10", []) // [{ start: "2024-01-01", end: "2024-01-10" }]
intervalSplitAtDate("invalid", "2024-01-10", ["2024-01-05"]) // []
intervalSplitAtDate("2024-01-01", "2024-01-10", ["not-a-date"]) // []

plain/interval/intervalSplitAtDate.ts