45 lines
1.6 KiB
JavaScript
45 lines
1.6 KiB
JavaScript
|
/*---------------------------------------------------------------------------------------------
|
||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||
|
*--------------------------------------------------------------------------------------------*/
|
||
|
export var Range;
|
||
|
(function (Range) {
|
||
|
/**
|
||
|
* Returns the intersection between two ranges as a range itself.
|
||
|
* Returns `{ start: 0, end: 0 }` if the intersection is empty.
|
||
|
*/
|
||
|
function intersect(one, other) {
|
||
|
if (one.start >= other.end || other.start >= one.end) {
|
||
|
return { start: 0, end: 0 };
|
||
|
}
|
||
|
var start = Math.max(one.start, other.start);
|
||
|
var end = Math.min(one.end, other.end);
|
||
|
if (end - start <= 0) {
|
||
|
return { start: 0, end: 0 };
|
||
|
}
|
||
|
return { start: start, end: end };
|
||
|
}
|
||
|
Range.intersect = intersect;
|
||
|
function isEmpty(range) {
|
||
|
return range.end - range.start <= 0;
|
||
|
}
|
||
|
Range.isEmpty = isEmpty;
|
||
|
function intersects(one, other) {
|
||
|
return !isEmpty(intersect(one, other));
|
||
|
}
|
||
|
Range.intersects = intersects;
|
||
|
function relativeComplement(one, other) {
|
||
|
var result = [];
|
||
|
var first = { start: one.start, end: Math.min(other.start, one.end) };
|
||
|
var second = { start: Math.max(other.end, one.start), end: one.end };
|
||
|
if (!isEmpty(first)) {
|
||
|
result.push(first);
|
||
|
}
|
||
|
if (!isEmpty(second)) {
|
||
|
result.push(second);
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
Range.relativeComplement = relativeComplement;
|
||
|
})(Range || (Range = {}));
|