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

number-ranger

v2.0.0

Published

Interpret ranges from strings and use them easily

Downloads

4

Readme

number-ranger

NPM version Build Status Dependencies Status devDependencies Status

Interpret ranges from strings and use them easily.

Install

Install via npm:

npm install number-ranger

API

ranger.parse(s)

Parses and transforms a string into a easily usable range.

var ranger = require('number-ranger');

ranger.parse('2');
// --> [{ start: 2 }]

// Add ':' to make a range between two numbers
ranger.parse('2:3');
// --> [{ start: 2, end: 3 }]

// Create multiple ranges by separating them by ','
ranger.parse('2:3,4');
// --> [{ start: 2, end: 3 }, { start: 4 }]

// Floating numbers are fine
ranger.parse('.002:.4');
// --> [{ start: .002, end: .4 }]

// -/+ infinity is represented by $
ranger.parse('2:$');
// --> [{ start: 2, end: +Infinity }]
ranger.parse('$:2');
// --> [{ start: -Infinity, end: 2 }]

// Exclude bounds by prepending by x
ranger.parse('x2:4');
// --> [{ start: 2, end: 4, startIncluded: false }]
ranger.parse('2:x4');
// --> [{ start: 2, end: 4, endIncluded: false }]

You can specify your own symbols if you want. If you use forbidden symbols or have multiple keys have the same value, any calls will return false. If you wish to use odd and/or unforeseen symbols, test carefully beforehand to make sure it works. If you use symbols like '.', '-', you may lose the ability to parse floating or negative numbers for instance.

ranger.parse('2p}4?-47p%', {
    // All of these are optional. Only specify those you wish to override
    range: 'p',
    separator: '?',
    infinity: '%',
    exclude: '}'
});
// --> [
//     { start: 2, end: 4, endIncluded: false },
//     { start: -47, end: +Infinity }
// ]

ranger.parse('3', {
    range: '', // no empty strings
    separator: '   p', // no spaces,
    infinity: 'x' // not a conflicting symbol (= exclude's default symbol)
});
// --> false

ranger.isInRange(item, ranges[, key])

Checks if item is in the given ranges. Ranges can be a string as given for #ranger.parse(s), or the result of it. A key can also be provided when item is not a number but an object, and the value of item[key] will then be compared.

var ranger = require('number-ranger');

// Pass the result of ranger.range
ranger.isInRange(325, ranger.parse('300:350'));
// --> true

// or ranger.ranger will be used implicitly
ranger.isInRange(450, '400:450');
// --> true

ranger.isInRange(0, '400:450');
// --> false

ranger.isInRange(450, [{
    start: 400,
    end: 500
}]);
// --> true

// By specifying a field, it will consider item to be an object and use item[key] as the value
ranger.isInRange({ value: 450 }, '400:450', 'value');
// --> true

// The order of start and end are not important
ranger.isInRange(450, [{
    start: 500,
    end: 400
}]);
// --> true

// You can exclude bounds
ranger.isInRange(2, [{
    start: 2,
    end: 5,
    startIncluded: false
}]);
// --> false

ranger.isInRangeFilter(ranges[, key])

Returns an easy filter function that removes items that wouldn't match ranger.isInRange(item, ranges). Ranges can be a string as given for #ranger.parse(s), or the result of it. A key can also be provided when item is not a number but an object, and the value of item[key] will then be compared.

var ranger = require('number-ranger');

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].filter(ranger.isInRangeFilter([{
    start: 0,
    end: 5
}]));
// --> [0, 1, 2, 3, 4, 5]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].filter(ranger.isInRangeFilter('0:4'));
// --> [0, 1, 2, 3, 4]

[{ value: 0 }, { value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }].filter(ranger.isInRangeFilter('1:3', 'value'));
// --> [{ value: 1 }, { value: 2 }, { value: 3 }]