Interval (@xtandard/lib/interval)
A generic, immutable interval over any ordered type — modeled on Guava’s
Range + DiscreteDomain. The engine and the built-in primitive domains live in
@xtandard/lib/interval (zero dependencies). Temporal-typed intervals (Instant,
ZonedDateTime, …) ship from @xtandard/lib/temporal (peer @js-temporal/polyfill) —
see Temporal intervals.
import { NumberInterval } from "@xtandard/lib/interval";
const range = new NumberInterval({ start: 0, startClose: true, end: 10, endClose: false }); // [0,10)range.contains(5); // truerange.union(NumberInterval.closedOpen(10, 20))?.toString(); // "[0,20)"NumberInterval.parse("(-Infinity,5]").contains(-3); // trueBuilt-in interval types
Section titled “Built-in interval types”Each is a new-able class; the same-named type is its instance type.
| Class | Element T |
Kind |
|---|---|---|
NumberInterval |
number |
continuous, extended line (±Infinity includable) |
IntegerInterval |
number |
discrete (ε 1) |
BigIntInterval |
bigint |
discrete (ε 1n) |
DateInterval |
Date |
continuous (ms) |
StringInterval |
string |
continuous, lexicographic (UTF-16 code-unit) |
Plus createOrdinalInterval([...]) for an ordered-label “enum” domain (discrete) — also
zero-dep, see Ordinal intervals. The TC39 Temporal types get
ready-made classes from @xtandard/lib/temporal (see Temporal intervals),
and there are dedicated entries for money, exact numbers, semver, and IP addresses (below).
You can also define your own for any ordered T (see Custom types).
Construction
Section titled “Construction”Spec object — explicit brackets, no defaults. Give a side via start+startClose
(both required together) or omit it for an unbounded side. Omitting a close flag for a
provided value is a compile error.
new NumberInterval({ start: 1, startClose: true, end: 5, endClose: false }); // [1,5)new NumberInterval({ start: 5, startClose: false }); // (5,+Infinity)new NumberInterval({ end: 5, endClose: false }); // (-Infinity,5)new NumberInterval(); // (-Infinity,+Infinity)// @ts-expect-error — start needs startClosenew NumberInterval({ start: 1, end: 5 });Statics: closed open closedOpen openClosed point atLeast greaterThan
atMost lessThan all empty · fromStart(start,len) fromEnd(end,len)
fromCenter(center,len) (half-open, need add) · parse(text).
NumberInterval.closedOpen(1, 5); // [1,5)NumberInterval.atLeast(0); // [0,+Infinity)NumberInterval.parse("[1,5]"); // [1,5]Operations
Section titled “Operations”All instance methods. Operations return base Interval<T> values (still
instanceof Interval).
- Queries:
contains(value)·encloses(other)·overlaps·isConnected(overlap or adjacent) ·isAdjacent·isBefore/isAfter·isEmptyisFullisPointisDiscrete·hasLowerBound/hasUpperBound·lowerValue()/upperValue(). - Set composition (mirrors JS
Set):union(→Interval | null, null = gap) ·intersection·difference(→Interval[], 0/1/2 pieces) ·symmetricDifference·isSubsetOf·isSupersetOf·isDisjointFrom. - Geometry:
span(other)(minimal enclosing, across gaps) ·gap(other)(the hole between disjoint intervals, or null) ·middle()(midpoint). - Measure / mutation:
length()(→number | null; null if unbounded) ·withStart(value, closed)/withEnd(value, closed)(immutable setters) ·equals(other). - Serialization:
toString()/format({ negativeInfinity, positiveInfinity }). - Many intervals at once:
mergeIntervals(domain, intervals)merges a bag into sorted disjoint pieces; for the full set algebra (complement, gaps, lookups) and interval → value maps, see Interval sets and maps.
// difference respects open/closed boundaries exactly:NumberInterval.closed(3, 10).difference(NumberInterval.open(4, 6)); // [3,4] ∪ [6,10]NumberInterval.closed(3, 10).difference(NumberInterval.closed(4, 6)); // [3,4) ∪ (6,10]How the pairwise operations relate, for two non-empty intervals a and b
(span is always the smallest interval enclosing both):
flowchart TD
AB["a, b (both non-empty)"] --> CONN{"a.isConnected(b)?"}
CONN -->|"no: disjoint"| DIS["intersection: empty<br/>union: null<br/>gap: the hole between them"]
CONN -->|yes| OVL{"a.overlaps(b)?"}
OVL -->|yes| OV["intersection: the shared part<br/>union: same as span<br/>gap: null"]
OVL -->|"no: isAdjacent"| ADJ["intersection: empty<br/>union: same as span<br/>gap: null"]
String form
Section titled “String form”[a,b] (a,b) [a,b) (a,b], unbounded (-Infinity,5] / [0,+Infinity), empty ∅.
The canonical token toString() emits is +Infinity / -Infinity; parse is lenient
and also accepts -inf / infinity / ∞ (case-insensitive), normalizing them to the
canonical form on output. Round-trips through toString(). Safe to store in DB columns /
URL params.