@rickosborne/rebound
Version:
Rick Osborne's utilities for working with bounded numbers
128 lines (127 loc) • 4.9 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var range_base_exports = {};
__export(range_base_exports, {
RangeBase: () => RangeBase
});
module.exports = __toCommonJS(range_base_exports);
var import_bound = require("./bound.cjs");
var import_range_like = require("./range-like.cjs");
const _RangeBase = class _RangeBase {
comparator;
isBounded;
isBoundedAbove;
isBoundedBelow;
isEmpty;
isLowerInc;
isSingleton;
isUpperInc;
lowerBound;
lowerEndpoint;
upperBound;
upperEndpoint;
constructor(isLowerInc, lowerBound, upperBound, isUpperInc, comparator) {
if (lowerBound === import_range_like.unbounded && !isLowerInc) {
throw new Error("Lower should be inclusive when unbounded");
}
if (upperBound === import_range_like.unbounded && !isUpperInc) {
throw new Error("Upper should be inclusive when unbounded");
}
this.comparator = comparator;
this.lowerBound = lowerBound === import_range_like.unbounded ? import_range_like.unbounded : isLowerInc ? import_bound.Bound.gte(lowerBound, comparator) : import_bound.Bound.gt(lowerBound, comparator);
this.upperBound = upperBound === import_range_like.unbounded ? import_range_like.unbounded : isUpperInc ? import_bound.Bound.lte(upperBound, comparator) : import_bound.Bound.lt(upperBound, comparator);
this.isLowerInc = isLowerInc;
this.isUpperInc = isUpperInc;
this.isBoundedAbove = upperBound !== import_range_like.unbounded;
this.isBoundedBelow = lowerBound !== import_range_like.unbounded;
this.isBounded = this.isBoundedAbove && this.isBoundedBelow;
if (lowerBound !== import_range_like.unbounded && upperBound !== import_range_like.unbounded && comparator(lowerBound, upperBound) === 0) {
if (!isLowerInc && !isUpperInc) {
throw new Error("Expected at least one inclusive when lower === upper");
}
this.isSingleton = isLowerInc && isUpperInc;
this.isEmpty = !this.isSingleton;
} else {
this.isSingleton = false;
this.isEmpty = false;
}
this.lowerEndpoint = lowerBound === import_range_like.unbounded ? void 0 : lowerBound;
this.upperEndpoint = upperBound === import_range_like.unbounded ? void 0 : upperBound;
}
assertIsA(obj) {
if (!this.isType(obj)) {
throw new RangeError(`Incorrect type: ${typeof obj}`);
}
if (!this.isA(obj)) {
throw new RangeError(`Out of range: ${String(obj)}`);
}
}
castAs(obj) {
this.assertIsA(obj);
return obj;
}
compareTo(other) {
if (this.lowerBound === import_range_like.unbounded) {
if (other.lowerBound !== import_range_like.unbounded) return -1;
} else if (other.lowerBound === import_range_like.unbounded) {
return 1;
} else {
const lowerCompare = this.lowerBound.compareTo(other.lowerBound);
if (lowerCompare !== 0) {
return lowerCompare;
}
}
if (this.upperBound === import_range_like.unbounded) {
if (other.upperBound !== import_range_like.unbounded) return 1;
} else if (other.upperBound === import_range_like.unbounded) {
return -1;
} else {
const upperCompare = this.upperBound.compareTo(other.upperBound);
if (upperCompare !== 0) {
return upperCompare;
}
}
return 0;
}
contains(value) {
return (this.lowerBound === import_range_like.unbounded || this.lowerBound.isValid(value)) && (this.upperBound === import_range_like.unbounded || this.upperBound.isValid(value));
}
encloses(other) {
if (other === this) {
return true;
}
if (this.comparator !== other.comparator) {
return false;
}
return (0, import_bound.boundComparator)(this.lowerBound, other.lowerBound) <= 0 && (0, import_bound.boundComparator)(this.upperBound, other.upperBound) >= 0;
}
isA(obj) {
return this.isType(obj) && this.contains(obj);
}
toString() {
return this.label;
}
[Symbol.toStringTag]() {
return this.label;
}
};
__name(_RangeBase, "RangeBase");
let RangeBase = _RangeBase;
//# sourceMappingURL=range-base.cjs.map