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.

47 lines (45 loc) • 3.02 kB
import { canonicalInstantIntervals } from "../../internal/instantIntervalText.js"; import { mergeIntervals } from "../../interval/calculate/mergeIntervals.js"; import { isValidUtc } from "../validate/isValidUtc.js"; // src/utc/interval/intervalUnionUtc.ts /** * Return the combined span of two UTC intervals, or null when they are disjoint. * * - Half-open: an interval holds every `t` with `start <= t < end`. The union is returned only * when it is one non-empty interval — the single run CORE-6's `mergeIntervals` would give. * - Overlapping intervals, and touching intervals (`aEnd === bStart`), return their combined span. * - Two **non-empty** intervals with any gap between them return `null`, even a one-unit gap. * The caveat matters: an empty interval is the empty set, so it is never “separated” from * anything. A non-empty interval unioned with an empty one far away is still that non-empty * interval — see the next bullet — not `null`. * - An empty interval (`start === end`) is the empty set: it adds nothing, so the union is the * other interval. Two empty intervals have no non-empty union and return `null`. * - Delegates to CORE-6's `mergeIntervals` once the arguments pass the UTC-string gate, and * re-serialises the run to canonical `Z` strings. * - Returns `null` if either interval is invalid (`start > end`). * - Returns `null` on invalid input (wrong type, non-`Z` strings, malformed strings, leap seconds). * * @param aStart ISO 8601 UTC datetime string for the first interval start * @param aEnd ISO 8601 UTC datetime string for the first interval end * @param bStart ISO 8601 UTC datetime string for the second interval start * @param bEnd ISO 8601 UTC datetime string for the second interval end * @returns `{ start, end }` with the merged span, or null on invalid input / disjoint intervals * * @example intervalUnionUtc("2024-01-01T09:00:00Z", "2024-01-01T13:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T17:00:00Z") // { start: "2024-01-01T09:00:00Z", end: "2024-01-01T17:00:00Z" } * @example intervalUnionUtc("2024-01-01T09:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T17:00:00Z") // { start: "2024-01-01T09:00:00Z", end: "2024-01-01T17:00:00Z" } (touching) * @example intervalUnionUtc("2024-01-01T09:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T12:00:00.000000001Z", "2024-01-01T17:00:00Z") // null (1 ns gap) * @example intervalUnionUtc("invalid", "2024-06-30T23:59:59Z", "2024-04-01T00:00:00Z", "2024-12-31T23:59:59Z") // null */ function intervalUnionUtc(aStart, aEnd, bStart, bEnd) { if (typeof aStart !== "string" || typeof aEnd !== "string" || typeof bStart !== "string" || typeof bEnd !== "string") return null; if (!isValidUtc(aStart) || !isValidUtc(aEnd) || !isValidUtc(bStart) || !isValidUtc(bEnd)) return null; const runs = mergeIntervals([{ start: aStart, end: aEnd }, { start: bStart, end: bEnd }]); return runs.length === 1 ? canonicalInstantIntervals(runs)?.[0] ?? null : null; } export { intervalUnionUtc };