Intervals are arrays whose first two elements are numbers and the first element should be less or equal to the second element, see interval.isInterval. This abstraction is useful when working with text ranges in rich text, for example.

Methods

(inner) coalesce(interval1, interval2, optMergeCallbackopt, The)

Turns two interval into one iff compare(interval1, interval2) ∈ [-2, -1,0,1, 2] (see inerval.compare). Otherwise returns null. Optionally uses merge function.

Parameters:
NameTypeAttributesDescription
interval1Interval
interval2Interval
optMergeCallbackfunction<optional>

Optional callback that is invoked upon coalescing.

TheInterval | null

coalesced interval or null if the intervals are disjunct.

Example
interval.coalesce([1,4], [5,7]) // => null
  interval.coalesce([1,2], [1,2]) // => [1,2]
  interval.coalesce([1,4], [3,6]) // => [1,6]
  interval.coalesce([3,6], [4,5]) // => [3,6]

(inner) coalesceOverlapping(intervals, optMergeCallbackopt, The)

This is essentially a lifted version of coalesce and accepts an array of intervals.

Parameters:
NameTypeAttributesDescription
intervalsArray.<Interval>

A list of intervals to coalesce.

optMergeCallbackfunction<optional>

Optional callback that is invoked upon coalescing.

TheArray.<Interval>

set of coalesced intervals.

Example
interval.coalesceOverlapping([[9,10], [1,8], [3, 7], [15, 20], [14, 21]])
  // => [[1,8],[9,10],[14,21]]

(inner) compare(a, b) → {number}

How interval.sort compares. We assume that a[0] <= a[1] and b[0] <= b[1] according to isInterval.

-3: a < b and non-overlapping, e.g [1,2] and [3,4]
-2: a < b and intervals border at each other, e.g [1,3] and [3,4]
-1: a < b and overlapping, e.g, [1,3] and [2,4] or [1,3] and [1,4]
 0: a = b, e.g. [1,2] and [1,2]
 1: a > b and overlapping, e.g. [2,4] and [1,3]
 2: a > b and share border, e.g [1,4] and [0,1]
 3: a > b and non-overlapping, e.g [2,4] and [0,1]
```
Parameters:
NameTypeDescription
aInterval
bInterval
Returns:
Type: 
number

(inner) intervalsInbetween(start, end, intervals) → {Array.<Interval>}

Computes "free" intervals between the intervals given in range start - end currently used for computing text chunks in lively.morphic/text

Parameters:
NameTypeDescription
startnumber

The lower bound for the intervals to be considered.

endnumber

The upper bound for the intervals to be considered.

intervalsArray.<Interval>

The collection of intervals to be filtered.

Returns:

The list of free intervals between the ranges and confined by the set of intervals.

Type: 
Array.<Interval>
Example
interval.intervalsInbetween(0, 10,[[1,4], [5,8]])
// => [[0,1],[4,5],[8,10]]

(inner) intervalsInRangeDo(start, end, intervals, iterator, mergeFunc, contextopt) → {Array.<Interval>}

Merges and iterates through sorted intervals. Will "fill up" intervals. This is currently used for computing text chunks in lively.morphic/text

Parameters:
NameTypeAttributesDescription
startnumber

The lower bound for the intervals to be considered.

endnumber

The upper bound for the intervals to be considered.

intervalsArray.<Interval>

The collection of intervals to iterate over and fill up as needed.

iteratorfunction

The iterator to call for each interval and also the inserted ones.

mergeFuncfunction

Callback to handle overlapping intervals.

contextobject<optional>

The context the callback functions iterator and mergeFunc are bound to.

Returns:

The collected intervals returned by iterator or mergeFunc.

Type: 
Array.<Interval>
Example
interval.intervalsInRangeDo(
  2, 10, [[0, 1], [5,8], [2,4]],
  function(i, isNew) { i.push(isNew); return i; })
// => [[2,4,false],[4,5,true],[5,8,false],[8,10,true]]

(inner) isInterval(object) → {boolean}

Returns wether the given object is an interval or not.

Parameters:
NameTypeDescription
objectobject

The object to check.

Returns:

Wether or not the given object is an interval.

Type: 
boolean
Example
interval.isInterval([1,12]) // => true
interval.isInterval([1,12, {property: 23}]) // => true
interval.isInterval([1]) // => false
interval.isInterval([12, 1]) // => false

(inner) mapToMatchingIndexes(intervals, intervalsToFind) → {Array.<number>}

Returns an array of indexes of the items in intervals that match items in intervalsToFind. We expect intervals and intervals to be sorted according to interval.compare! This is the optimized version of:

Parameters:
NameTypeDescription
intervalsArray.<Intervals>

The intervals considered the "candidates" for matches.

intervalsToFindArray.<Intervals>

The set of intervals to match up.

Returns:

The list of matching indices.

Type: 
Array.<number>
Example
return intervalsToFind.collect(function findOne(toFind) {
   var startIdx, endIdx;
   var start = intervals.detect(function(ea, i) {
      startIdx = i; return ea[0] === toFind[0]; });
   if (start === undefined) return [];
   var end = intervals.detect(function(ea, i) {
      endIdx = i; return ea[1] === toFind[1]; });
   if (end === undefined) return [];
   return Array.range(startIdx, endIdx);
});

(inner) mergeOverlapping(intervalsA, intervalsB, mergeFunc) → {Array.<Interval>}

Merges a set of potentially overlapping intervals.

Parameters:
NameTypeDescription
intervalsAArray.<Intervals>

The first set of intervals to merge.

intervalsBArray.<Intervals>

The second set of intervals to merge.

mergeFuncfunction

The function that given to overlapping intervals returns a single resolved interval.

Returns:

The set of merged intervals.

Type: 
Array.<Interval>

(inner) sort(intervals) → {Array.<Interval>}

Sorts intervals according to rules defined in interval.compare.

Parameters:
NameTypeDescription
intervalsArray.<Interval>

The collection of intervals to sort.

Returns:

The sorted list of intervals.

Type: 
Array.<Interval>

Type Definitions

Interval

An interval defining an upper and a lower bound.

Type:
  • Array.<number>
Properties
NameTypeDescription
0number

The lower bound of the interval.

1number

The upper bound of the interval.