Fast, light-weight, dependency-free library supporting numeric ranges
npm install numeric-rangenpm i numeric-range
'5.5' or bigints will work properly but using non-numerical types might lead to unexpected
js
new NumericRange(5, 10)
`
$3
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.
`js
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.
`js
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.
`js
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
`
$3
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.
`js
new NumericRange(5, 10).includes(7.5)
true
new NumericRange(5, 10).includes(5)
true
new NumericRange(5, 10).includes(3.5)
false
`
$3
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.
`js
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
``