API reference
Every public export of @dirtytalk/ranges with exact TypeScript signatures, parameters, return values, errors and examples.
All exports come from @dirtytalk/ranges. For guided explanations see Overview.
Types
Interval
interface Interval {
readonly start: number;
readonly end: number;
}
A half-open span [start, end). Coordinates are plain numbers and may be negative or fractional.
import type { Interval } from '@dirtytalk/ranges';
const span: Interval = { start: 100, end: 104 };
IntervalSet
class IntervalSet {
static readonly empty: IntervalSet;
private constructor();
static from(intervals: readonly Interval[]): IntervalSet;
isEmpty(): boolean;
union(other: IntervalSet): IntervalSet;
intersects(other: IntervalSet): boolean;
spans(): readonly Interval[];
}
An immutable set of canonical, disjoint, half-open intervals. union is exact: [0, 1) and [9, 10) stay two spans, with no bounding-range over-approximation. Sets are only created by from, union and empty.
IntervalSet.empty
static readonly empty: IntervalSet;
The shared empty set. isEmpty() is true; union(empty, r) returns r.
import { IntervalSet } from '@dirtytalk/ranges';
IntervalSet.empty.isEmpty(); // true
IntervalSet.from
static from(intervals: readonly Interval[]): IntervalSet;
Builds a canonical set from arbitrary intervals. The input is normalized:
- Each interval is validated: endpoints must be finite;
startmust not be greater thanend. - Empty spans (
start === end) are dropped. - Spans are sorted by
start, thenend. - Overlapping or touching spans are merged:
[1, 2)+[2, 3)becomes[1, 3).
Returns IntervalSet.empty when the input is empty or every span is empty.
Throws
Error('IntervalSet.from: interval endpoints must be finite numbers')Error('IntervalSet.from: interval start must not be greater than end')
import { IntervalSet } from '@dirtytalk/ranges';
const set = IntervalSet.from([
{ start: 90, end: 94 },
{ start: 0, end: 4 },
{ start: 3, end: 5 }, // overlaps [0, 4)
]);
set.spans(); // [{ start: 0, end: 5 }, { start: 90, end: 94 }]
isEmpty
isEmpty(): boolean;
Returns true when the set has no spans.
import { IntervalSet } from '@dirtytalk/ranges';
IntervalSet.from([{ start: 5, end: 5 }]).isEmpty(); // true (empty span dropped)
union
union(other: IntervalSet): IntervalSet;
Returns the exact union of two sets. Returns this or other by reference when one side is empty; otherwise returns a new set. Touching and overlapping spans merge.
import { IntervalSet } from '@dirtytalk/ranges';
const a = IntervalSet.from([{ start: 0, end: 2 }]);
const b = IntervalSet.from([{ start: 1, end: 4 }]);
a.union(b).spans(); // [{ start: 0, end: 4 }]
intersects
intersects(other: IntervalSet): boolean;
Returns true when any span of this set overlaps a span of other. Half-open overlap: [1, 2) and [2, 3) touch but do not intersect.
import { IntervalSet } from '@dirtytalk/ranges';
const a = IntervalSet.from([{ start: 0, end: 4 }]);
a.intersects(IntervalSet.from([{ start: 3, end: 9 }])); // true
a.intersects(IntervalSet.from([{ start: 4, end: 9 }])); // false (touching)
spans
spans(): readonly Interval[];
The canonical spans: sorted by start, non-overlapping, non-empty, and frozen. The array is the set’s internal representation and is returned without copying.
import { IntervalSet } from '@dirtytalk/ranges';
const set = IntervalSet.from([
{ start: 9, end: 10 },
{ start: 0, end: 1 },
]);
set.spans(); // [{ start: 0, end: 1 }, { start: 9, end: 10 }]
IntervalSpace
const IntervalSpace: Space<IntervalSet>;
The Space (from @dirtytalk/engine) over IntervalSet.
| Method | Implementation |
|---|---|
empty() | IntervalSet.empty |
isEmpty(r) | r.isEmpty() |
union(a, b) | a.union(b) |
intersects(interest, dirty) | interest.intersects(dirty) |
unionInto(acc, b) | Appends b’s spans to acc and merges them once, on the next read of acc |
unionInto makes a burst of N marks in one flush window cost O(N log N) instead of O(N²). The channel only calls it on an accumulator it created itself, so sets you pass to mark and sets subscribers receive are never mutated afterwards. unionInto is for DirtyChannel only. Calling it yourself mutates a set other code may share, including IntervalSet.empty; use union instead.
import { IntervalSet, IntervalSpace } from '@dirtytalk/ranges';
import { DirtyChannel, ManualScheduler } from '@dirtytalk/engine';
const channel = new DirtyChannel(IntervalSpace, new ManualScheduler());
channel.subscribe(
() => IntervalSet.from([{ start: 0, end: 1024 }]),
(dirty) => console.log('upload', dirty.spans()),
);
channel.mark(IntervalSet.from([{ start: 100, end: 104 }]));
See Custom spaces for the full Space contract and a worked channel example.