Interval.js is a lightweight JavaScript library for working with mathematical intervals, providing a wide range of operations useful in computational geometry, scheduling, numerical analysis and when using Unix timestamps also date ranges.
- Basic interval operations: intersection, union, subtraction, merging
- Comparison and equality checks
- Query functions: overlap, containment, adjacency, distance (gap)
- Utility functions: size, midpoint, translation, scaling, extension
- Static helpers for merging and normalizing interval sets
- Designed for Closure Compiler advanced optimizations
You can install Interval.js
via npm:
npm install @rawify/interval
Or with yarn:
yarn add @rawify/interval
Alternatively, download or clone the repository:
git clone https://github.com/rawify/Interval.js
Include the interval.min.js
file in your project:
<script src="path/to/interval.min.js"></script>
Or in a Node.js project:
const Interval = require('@rawify/interval');
or
import Interval from '@rawify/interval';
Intervals can be created using the Interval
constructor:
let i1 = new Interval(1, 5);
let i2 = new Interval(3, 7);
Empty intervals are represented by [0, 0]
.
Finds the intersection of two intervals. Returns null
if they don’t intersect.
let i1 = new Interval(1, 5);
let i2 = new Interval(3, 7);
let inter = i1.intersection(i2); // [3, 5]
Returns the convex hull (minimal covering interval) of two intervals.
let uni = i1.union(i2); // [1, 7]
Checks if two intervals are equal.
i1.equals(new Interval(1, 5)); // true
Checks if two intervals overlap.
i1.overlaps(i2); // true
Checks if two intervals are adjacent (share a boundary point).
Checks if the current interval fully contains another interval.
new Interval(1, 10).containsInterval(new Interval(3, 5)); // true
Checks if a point x
lies inside the interval.
Returns true
if the interval is empty (i.e. a >= b
).
Returns the length of the interval.
Returns the midpoint of the interval.
Computes the distance between two intervals (0 if overlapping or touching).
Shifts the interval by dx
.
Extends the start and end of the interval.
Scales the interval around its midpoint by factor f
.
Subtracts interval i
from the current interval. May return up to two disjoint intervals.
Subtracts multiple intervals, returning a normalized list of disjoint intervals.
Returns a string representation of the interval.
Creates an empty interval [0,0]
.
Sorts an array of intervals by start, then end.
Merges overlapping or adjacent intervals in a list, returning disjoint intervals.
Returns the minimal covering interval of a list of intervals.
Computes the intersection of all intervals in a list.
Like all my libraries, Interval.js is written to minimize size after compression with Google Closure Compiler in advanced mode. The code style is optimized to maximize compressibility. If you extend the library, please preserve this style.
After cloning the Git repository run:
npm install
npm run build
Copyright (c) 2025, Robert Eisele Licensed under the MIT license.