@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.18 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { calendarDateOf } from "../../internal/calendarDateString.js";
import { halfOpenContainsSpan } from "../../internal/halfOpenIntervals.js";
// src/plain/interval/intervalEngulfsDate.ts
/**
* Return true when the half-open interval B `[bStart, bEnd)` lies within the half-open interval
* A `[aStart, aEnd)` — every day of B is also in A.
*
* - Half-open: an interval holds every `t` with `start <= t < end`. B may start at A's start and
* end at A's end, because neither end is part of either interval.
* - B must also overlap A, so an empty B (`bStart === bEnd`) counts only strictly inside A, never
* at an edge (CORE-6's `clampInterval` clamps it away there).
* - Equivalent to 4-argument `intervalContainsDate(aStart, aEnd, bStart, bEnd)`.
* - Uses `Temporal.PlainDate.compare` for comparison.
* - Returns `false` if either interval is invalid (`start > end`).
* - Returns `false` on invalid input (wrong type, malformed strings).
* - Accepts RFC 9557 calendar-annotated PlainDate strings — E5 (issue #78). Ordering is
* calendar-independent, so arguments 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 outer interval start, optionally calendar-annotated
* @param aEnd ISO 8601 date string for the outer interval end (excluded), optionally calendar-annotated
* @param bStart ISO 8601 date string for the inner interval start, optionally calendar-annotated
* @param bEnd ISO 8601 date string for the inner interval end (excluded), optionally calendar-annotated
* @returns true if B lies within A, or false on invalid input
*
* @example intervalEngulfsDate("2024-01-01", "2024-12-31", "2024-06-01", "2024-07-01") // true
* @example intervalEngulfsDate("2024-01-01", "2024-12-31", "2024-01-01", "2024-12-31") // true (equal intervals)
* @example intervalEngulfsDate("2024-01-01", "2024-12-31", "2024-06-01", "2024-12-31") // true (same end)
* @example intervalEngulfsDate("2024-01-01", "2024-12-31", "2024-06-01", "2024-06-01") // true (empty interval strictly inside)
* @example intervalEngulfsDate("2024-01-01", "2024-12-31", "2024-12-31", "2024-12-31") // false (empty interval at the edge)
* @example intervalEngulfsDate("2024-06-01", "2024-07-01", "2024-01-01", "2024-12-31") // false
*/
function intervalEngulfsDate(aStart, aEnd, bStart, bEnd) {
if (typeof aStart !== "string" || typeof aEnd !== "string" || typeof bStart !== "string" || typeof bEnd !== "string") return false;
const aS = calendarDateOf(aStart);
const aE = calendarDateOf(aEnd);
const bS = calendarDateOf(bStart);
const bE = calendarDateOf(bEnd);
if (aS === null || aE === null || bS === null || bE === null) return false;
try {
if (Temporal.PlainDate.compare(aS, aE) > 0) return false;
if (Temporal.PlainDate.compare(bS, bE) > 0) return false;
return halfOpenContainsSpan({
start: aS,
end: aE
}, {
start: bS,
end: bE
}, Temporal.PlainDate.compare);
} catch {
return false;
}
}
export { intervalEngulfsDate };