UNPKG

@drozdik.m/comparator-handler

Version:

Class that handles comparators and makes it easy for programmer to compare objects or primitive types inside more complicated classes.

104 lines (103 loc) 4.26 kB
exports.__esModule = true; exports.ComparatorHandler = void 0; var default_comparator_1 = require("@drozdik.m/default-comparator"); /** * Class for easy handling of comparators. If explicit comparator is set, automatic * recognition of IComparable interface is turned off (on by default). It can be also turned * off manually. The class starts with default comparator for primitive types. * @author Martin Drozdík <info@bonsai-development.cz> (http://bonsai-development.cz) * */ var ComparatorHandler = /** @class */ (function () { //-------------------------------------------------- //----------CONSTRUCTOR----------------------------- //-------------------------------------------------- /** * Creates new instance of ConparatorHandler. If explicit comparator is set, * automatic recognition of IEcomparable if turned off. * @param comparator Explicit comparator */ function ComparatorHandler(comparator) { if (comparator === void 0) { comparator = null; } //-------------------------------------------------- //----------ATTRIBUTES------------------------------ //-------------------------------------------------- /** * Default comparator function. * Returns -1 if a < b * Returns 0 if a == b * Returns 1 if a > b * @param a Item #1 * @param b Item #2 */ this.DefaultComparator = function (a, b) { return (0, default_comparator_1.DefaultComparator)(a, b); }; this.comparator = this.DefaultComparator; this.automaticIComparableRecognition = true; if (comparator != null) this.SetComparator(comparator); } //-------------------------------------------------- //----------METHODS--------------------------------- //-------------------------------------------------- ComparatorHandler.prototype.Clone = function () { var newComparatorHandler = new ComparatorHandler(); newComparatorHandler.comparator = this.comparator; newComparatorHandler.automaticIComparableRecognition = this.automaticIComparableRecognition; return newComparatorHandler; }; /** * Sets new comparator to use and turns off the automatic IComparable recognition. * @param newComparator? The new comparator (nullable) */ ComparatorHandler.prototype.SetComparator = function (newComparator) { if (newComparator == null) return; this.comparator = newComparator; this.automaticIComparableRecognition = false; }; /** * Set the automatic recognition on/off * @param OnOff True for on, false for off */ ComparatorHandler.prototype.SetAutomaticIComparableRecognition = function (OnOff) { this.automaticIComparableRecognition = OnOff; }; /** * If automatic IComparable recognition is on, tries to find new comparator. * @param item Item to dig in */ ComparatorHandler.prototype.AutomaticIComparableRecognition = function (item) { //Is turned off if (!this.automaticIComparableRecognition) return; //Check if T has GetComparator() method if (typeof item.GetComparator != "undefined") this.comparator = item.GetComparator(); }; /** * Comparator function. If no explicit comparator has been set and automatic IComparable recognition * is on, the comparator function is set to a.GetComparator(). * Returns -1 if a < b * Returns 0 if a == b * Returns 1 if a > b * @param a Item #1 * @param b Item #2 */ ComparatorHandler.prototype.Compare = function (a, b) { //Fish for new comparator this.AutomaticIComparableRecognition(a); //Do the compare var res = this.comparator(a, b); if (res < 0) return -1; else if (res > 0) return 1; return 0; }; ComparatorHandler.prototype.Dispose = function () { this.comparator = null; }; return ComparatorHandler; }()); exports.ComparatorHandler = ComparatorHandler;