range-ts
Version:
RangeMap implementation based on Guava
185 lines • 8.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberRange = void 0;
var bound_type_1 = require("../core/bound-type");
var NumberRange = (function () {
function NumberRange(lowerEndpoint, lowerBoundType, upperEndpoint, upperBoundType) {
this.lowerEndpoint = lowerEndpoint;
this.lowerBoundType = lowerBoundType;
this.upperEndpoint = upperEndpoint;
this.upperBoundType = upperBoundType;
}
Object.defineProperty(NumberRange.prototype, "lowerEndpointValue", {
get: function () {
var _a;
return (_a = this.lowerEndpoint) === null || _a === void 0 ? void 0 : _a.valueOf();
},
enumerable: false,
configurable: true
});
Object.defineProperty(NumberRange.prototype, "upperEndpointValue", {
get: function () {
var _a;
return (_a = this.upperEndpoint) === null || _a === void 0 ? void 0 : _a.valueOf();
},
enumerable: false,
configurable: true
});
NumberRange.closedOpen = function (lower, upper) {
return new NumberRange(lower, bound_type_1.BoundType.CLOSED, upper, bound_type_1.BoundType.OPEN);
};
NumberRange.closed = function (lower, upper) {
return new NumberRange(lower, bound_type_1.BoundType.CLOSED, upper, bound_type_1.BoundType.CLOSED);
};
NumberRange.open = function (lower, upper) {
return new NumberRange(lower, bound_type_1.BoundType.OPEN, upper, bound_type_1.BoundType.OPEN);
};
NumberRange.openClosed = function (lower, upper) {
return new NumberRange(lower, bound_type_1.BoundType.OPEN, upper, bound_type_1.BoundType.CLOSED);
};
NumberRange.all = function () {
return new NumberRange(Number.NEGATIVE_INFINITY, bound_type_1.BoundType.OPEN, Number.POSITIVE_INFINITY, bound_type_1.BoundType.OPEN);
};
NumberRange.atLeast = function (endpoint) {
return new NumberRange(endpoint, bound_type_1.BoundType.CLOSED, Number.POSITIVE_INFINITY, bound_type_1.BoundType.OPEN);
};
NumberRange.atMost = function (endpoint) {
return new NumberRange(Number.NEGATIVE_INFINITY, bound_type_1.BoundType.OPEN, endpoint, bound_type_1.BoundType.CLOSED);
};
NumberRange.downTo = function (endpoint, boundType) {
return new NumberRange(endpoint, boundType, Number.POSITIVE_INFINITY, bound_type_1.BoundType.OPEN);
};
NumberRange.upTo = function (endpoint, boundType) {
return new NumberRange(Number.NEGATIVE_INFINITY, bound_type_1.BoundType.OPEN, endpoint, boundType);
};
NumberRange.prototype.contains = function (comparable) {
var value = comparable === null || comparable === void 0 ? void 0 : comparable.valueOf();
var aboveLowerEndpoint = this.lowerBoundType === bound_type_1.BoundType.OPEN
? this.lowerEndpointValue < value
: this.lowerEndpointValue <= value;
var belowUpperEndpoint = this.upperBoundType === bound_type_1.BoundType.OPEN
? this.upperEndpointValue > value
: this.upperEndpointValue >= value;
return aboveLowerEndpoint && belowUpperEndpoint;
};
NumberRange.prototype.encloses = function (other) {
var lowerEndpointEnclosed = other.lowerBoundType === bound_type_1.BoundType.OPEN
? this.contains(other.lowerEndpoint) ||
this.lowerEndpointValue === other.lowerEndpointValue
: this.contains(other.lowerEndpoint);
var upperEndpointEnclosed = other.upperBoundType === bound_type_1.BoundType.OPEN
? this.contains(other.upperEndpoint) ||
this.upperEndpointValue === other.upperEndpointValue
: this.contains(other.upperEndpoint);
return lowerEndpointEnclosed && upperEndpointEnclosed;
};
NumberRange.prototype.overlaps = function (other) {
var _a;
var intersection = this.intersection(other);
return (_a = (intersection && !intersection.isEmpty())) !== null && _a !== void 0 ? _a : false;
};
NumberRange.prototype.intersection = function (other) {
if (!this.isConnected(other)) {
return null;
}
var lowerRange = this.lowerEndpointValue <= other.lowerEndpointValue ? other : this;
var upperRange = this.upperEndpointValue >= other.upperEndpointValue ? other : this;
var lowerBoundType;
var upperBoundType;
if (this.lowerEndpointValue === other.lowerEndpointValue) {
lowerBoundType =
this.lowerBoundType === bound_type_1.BoundType.OPEN ||
other.lowerBoundType === bound_type_1.BoundType.OPEN
? bound_type_1.BoundType.OPEN
: bound_type_1.BoundType.CLOSED;
}
else {
lowerBoundType = lowerRange.lowerBoundType;
}
if (this.upperEndpointValue === other.upperEndpointValue) {
upperBoundType =
this.upperBoundType === bound_type_1.BoundType.OPEN ||
other.upperBoundType === bound_type_1.BoundType.OPEN
? bound_type_1.BoundType.OPEN
: bound_type_1.BoundType.CLOSED;
}
else {
upperBoundType = upperRange.upperBoundType;
}
return new NumberRange(lowerRange.lowerEndpoint, lowerBoundType, upperRange.upperEndpoint, upperBoundType);
};
NumberRange.prototype.isConnected = function (other) {
return (this.contains(other.lowerEndpoint) ||
this.contains(other.upperEndpoint) ||
other.contains(this.lowerEndpoint) ||
other.contains(this.upperEndpoint));
};
NumberRange.prototype.isEmpty = function () {
return (this.lowerEndpointValue === this.upperEndpointValue &&
(this.lowerBoundType === bound_type_1.BoundType.OPEN ||
this.upperBoundType === bound_type_1.BoundType.OPEN));
};
NumberRange.prototype.span = function (other) {
var lowerRange = this.lowerEndpointValue <= other.lowerEndpointValue ? this : other;
var upperRange = this.upperEndpointValue >= other.upperEndpointValue ? this : other;
var lowerBoundType;
var upperBoundType;
if (this.lowerEndpointValue === other.lowerEndpointValue) {
lowerBoundType =
this.lowerBoundType === bound_type_1.BoundType.CLOSED ||
other.lowerBoundType === bound_type_1.BoundType.CLOSED
? bound_type_1.BoundType.CLOSED
: bound_type_1.BoundType.OPEN;
}
else {
lowerBoundType = lowerRange.lowerBoundType;
}
if (this.upperEndpointValue === other.upperEndpointValue) {
upperBoundType =
this.upperBoundType === bound_type_1.BoundType.CLOSED ||
other.upperBoundType === bound_type_1.BoundType.CLOSED
? bound_type_1.BoundType.CLOSED
: bound_type_1.BoundType.OPEN;
}
else {
upperBoundType = upperRange.upperBoundType;
}
return new NumberRange(lowerRange.lowerEndpoint, lowerBoundType, upperRange.upperEndpoint, upperBoundType);
};
NumberRange.prototype.toString = function () {
var _this = this;
var getLowerBoundCharacter = function () {
switch (_this.lowerBoundType) {
case bound_type_1.BoundType.OPEN:
return "(";
case bound_type_1.BoundType.CLOSED:
return "[";
}
};
var getUpperBoundCharacter = function () {
switch (_this.upperBoundType) {
case bound_type_1.BoundType.OPEN:
return ")";
case bound_type_1.BoundType.CLOSED:
return "]";
}
};
var valueToString = function (value) {
switch (value.valueOf()) {
case Number.POSITIVE_INFINITY:
return "+∞";
case Number.NEGATIVE_INFINITY:
return "-∞";
default:
if (value['toISOString']) {
return value.toISOString();
}
return value.valueOf();
}
};
return "".concat(getLowerBoundCharacter()).concat(valueToString(this.lowerEndpoint), "..").concat(valueToString(this.upperEndpoint)).concat(getUpperBoundCharacter());
};
return NumberRange;
}());
exports.NumberRange = NumberRange;
//# sourceMappingURL=number-range.js.map