UNPKG

@barchart/common-js

Version:
148 lines (142 loc) 4.78 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var ComparatorBuilder_exports = {}; __export(ComparatorBuilder_exports, { default: () => ComparatorBuilder }); module.exports = __toCommonJS(ComparatorBuilder_exports); var assert = __toESM(require("./../../lang/assert.js")); var comparators = __toESM(require("./comparators.js")); class ComparatorBuilder { #comparator; #invert; #previous; /** * @param {Function} comparator - The initial comparator. * @param {boolean=} invert - Indicates if the comparator should sort in descending order. * @param {ComparatorBuilder=} previous - The previous comparator builder in the chain. */ constructor(comparator, invert, previous) { assert.argumentIsRequired(comparator, "comparator", Function); assert.argumentIsOptional(invert, "invert", Boolean); this.#comparator = comparator; this.#invert = invert || false; this.#previous = previous || null; } /** * Adds a new comparator to the list of comparators to use. * * @public * @param {Function} comparator - The next comparator function. * @param {boolean=} invert - Indicates if the comparator should sort in descending order. * @returns {ComparatorBuilder} */ thenBy(comparator, invert) { assert.argumentIsRequired(comparator, "comparator", Function); assert.argumentIsOptional(invert, "invert", Boolean); return new ComparatorBuilder(comparator, invert, this); } /** * Flips the order of the comparator (e.g. ascending to descending). * * @public * @returns {ComparatorBuilder} */ invert() { let previous; if (this.#previous) { previous = this.#previous.invert(); } else { previous = null; } return new ComparatorBuilder(this.#comparator, !this.#invert, previous); } /** * Returns the comparator function. * * @public * @returns {Function} */ toComparator() { let previousComparator; if (this.#previous) { previousComparator = this.#previous.toComparator(); } else { previousComparator = comparators.empty; } return (a, b) => { let result = previousComparator(a, b); if (result === 0) { let sortA; let sortB; if (this.#invert) { sortA = b; sortB = a; } else { sortA = a; sortB = b; } result = this.#comparator(sortA, sortB); } return result; }; } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return "[ComparatorBuilder]"; } /** * Creates a {@link ComparatorBuilder}, given an initial comparator function. * * @public * @static * @param {Function} comparator - The initial comparator. * @param {boolean=} invert - Indicates if the comparator should sort in descending order. * @returns {ComparatorBuilder} */ static startWith(comparator, invert) { return new ComparatorBuilder(comparator, invert); } } { const cjsExports = module.exports; const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports; if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) { Object.keys(cjsExports).forEach((key) => { if (key !== 'default' && key !== '__esModule') { cjsDefaultExport[key] = cjsExports[key]; } }); } module.exports = cjsDefaultExport; }