Skip to content

Thomasan1999/numeric-range

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Note: @types/numeric-range doesn't exist, types are included in the package itself.

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 either 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 effects. 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(numberToCheck: number): boolean

This method determines whether the range contains the number to check. If the number to check 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 method returns the upper bound in the case of the number to incorporate being lower than the lower bound, the method returns the lower bound, in the case of the number being in the range, the method returns the number itself.

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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published