npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

int-interval-set

v0.2.3

Published

A simple interval set (range set) for integers.

Downloads

388

Readme

Integer Interval Set

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.

Installation

npm install --save int-interval-set

API

Union

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}]

Union All

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}]

Clone

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}]

Span

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}

Intersection

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}]

Complement

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}]

Values

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

TODO

todo: Remove

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}]

todo: performance

... and improve performance with a tree data structure, under the hood?