Skip to content

@xtandard/lib/interval

A generic, immutable Interval over any ordered type (Guava Range + DiscreteDomain model), plus built-in domains for the JS primitives (number, integer, bigint, Date), and IntervalSet / IntervalMap (Guava RangeSet / RangeMap) over any domain. Zero runtime dependencies.

See docs/INTERVAL.md for the full guide.

import { … } from "@xtandard/lib/interval";
Name Summary
createOrdinalInterval Build a DISCRETE interval class over an ordered list of string labels (a “small enum”: sizes XS < S < M < L < XL, priority/log levels, …). Ordering is the list position, so adjacent labels merge and length() counts labels.
defineIntervalType Bind a IntervalDomain to a concrete, new-able interval CLASS that extends Interval, so instances are instanceof Interval (and instanceof the bound class). The built-in classes (NumberInterval, DateInterval, …) are made this way; use it for your own domains.
mergeIntervals Merge a bag of intervals into the minimal sorted set of disjoint pieces: overlapping or adjacent intervals are combined, empty ones are dropped, and the result is ordered by lower bound. Pass the domain the intervals use.
parseInterval Parse [a,b), (-Infinity,5], ∅, etc. into an Interval<T> via domain.parse. The unbounded token is lenient — inf / infinity / ∞, any case, with an optional +/- sign — but toString() always emits the canonical ±Infinity. Empty text or ∅ gives the empty interval. Discrete domains are canonicalized, so (0,5] over the integers reads back as [1,5].
Name Summary
Interval An immutable interval over an ordered type T, with set algebra (union/intersection/difference/…) decided entirely by an injected IntervalDomain. Build instances with the static Interval.of (or, more ergonomically, a domain-bound class from defineIntervalType); new Interval(...) is blocked because the constructor is protected. Every operation returns a fresh Interval — nothing mutates in place.
IntervalMap An immutable map from disjoint intervals of T to values V (Guava’s RangeMap). Each value of T maps to at most one V; IntervalMap.put overwrites whatever it overlaps, splitting the entries it cuts through, and IntervalMap.putCoalescing also merges the new entry with connected neighbours that hold an equal value. Every operation returns a new map.
IntervalSet An immutable set of values of T, stored as the minimal sorted list of disjoint, non-connected intervals (Guava’s RangeSet). Adding an interval coalesces it with every interval it overlaps or touches, so on a discrete domain [1,3] ∪ [4,6] is stored as [1,6], and on a continuous one [1,2) ∪ [2,3] as [1,3]. Every operation returns a new set.
Name Summary
BoundIntervalType The shape of a domain-bound interval CLASS produced by defineIntervalType: a new-able constructor plus the static factory methods. The explicit type is the public return type of defineIntervalType (not the local anonymous class) so .d.ts generation never has to synthesize an inline type literal for it.
IntervalDomain Describes how to order, step, measure, and serialize values of T.
IntervalMapOptions Options for IntervalMap.of / IntervalMap.empty.
Name Summary
BigIntInterval Instance type of BigIntInterval.
Bound One endpoint. Unbounded ⇒ ±∞; its closed flag matters only when the domain declares that infinity as a value (else it is forced open).
DateInterval Instance type of DateInterval.
IntegerInterval Instance type of IntegerInterval.
IntervalMapEntry One [interval, value] pair of an IntervalMap (the shape Map entries have).
IntervalSpec Friendly constructor spec — explicit brackets, NO defaults. Give a side via its flat keys (start + startClose, both required together), or omit it entirely for an unbounded (±∞) side. Supplying start without startClose (or end without endClose) is a compile error.
NumberInterval Instance type of NumberInterval.
StringInterval Instance type of StringInterval.
Name Summary
bigIntDomain Big integers — DISCRETE (epsilon 1n). Exact past Number.MAX_SAFE_INTEGER; only length() converts to a number. Bound to a class as BigIntInterval.
BigIntInterval Interval over bigints (bigIntDomain, discrete, ε 1n).
dateDomain Date timestamps — CONTINUOUS (preserves [ISO,ISO]); measure/add in ms. Invalid Dates fail isValid. Bound to a class as DateInterval.
DateInterval Interval over Dates (dateDomain, continuous, ms-measured).
integerDomain Integers — DISCRETE (epsilon 1): [1,5] ∪ [6,10] = [1,10]. Non-integers fail isValid; length() counts elements. Bound to a class as IntegerInterval.
IntegerInterval Interval over integer numbers (integerDomain, discrete, ε 1).
numberDomain Real numbers on the EXTENDED line — CONTINUOUS (no next). ±Infinity are real values, so a closed bracket on infinity is meaningful ([-Infinity,5] includes -∞). Only NaN is rejected (not comparable). Bound to a class as NumberInterval; also a handy base to spread into a custom domain.
NumberInterval Interval over real numbers on the extended line (numberDomain, continuous).
stringDomain Strings in lexicographic (UTF-16 code-unit) order — CONTINUOUS. Useful for keyspace / shard ranges, prefix scans, alphabetical buckets, and geohash spatial ranges (geohashes sort lexicographically). Code-unit order, NOT locale-aware. No measure (no natural distance between strings).
StringInterval Interval over lexicographically-ordered strings (stringDomain, continuous).