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.
- Source
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.
| Name | Type | Attributes | Description |
|---|---|---|---|
interval1 | Interval | ||
interval2 | Interval | ||
optMergeCallback | function | <optional> | Optional callback that is invoked upon coalescing. |
The | Interval | | coalesced interval or |
- Source
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.
| Name | Type | Attributes | Description |
|---|---|---|---|
intervals | Array.<Interval> | A list of intervals to coalesce. | |
optMergeCallback | function | <optional> | Optional callback that is invoked upon coalescing. |
The | Array.<Interval> | set of coalesced intervals. |
- Source
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]
```| Name | Type | Description |
|---|---|---|
a | Interval | |
b | Interval |
- Source
- 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
| Name | Type | Description |
|---|---|---|
start | number | The lower bound for the intervals to be considered. |
end | number | The upper bound for the intervals to be considered. |
intervals | Array.<Interval> | The collection of intervals to be filtered. |
- Source
The list of free intervals between the ranges and confined by the set of intervals.
- Type:
- Array.<Interval>
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
| Name | Type | Attributes | Description |
|---|---|---|---|
start | number | The lower bound for the intervals to be considered. | |
end | number | The upper bound for the intervals to be considered. | |
intervals | Array.<Interval> | The collection of intervals to iterate over and fill up as needed. | |
iterator | function | The iterator to call for each interval and also the inserted ones. | |
mergeFunc | function | Callback to handle overlapping intervals. | |
context | object | <optional> | The context the callback functions |
- Source
The collected intervals returned by iterator or mergeFunc.
- Type:
- Array.<Interval>
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.
| Name | Type | Description |
|---|---|---|
object | object | The object to check. |
- Source
Wether or not the given object is an interval.
- Type:
- boolean
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:
| Name | Type | Description |
|---|---|---|
intervals | Array.<Intervals> | The intervals considered the "candidates" for matches. |
intervalsToFind | Array.<Intervals> | The set of intervals to match up. |
- Source
The list of matching indices.
- Type:
- Array.<number>
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.
| Name | Type | Description |
|---|---|---|
intervalsA | Array.<Intervals> | The first set of intervals to merge. |
intervalsB | Array.<Intervals> | The second set of intervals to merge. |
mergeFunc | function | The function that given to overlapping intervals returns a single resolved interval. |
- Source
The set of merged intervals.
- Type:
- Array.<Interval>
(inner) sort(intervals) → {Array.<Interval>}
Sorts intervals according to rules defined in interval.compare.
| Name | Type | Description |
|---|---|---|
intervals | Array.<Interval> | The collection of intervals to sort. |
- Source
The sorted list of intervals.
- Type:
- Array.<Interval>
Type Definitions
Interval
An interval defining an upper and a lower bound.
- Array.<number>
| Name | Type | Description |
|---|---|---|
0 | number | The lower bound of the interval. |
1 | number | The upper bound of the interval. |
- Source