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

valiant

v2.0.0

Published

JavaScript interval arithmetic library

Downloads

41

Readme

valiant

semantic-release Build Status

JavaScript interval arithmetic library.

An interval represents a range of data with an associated sort function that defines how values relate to each other. For example, a range of numbers just uses subtraction.

Intervals can have inclusive or exclusive bounds, and you can define a custom sort function.

For more, see Wikipedia. This library is largely inspired by the Haskell's Data.Interval and was built originally to manage lists of Tweets within TweetDeck.

Install

npm install --save valiant

Use

To make an Interval, you need to create a constructor:

import { createInterval } from 'valiant';

const Interval = createInterval();

By default it will manage integers:

// (0,100] — the numbers 0 to 100, excluding 0
new Interval(
    Interval.exclusiveEndpoint(0),
    Interval.inclusiveEndpoint(100)
)

You can provide a custom sort function to support a different data type:

const DateInterval = createInterval(function sortDates(a, b) {
    return a.getTime() - b.getTime();
});

// [12 hours ago,now] — 12 hours ago until now
new DateInterval(
    DateInterval.incEnd(
        new Date(Date.now() - (1000 * 60 * 60 * 12))
    ),
    DateInterval.incEnd(
        new Date(Date.now())
    )
)

You can do calculations with two intervals:

i = [1,3]
j = [2,4]
k = [5,6]

i.intersection(j)         // [2,3]
i.hull(j)                 // [1,4]
i.contiguousWith(j)       // true
i.unify(j)                // [1,4]

i.intersection(k)         // Interval.empty
i.hull(k)                 // [1,6]
i.contiguousWith(k)       // false
i.unify(k)                // Interval.empty

i.equalTo(j)              // false
i.contains(2)             // true
i.isSubsetOf(j)           // false

If there is no possible unification, the empty set (Interval.empty) results.

There are two special intervals:

Interval.empty // {}
Interval.whole // (-Infinity, Infinity)

You can also create an interval of value:

Interval.singleton(5) // [5,5]

To get Haskelly for a bit, because of the closed-over sort function, Interval is a bit like a typeclass with an Ordinal type constraint:

data Ord a => Interval a = Interval (EndPoint a, Bool) (Endpoint a, Bool)

Why?

TweetDeck uses this library to manage lists of many kinds of data (we call them chirps). With it we detect overlapping blocks of Tweets, gaps and do neat memory management optimisations.

Development

$ npm install
$ npm test

Commit messages should follow the Angular commit message guidelines.

Release

This repository uses semantic-release. Changes will automatically be released to npm.