1.0.3 • Published 7 years ago

multi-part-range-compare v1.0.3

Weekly downloads
2
License
WTFPL
Repository
github
Last release
7 years ago

Given a multi-part range (think day/hour/minute or chapter/verse), check to see if a value is within the range.

const rangeStart = [10, 21]
const rangeEnd = [12, 2]

withinRange(rangeStart, rangeEnd, [ 10, 7 ]) // => false
withinRange(rangeStart, rangeEnd, [ 10, 25 ]) // => true
withinRange(rangeStart, rangeEnd, [ 12, 2 ]) // => true
withinRange(rangeStart, rangeEnd, [ 12, 5 ]) // => false

Ranges can have as many parts as you need.

const day = 4
const hour = 12
const minute = 55

withinRange([ 4, 0, 0 ], [ 5, 0, 0 ], [ day, hour, minute ]) // => true

withinRange(rangeStart, rangeEnd, value)

All arguments must be arrays of integers, all with the same number of elements.

Returns true or false.

Comparisons are inclusive.

withinRange.relative(rangeStart, rangeEnd, value)

Reminiscent of the Array.prototype.sort parameter.

Returns -1 if value is less than rangeStart.

Returns 1 if value is greater than rangeEnd.

Returns 0 if value is within the range.

withinRange.relative([1, 0], [2, 0], [0, 5]) // => withinRange.LESS_THAN_START
withinRange.relative([1, 0], [2, 0], [1, 4]) // => withinRange.WITHIN
withinRange.relative([1, 0], [2, 0], [2, 1]) // => withinRange.GREATER_THAN_END

withinRange.LESS_THAN_START // => -1
withinRange.WITHIN // => 0
withinRange.GREATER_THAN_END // => 1

License

WTFPL