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

numberset

v1.0.0

Published

Small library to perform set operations on number sets

Downloads

7

Readme

NumberSet

GitHub package.json version npm Test Coverage License

A small library to handle intervals and sets that can be represented by a finite amount of intervals.

NumberSet provides two immutable classes - NumberSet and Interval - capable of common arithmetic and set operations. The library supports closed, open and half-open intervals (bounded and unbounded) and sets build out of them. Both intervals and sets can be constructed from string representations. See the documentation for more details.

If you need to query large sets that are changing all the time and you find this library lacking, consider using interval trees instead.

Installation

npm install numberset

Usage

Interval

const unitInterval = Interval.Closed(0, 1);
const unitIntervalExplicit = new Interval({
  lowerBound: 0,
  upperBound: 1,
  lowerBoundIncluded: true,
  upperBoundIncluded: true,
});
const unitIntervalFromString = Interval.fromString('[0,1]');
unitInterval.equals(unitIntervalExplicit); // true
unitInterval.equals(unitIntervalFromString); // true

unitIntervalExplicit.intersects(Interval.BottomClosed(1, 2)); // true
unitIntervalExplicit.touches(Interval.Open(1, 2)); // true
unitIntervalExplicit.contains(0.5); // true
unitInterval.isEmpty(); // false
unitInterval.center(); // 0.5
unitInterval.diameter(); // 1
unitInterval.radius(); // 0.5

unitInterval.translatedBy(1); // [1,2]
unitInterval.scaledBy(2); // [0,2]
unitInterval.intersection(unitInterval.scaledBy(-1)); // [0,0]

// operations that might result in two intervals return NumberSets
unitInterval.without(unitInterval.interior()); // {[0,0], [1,1]}
unitInterval.union(unitInterval.scaledBy(-1)); // {[-1,1]}
unitInterval.symDiff(unitInterval.scaledBy(-1)); // {[-1,0), (0,1]}

NumberSet

const indices = [0, 1, 2, 3];
const fullSet = NumberSet.from(indices.map((p) => Interval.Closed(p, p + 1))); // {[0,4]}
const fullSetFromString = NumberSet.fromString('{[0,2], [2,4]}'); // {[0,4]}
fullSet.equals(fullSetFromString); // true, NumberSet "normalizes" the provided Intervals

fullSet.intersects(Interval.Closed(1, 2).toSet()); // true
fullSet.contains(1); // true
fullSet.isEmpty(); // false
fullSet.center(); // 2
fullSet.diameter(); // 4
fullSet.radius(); // 2
fullSet.volume(); // 4

const points = NumberSet.from(indices.map((p) => Interval.Point(p))); // {[0,0], [1,1], [2,2], [3,3]}
points.interior(); // {}
const withoutPoints = fullSet.without(points); // {(0,1), (1,2), (2,3), (3,4]}
withoutPoints.union(points); // {[0,4]}
withoutPoints.closure(); // {[0,4]}
fullSet.symDiff(points.scaledBy(-1)); // {[-3,-3], [-2,-2], [-1,-1], (0,4]}
points.translatedBy(-2); // {[-2,-2], [-1,-1], [0,0], [1,1]}

See the documentation for further information.