Check if a value lies in a range with inclusive/exclusive ends
A charting library highlights the points that fall inside a selected range. Users can drag the selection from left to right or from right to left, and each chart decides whether the ends of the range count as inside. Implement the check it uses.
Implement isBetween(value, a, b, bounds). It returns true when value lies in the interval between a and b, and false otherwise.
isBetween(value, a, b, bounds = '[)') // => boolean
a and b can come in either order. The interval runs from low = min(a, b) to high = max(a, b).bounds is one of four strings. The first character describes low, the second describes high. [ or ] means the bound is included, ( or ) means it is excluded:
'[)' (the default): low <= value < high'[]': low <= value <= high'(]': low < value <= high'()': low < value < highbounds value throws a TypeError.value, a or b is NaN, return false.Infinity and -Infinity are valid bounds and values, compared like any other number.a === b, only '[]' contains anything (that one number). The other modes describe an empty interval.true or false.isBetween(5, 1, 10); // => true
isBetween(1, 1, 10); // => true (low is included by default)
isBetween(10, 1, 10); // => false (high is excluded by default)
isBetween(10, 1, 10, '[]'); // => true
isBetween(1, 10, 1); // => true (bounds are reordered: low 1, high 10)
isBetween(1, 1, 10, '(]'); // => false
isBetween(3, 3, 3, '[]'); // => true
isBetween(3, 3, 3); // => false
isBetween(NaN, 0, 10, '[]'); // => false
isBetween(1e300, 0, Infinity); // => true
isBetween(4, 0, 10, '[ ]'); // throws TypeError