
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
int-interval-set
Advanced tools
A simple interval set for integers. This may be easier to work with than interval-tree-1d, for integer-only use cases like timestamps and sequential id numbers. All functions are synchronous. All boundaries are always closed (inclusive).
Note that this library is missing some important set operations, and I will add them as I come to need them for my own use. Please feel free to add more set operations and submit a pull request.
npm install --save int-interval-set
Add an interval to an empty set.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet();
console.log(set.isEmpty());
// true
set.union(2, 4);
console.log(set.intervals);
// [{lower: 2, upper: 4}]
Use fluent functions to add a bunch of intervals.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet().union(2, 4).union(5, 7).union(10, 20);
console.log(set.intervals);
// [{lower: 2, upper: 7}, {lower: 10, upper: 20}]
Add an array of intervals to an empty set. They can be out of order and overlapping.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet();
set.unionAll([
{lower: 2, upper: 4},
{lower: 10, upper: 20},
{lower: 5, upper: 7}
]);
console.log(set.intervals);
// [{lower: 2, upper: 7}, {lower: 10, upper: 20}]
Copy a set.
const IntIntervalSet = require('int-interval-set');
let set1 = new IntIntervalSet([{lower: 1, upper: 5}]);
let set2 = set1.clone().union(3, 20);
console.log(set2.intervals);
// [{lower: 1, upper: 20}]
console.log(set1.intervals);
// [{lower: 1, upper: 5}]
Get the smallest interval that contains the entire set.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet([{lower: 2, upper: 10}, {lower: 30, upper: 50}]);
console.log(set.span());
// {lower: 2, upper: 50}
Get the intersection of a set with a given interval.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet([{lower: 2, upper: 10}]);
let intersection = set.intersection(8, 20);
console.log(intersection.intervals);
// [{lower: 8, upper: 10}]
console.log(set.intervals);
// [{lower: 2, upper: 10}]
Get the complement of a set. This spans the whole number line, from the minimum to maximum safe integers.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet([{lower: 2, upper: 10}]);
let complement = set.complement();
console.log(complement.intervals);
// [{lower: -9007199254740991, upper: 1}, {lower: 11, upper: 9007199254740991}]
console.log(set.intervals);
// [{lower: 2, upper: 10}]
Iterate through all the integer values enclosed by this set.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet();
set.union(2, 4).union(9);
for (let x of set.values()) {
console.log(x);
}
// 2
// 3
// 4
// 9
Remove a point.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet([{lower: 4, upper: 10}]);
set.remove(7);
console.log(set.intervals);
// [{lower: 4, upper: 6}, {lower: 8, upper: 10}]
Remove an interval.
const IntIntervalSet = require('int-interval-set');
let set = new IntIntervalSet([{lower: 4, upper: 10}]);
set.remove(6, 8);
console.log(set.intervals);
// [{lower: 4, upper: 5}, {lower: 9, upper: 10}]
... and improve performance with a tree data structure, under the hood?
FAQs
A simple interval set (range set) for integers.
The npm package int-interval-set receives a total of 147 weekly downloads. As such, int-interval-set popularity was classified as not popular.
We found that int-interval-set demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.