UNPKG

@monocircuit/utils

Version:

A repository that contains data structures, algorithms and more.

96 lines 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * An abstraction of the common compare operations. It enables more complex * comparisons via a custom compare function. */ var Comparator = /** @class */ (function () { /** * @param compareFunction An optional custom compare function */ function Comparator(compareFunction) { this.__compare = compareFunction !== null && compareFunction !== void 0 ? compareFunction : Comparator.defaultCompare; } /** * The default compare function treats the passed arguments as strings or as * numbers. For more complex values a custom comparison function needs to be * passed. * * @param x The second comparison subject * @param y The second comparison subject * @returns A numeric value indicating the comparison result */ Comparator.defaultCompare = function (x, y) { if (x === y) return 0; return x < y ? -1 : 1; }; /** * Checks if the two arguments passed into the method are equal to each * other. This is completely dependend of the specified comparison function. * * @param x The second comparison subject * @param y The second comparison subject * @returns A value indicating the result of the operation */ Comparator.prototype.equalTo = function (x, y) { return this.__compare(x, y) === 0; }; /** * Checks if the two arguments passed into the method are less than each * other. This is completely dependend of the specified comparison function. * * @param x The second comparison subject * @param y The second comparison subject * @returns A value indicating the result of the operation */ Comparator.prototype.lessThan = function (x, y) { return this.__compare(x, y) < 0; }; /** * Checks if the two arguments passed into the method are greater than each * other. This is completely dependend of the specified comparison function. * * @param x The second comparison subject * @param y The second comparison subject * @returns A value indicating the result of the operation */ Comparator.prototype.greaterThan = function (x, y) { return this.__compare(x, y) > 0; }; /** * Checks if the two arguments passed into the method are less than or equal * to each other. This is completely dependend of the specified comparison * function. * * @param x The second comparison subject * @param y The second comparison subject * @returns A value indicating the result of the operation */ Comparator.prototype.lessThanOrEqualTo = function (x, y) { return this.lessThan(x, y) || this.equalTo(x, y); }; /** * Checks if the two arguments passed into the method are greater than or * equal to each other. This is completely dependend of the specified * comparison function. * * @param x The second comparison subject * @param y The second comparison subject * @returns A value indicating the result of the operation */ Comparator.prototype.greaterThanOrEqualTo = function (x, y) { return this.greaterThan(x, y) || this.equalTo(x, y); }; /** * Reverses the comparison function in a way that the two passed arguments * are switched. */ Comparator.prototype.reverse = function () { var originalCompareFunction = this.__compare; this.__compare = function (x, y) { return originalCompareFunction(y, x); }; }; return Comparator; }()); exports.default = Comparator; //# sourceMappingURL=Comparator.js.map