@zfunction/genetics-js
Version:
Genetic and evolutionary algorithms framework for the web
72 lines • 2.58 kB
JavaScript
;
/*
* @license
* Copyright (c) 2019 Cristian Abrante. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* ## Numeric Range
* Represents a numeric range.
*/
var NumericRange = /** @class */ (function () {
function NumericRange(lowest, highest) {
if (lowest === void 0) { lowest = Number.NEGATIVE_INFINITY; }
if (highest === void 0) { highest = Number.POSITIVE_INFINITY; }
this._lowest = Number.NEGATIVE_INFINITY;
this._highest = Number.POSITIVE_INFINITY;
this.set(lowest, highest);
}
NumericRange.isArrayInRange = function (array, range) {
var _this = this;
return array.every(function (value) { return _this.isValueInRange(value, range); });
};
NumericRange.isValueInRange = function (value, range) {
return value >= range.lowest && value <= range.highest;
};
NumericRange.normalizeValueToRange = function (value, range) {
if (!this.isValueInRange(value, range)) {
return value < range.lowest ? range.lowest : range.highest;
}
else {
return value;
}
};
NumericRange.checkRangeValues = function (lowest, highest) {
if (!this.rangeValuesAreValid(lowest, highest)) {
throw new Error("Error: range [" + lowest + ", " + highest + "] is not a valid range.");
}
};
NumericRange.rangeValuesAreValid = function (lowest, highest) {
return lowest < highest;
};
Object.defineProperty(NumericRange.prototype, "lowest", {
get: function () {
return this._lowest;
},
set: function (value) {
this.set(value, this.highest);
},
enumerable: true,
configurable: true
});
Object.defineProperty(NumericRange.prototype, "highest", {
get: function () {
return this._highest;
},
set: function (value) {
this.set(this.lowest, value);
},
enumerable: true,
configurable: true
});
NumericRange.prototype.set = function (lowest, highest) {
NumericRange.checkRangeValues(lowest, highest);
this._lowest = lowest;
this._highest = highest;
};
NumericRange.DEFAULT = new NumericRange();
return NumericRange;
}());
exports.NumericRange = NumericRange;
//# sourceMappingURL=NumericRange.js.map