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

numeric-range

v1.0.4

Published

Fast, light-weight, dependency-free library supporting numeric ranges

Downloads

227

Readme

NumericRange

NumericRange is a library supporting numeric ranges between two real numbers called bounds. The upper bound is the maximal value of the range, the lower bound is the minimal value. The object itself is not an array of numbers contained in the range but instead is an abstract representation of the range by containing bounds which allows us to add several helper methods and to save memory in the case the range is too large. The object contains a method enumerate() which returns an array of numbers instead similar to range function in other languages.

Installation

npm i numeric-range

API

min: number

The lower bound of the range. The value itself is included in the range.

max: number

The upper bound of the range. The value itself is included in the range.

constructor(min: number, max: number)

The new keyword, lower and upper bound are required in the constructor, if one of the bounds is missing an error will be thrown. It is preferred to use the number type, the library uses the Number function to parse non-numerical values, so strings like '5.5' or bigints will work properly but using non-numerical types might lead to unexpected behavior and side-effect. Use them at your own risk.

new NumericRange(5, 10)

enumerate(step: number = 1): number[]

The method returns an array of contained numbers inside the range including the bounds, step indicates the difference of two adjacent numbers of the returned array, defaults to 1.

new NumericRange(5, 10).enumerate()
[5, 6, 7, 8, 9, 10]

The step might be any positive number, if the number is decimal, the numbers will be rounded using the toFixed(the number of digits of the step) method to avoid numbers like 0.30000000000000004.

new NumericRange(0, 1).enumerate(.1)
[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
new NumericRange(5, 10).enumerate(.5)
[5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10]

The step might be any positive number, if the current value + step is greater than the upper bound, the next value is omitted, regardless of whether the current value is equal or lower to the upper bound. The upper bound is not included either.

new NumericRange(5, 10).enumerate(.7)
[5, 5.7, 6.4, 7.1, 7.8, 8.5, 9.2, 9.9] //10 and 10.6 are not included

includes(searchNumber: number): boolean

This method determines whether the search number is contained in the range. If the search number is equal to either bound, the method returns true.

new NumericRange(5, 10).includes(7.5)
true
new NumericRange(5, 10).includes(5)
true
new NumericRange(5, 10).includes(3.5)
false

incorporate(numberToIncorporate: number): number

This method incorporates the number given in the argument to the range, if the number is greater than the upper bound the number is set to the upper bound in the case of the number being lower than the lower bound, the number is set to the value of the lower bound, in the case of the number being in the range, the number itself is returned.

new NumericRange(5, 10).incorporate(12)
10
new NumericRange(5, 10).incorporate(3.5)
5
new NumericRange(5, 10).incorporate(7)
7
new NumericRange(5, 10).incorporate(10)
10

License

NumericRange is licensed under an MIT License.