Interval
Class to work with intervals of real numbers.
Version
0.1.1
Install
npm install math.interval
Usage
var Interval =var interval = '(1, 5)'interval // falseinterval // trueinterval // trueinterval // [new Interval('(1, 5)'), new Interval('[8, 9)')]interval // [new Interval('(1, 6]')]var isolatedInterval = '{3}' // equivalent to [3, 3]isolatedInterval // falseinterval // true
API
Interval
constructor(interval)
Constructor creates an instance of Interval class. it throws an exception if interval
is not IntervalCastable
Interval#isEmpty()
It returns true or false if interval is empty or not.
Example:
var Interval =var '[2, 4)'interval // returns false
Interval#contains(interval)
It returns true
or false
if instance contains interval
passed by parameter. contains
throws an exception if interval
is not IntervalCastable
Example:
var Interval =var interval = '[1, 3)'interval // returns trueinterval // returns false
Interval#union(...intervals)
It returns an array of minimum disjoint intervals that represents the union of instance of interval with intervals
passed by parameter.
var Interval =var interval = '[1, 3)'interval// returns [new Interval('[1, 4)'), new Interval('[5, 6)')]
Interval.union(...intervals)
Interval also has static method that calculates the union of intervals in the same way as union
method.
var Interval =Interval// returns [new Interval('[1, 4)'), new Interval('[5, 6)')]
Interval#toString()
It returns an string with a expression representation of interval
var Interval =var a = '(2, 5]'var b = '(5, 2]' // emptyvar c = '[2, 2]' // singleton intervala // '(2, 5]'b // '{}'c // '{2}'
IntervalCastable
A value is IntervalCastable if it is one of this list of types:
- instance of
Interval
. - string that parses with interval (
'[2, 5]'
,'[0, 5)'
,'{3}'
,'(-2, 1)'
, etc). - data structure defined in
math.interval-utils
package.
Exported functions
rawInterval(interval)
It converts Interval instance to interval data structure defined in math.interval-utils
package. It is posible to import this function thus:
var rawInterval =
cast(interval)
It converts IntervalCastable value to interval data structure defined in math.interval-utils
package. It is posible to import this function thus:
var rawInterval =
LICENSE
MIT