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.
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.
5 105 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.
0 10 01 02 03 04 05 06 07 08 09 15 105 55 6 65 7 75 8 85 9 95 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.
5 105 57 64 71 78 85 92 99 //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.
5 10true5 10true5 10false
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.
5 10105 1055 1075 1010
License
NumericRange is licensed under an MIT License.