@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.
58 lines (56 loc) • 3.25 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { parseCalendarDateValue } from "../../internal/calendarDateString.js";
import { halfOpenAbuts } from "../../internal/halfOpenIntervals.js";
import { isValidCalendarDate } from "../validate/isValidCalendarDate.js";
// src/plain/interval/intervalAbutsDate.ts
/**
* Return true when two half-open date intervals `[aStart, aEnd)` and `[bStart, bEnd)` are exactly
* adjacent — one ends where the other starts, so they share nothing and leave no gap.
*
* - Half-open: an interval holds every `t` with `start <= t < end`, so an interval's `end` is the
* first value after it. Returns `true` when `aEnd === bStart` or `bEnd === aStart` (Allen's
* "meets"). There is no one-day step: a one-day gap is a gap.
* - An empty interval (`start === end`) abuts nothing.
* - Uses `Temporal.PlainDate.compare` for comparison.
* - Returns `false` when intervals overlap, are disjoint with a gap, or are invalid.
* - Returns `false` on invalid input (wrong type, malformed strings).
* - Accepts RFC 9557 calendar-annotated PlainDate strings (as produced by `convertDateToCalendar`) —
* E5 (issue #78). Comparison is by calendar-independent ordering (`Temporal.PlainDate.compare`
* ignores calendar), so the four endpoints may carry different or no calendar tags.
* - Compatibility: since 1.16.0 calendar strings are RFC 9557 (ISO digits, `[u-ca=<id>]`, canonical
* calendar ids); see `isValidCalendarDate`.
*
* @param aStart ISO 8601 date string for the first interval start, optionally calendar-annotated
* @param aEnd ISO 8601 date string for the first interval end, optionally calendar-annotated
* @param bStart ISO 8601 date string for the second interval start, optionally calendar-annotated
* @param bEnd ISO 8601 date string for the second interval end, optionally calendar-annotated
* @returns true if intervals are exactly adjacent, or false on invalid input
*
* @example intervalAbutsDate("2024-01-01", "2024-06-30", "2024-06-30", "2024-12-31") // true
* @example intervalAbutsDate("2024-06-30", "2024-12-31", "2024-01-01", "2024-06-30") // true
* @example intervalAbutsDate("2024-01-01", "2024-06-30", "2024-07-01", "2024-12-31") // false ([01-01, 06-30) ends before 06-30)
* @example intervalAbutsDate("2024-01-01", "2024-07-01", "2024-06-30", "2024-12-31") // false (overlap)
* @example intervalAbutsDate("invalid", "2024-06-30", "2024-07-01", "2024-12-31") // false
*/
function intervalAbutsDate(aStart, aEnd, bStart, bEnd) {
if (typeof aStart !== "string" || typeof aEnd !== "string" || typeof bStart !== "string" || typeof bEnd !== "string") return false;
if (!isValidCalendarDate(aStart) || !isValidCalendarDate(aEnd) || !isValidCalendarDate(bStart) || !isValidCalendarDate(bEnd)) return false;
try {
const aS = parseCalendarDateValue(aStart);
const aE = parseCalendarDateValue(aEnd);
const bS = parseCalendarDateValue(bStart);
const bE = parseCalendarDateValue(bEnd);
if (Temporal.PlainDate.compare(aS, aE) > 0) return false;
if (Temporal.PlainDate.compare(bS, bE) > 0) return false;
return halfOpenAbuts({
start: aS,
end: aE
}, {
start: bS,
end: bE
}, Temporal.PlainDate.compare);
} catch {
return false;
}
}
export { intervalAbutsDate };