Comparator

Configurable Comparator similar to java.util.Comparator in Java 8

目前為 2019-10-23 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/390752/743392/Comparator.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Comparator
// @namespace    hoehleg.userscripts.private
// @version      0.4
// @description  Configurable Comparator similar to java.util.Comparator in Java 8
// @author       Gerrit Höhle
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */
const Comparator = (() => {
    'use strict';

    // internal functions
    const isMissing = obj => obj === null || typeof obj === "undefined";
    const compareValues = (a, b) => (a < b) ? -1 : ((a === b) ? 0 : 1);
    const compareByMissing = (a, b) => isMissing(a) ? (isMissing(b) ? 0 : 1) : (isMissing(b) ? -1 : null);

    /**
     * Basic compare function
     * @param {*} a - Object or value to compare
     * @param {*} b - Object or value to which to compare
     * @param {boolean} [missingFirst=false] - order of "undefined" and "null". true = first, false = last.
     * @returns "-1" for a < b, "0" for a === b or "1" for a > b
     **/
    const compare = (a, b, missingFirst = false) => {
        let result = missingFirst ? compareByMissing(b, a) : compareByMissing(a, b);
        if (result !== null) {
            return result;
        }

        if (typeof a === 'string' || a instanceof String) {
            return a.localeCompare(b);
        }

        return compareValues(a, b);
    };

    /**
     * Creates the compare-function bound to a Comparator.
     * @param {Comparator} cmparator - the Comparator the compare - function should belong to
     * @returns {Function} the compare(a, b) - function
     */
    const createCompareFunction = (comparator) => {
        /**
         * Compare function bound to a comparator.
         * When executed the current setup of the comparator is considered
         * @param {*} a - Object or value to compare
         * @param {*} b - Object or value to which to compare
         **/
        return (a, b) => {
            if (comparator.isAscending) {
                const tmp = a;
                a = b;
                b = tmp;
            }

            let result = comparator.isMissingFirst ? compareByMissing(b, a) : compareByMissing(a, b);

            for (let i = 0; !result && i < comparator.sortByFunctionsAndComparators.length; i++) {
                const accessorFnc = comparator.sortByFunctionsAndComparators[i];

                if (accessorFnc instanceof Comparator) {
                    result = accessorFnc.compare(a, b);
                } else {
                    result = compare(accessorFnc(a), accessorFnc(b), comparator.isMissingFirst);
                }
            }

            return result !== null ? result : compare(a, b, comparator.isMissingFirst);
        };
    };

    // private field accessor
    const _isReverse = Symbol("isReverse");
    const _accessorFunctions = Symbol("accessorFunctions");
    const _isMissingFirst = Symbol("isMissingFirst");
    const _compareFunction = Symbol("compareFunction");

    // export class
    return class Comparator {
        /**
         * @param {Array.<function|Compator>} sortByFunctionsAndComparators - accessor-functions or comparators to evaluate values to sort by
         */
        constructor(...sortByFunctionsAndComparators) {
            this[_accessorFunctions] = sortByFunctionsAndComparators;
            this[_isReverse] = false;
            this[_isMissingFirst] = false;
            this[_compareFunction] = createCompareFunction(this);
        }

        /**
         * @type {Array.<function|Compator>} - accessor-functions and/or comparators to evaluate values to sort by
         **/
        get sortByFunctionsAndComparators() {
            return [...this[_accessorFunctions]];
        }

        get isAscending() {
            return this[_isReverse];
        }

        get isDescending() {
            return !this.isAscending;
        }

        get isMissingFirst() {
            return this[_isMissingFirst];
        }

        get isMissingLast() {
            return !this.isMissingFirst;
        }

        /**
         * @type {Function} - the compare(a, b) - function that returns "-1" for a < b, "0" for a === b or "1" for a > b.
         * the funtion is setup by this Comparator
         **/
        get compareFnc() {
            return this[_compareFunction];
        }

        /**
         * Defines that "undefined" and "null" gets sorted to the begin
         **/
        setMissingFirst() {
            this[_isMissingFirst] = true;
            return this;
        }

        /**
         * Defines that "undefined" and "null" gets sorted to the end
         **/
        setMissingLast() {
            this[_isMissingFirst] = false;
            return this;
        }

        /**
         * Reverses the sort order from ascending to descending and vice versa
         **/
        reverse() {
            this[_isReverse] = !this[_isReverse];
            return this;
        }

        /**
         * Compares an object or value to another considering the setup of this Comparator
         * @param {*} a - Object or value to compare
         * @param {*} b - Object or value to compare by
         * @returns "-1" for a < b, "0" for a === b or "1" for a > b
         **/
        compare(a, b) {
            return this.compareFnc(a, b);
        }
        
        /**
         * Compares an object or value to another one
         * @param {*} a - Object or value to compare
         * @param {*} b - Object or value to compare by
         * @param {boolean} [missingFirst=false] - order of "undefined" and "null". true = first, false = last.
         * @returns "-1" for a < b, "0" for a === b or "1" for a > b
         **/
        static compare(a, b, missingFirst = false) {
            return compare(a, b, missingFirst);
        }
    }
})();