math-interval-2
Version:
Create math intervals like '(-∞, 100]' and test values
178 lines • 6.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var bound_1 = require("./bound");
var MathInterval = /** @class */ (function () {
function MathInterval(lowerBound, upperBound) {
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.validate();
this.intervalString = this.getIntervalString();
}
/**
* Returns an interval equivalent to:
* (a, b) = {x | a < x < b}
*/
MathInterval.open = function (lower, upper) {
return MathInterval.interval(lower, false, upper, false);
};
/**
* Returns an interval equivalent to:
* [a, b] = {x | a <= x <= b}
*/
MathInterval.closed = function (lower, upper) {
return MathInterval.interval(lower, true, upper, true);
};
/**
* Returns an interval equivalent to:
* [a, b) = {x | a <= x < b}
*/
MathInterval.closedOpen = function (lower, upper) {
return MathInterval.interval(lower, true, upper, false);
};
/**
* Returns an interval equivalent to:
* (a, b] = {x | a < x <= b}
*/
MathInterval.openClosed = function (lower, upper) {
return MathInterval.interval(lower, false, upper, true);
};
/**
* Returns an interval equivalent to:
* (a, ∞) = {x | a < x < ∞}
*/
MathInterval.greaterThan = function (lower) {
return MathInterval.interval(lower, false, Infinity, false);
};
/**
* Returns an interval equivalent to:
* [a, ∞) = {x | a < x < ∞}
*/
MathInterval.atLeast = function (lower) {
return MathInterval.interval(lower, true, Infinity, false);
};
/**
* Returns an interval equivalent to:
* (-∞, a) = {x | -∞ < x < a}
*/
MathInterval.lessThan = function (upper) {
return MathInterval.interval(-Infinity, false, upper, false);
};
/**
* Returns an interval equivalent to:
* (-∞, a] = {x | -∞ < x <= a}
*/
MathInterval.atMost = function (upper) {
return MathInterval.interval(-Infinity, false, upper, true);
};
/**
* Returns an interval equivalent to:
* (-∞, ∞) = {x | -∞ < x < ∞}
*/
MathInterval.all = function () {
return MathInterval.interval(-Infinity, false, Infinity, false);
};
/**
* Returns an interval equivalent to:
* for interval(a, true, b, false) -> [a, b) = {x | a <= x < b}
*/
MathInterval.interval = function (lower, lowerClosed, upper, upperClosed) {
var lowerBound = lowerClosed ? bound_1.Bound.lowerClosedBound(lower) : bound_1.Bound.lowerOpenBound(lower);
var upperBound = upperClosed ? bound_1.Bound.upperClosedBound(upper) : bound_1.Bound.upperOpenBound(upper);
return new MathInterval(lowerBound, upperBound);
};
MathInterval.prototype.validate = function () {
if (this.lowerEndpoint() > this.upperEndpoint()) {
throw new Error('lower endpoint can not be greater than upper endpoint');
}
if (this.lowerEndpoint() === this.upperEndpoint() && !(this.isLowerBoundClosed() && this.isUpperBoundClosed())) {
throw new Error('upper endpoint can be equal to lower endpoint only if both bounds are closed');
}
};
MathInterval.prototype.getIntervalString = function () {
// TODO: move logic to Bound.toString
var lowerSymbol = this.isLowerBoundClosed() ? '[' : '(';
var upperSymbol = this.isUpperBoundClosed() ? ']' : ')';
var lowerEndpoint = this.lowerEndpoint() === -Infinity ? '-∞' : this.lowerEndpoint();
var upperEndPoint = this.upperEndpoint() === Infinity ? '+∞' : this.upperEndpoint();
return "" + lowerSymbol + lowerEndpoint + ", " + upperEndPoint + upperSymbol;
};
MathInterval.prototype.isLowerBoundClosed = function () {
return this.lowerBound.closed;
};
MathInterval.prototype.isUpperBoundClosed = function () {
return this.upperBound.closed;
};
MathInterval.prototype.lowerEndpoint = function () {
return this.lowerBound.endpoint;
};
MathInterval.prototype.upperEndpoint = function () {
return this.upperBound.endpoint;
};
MathInterval.prototype.containsAll = function (numbers) {
var _this = this;
return !numbers.some(function (n) { return !_this.contains(n); });
};
/**
* Behaves exactly as you might expect
*/
MathInterval.prototype.contains = function (n) {
return this.lowerBound.test(n) && this.upperBound.test(n);
};
/**
* Returns true,
* if there is some interval enclosed by both of these intervals
*/
MathInterval.prototype.isConnected = function (other) {
return this.lowerBound.test(other.upperEndpoint())
&& this.upperBound.test(other.lowerEndpoint());
};
/**
* Returns true,
* if the bounds of the inner interval do not extend outside the bounds of the outer interval
*/
MathInterval.prototype.encloses = function (other) {
if (this.equals(other)) {
return true;
}
return this.lowerBound.test(other.lowerEndpoint())
&& this.upperBound.test(other.upperEndpoint());
};
/**
* Returns the minimal interval that encloses both this interval and other.
* If the intervals are both connected, this is their union.
*/
MathInterval.prototype.span = function (other) {
var lowerBound = this.lowerBound.compareTo(other.lowerBound) > 0 ?
this.lowerBound : other.lowerBound;
var upperBound = this.upperBound.compareTo(other.upperBound) > 0 ?
this.upperBound : other.upperBound;
return new MathInterval(lowerBound, upperBound);
};
/**
* Returns the maximal interval enclosed by both this interval and other
* if they are connect, otherwise throws error
*/
MathInterval.prototype.intersection = function (connected) {
// TODO: throw categorized error if intervals are not connected
var lowerBound = this.lowerBound.compareTo(connected.lowerBound) < 0 ?
this.lowerBound : connected.lowerBound;
var upperBound = this.upperBound.compareTo(connected.upperBound) < 0 ?
this.upperBound : connected.upperBound;
return new MathInterval(lowerBound, upperBound);
};
MathInterval.prototype.equals = function (other) {
// TODO: try to move some logic to Bound.equals
return this.lowerBound.equals(other.lowerBound)
&& this.upperBound.equals(other.upperBound);
};
/**
* Returns string representation of the interval
* in form of `[a, b)`
*/
MathInterval.prototype.toString = function () {
return this.intervalString;
};
return MathInterval;
}());
exports.MathInterval = MathInterval;
//# sourceMappingURL=/home/travis/build/harunurhan/math-interval/src/index.js.map