UNPKG

@everwhen/temporal

Version:
74 lines 2.35 kB
import { isPlainYearMonth, isPoint } from './is.js'; export class Interval { constructor(start, end) { Object.defineProperty(this, "start", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "end", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.start = start; this.end = end; this.validate(); } static from(value) { if (isPlainYearMonth(value)) { return new Interval(value.startOfMonth(), value.endOfMonth()); } return new Interval(value.start, value.end); } static overlaps(a, b, options) { if (options?.inclusive) { return a.start.compare(b.end) <= 0 && a.end.compare(b.start) >= 0; } return a.start.compare(b.end) < 0 && a.end.compare(b.start) > 0; } validate() { if (this.start.compare(this.end) > 0) { throw new Error('Invalid interval: end cannot be before start'); } } isBefore(other) { return this.end.compare(other.start) < 0; } isAfter(other) { return this.start.compare(other.end) > 0; } equals(other) { return this.start.equals(other.start) && this.end.equals(other.end); } overlaps(other, options) { if (options?.inclusive) { return (this.start.compare(other.end) <= 0 && this.end.compare(other.start) >= 0); } return (this.start.compare(other.end) < 0 && this.end.compare(other.start) > 0); } contains(value) { if (isPoint(value)) { return this.start.compare(value) <= 0 && this.end.compare(value) >= 0; } return (this.start.compare(value.start) <= 0 && this.end.compare(value.start) >= 0 && this.start.compare(value.end) <= 0 && this.end.compare(value.end) >= 0); } toJSON() { return { start: this.start.toString(), end: this.end.toString(), }; } toString() { return `[${this.start.toString()}, ${this.end.toString()}]`; } get duration() { return this.start.until(this.end); } } //# sourceMappingURL=interval.js.map