Enum

Enumeration class. Each enum propertiy has the properties "ordinal", "name" and "text".

اعتبارا من 01-11-2019. شاهد أحدث إصدار.

لا ينبغي أن لا يتم تثبيت هذا السكريت مباشرة. هو مكتبة لسكبتات لتشمل مع التوجيه الفوقية // @require https://update.greasyfork.org/scripts/391854/745818/Enum.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         Enum
// @namespace    hoehleg.userscripts.private
// @version      0.1
// @description  Enumeration class. Each enum propertiy has the properties "ordinal", "name" and "text".
// @author       Gerrit Höhle
// @grant        none
// ==/UserScript==

/* jslint esnext: true */
const Enum = (() => {
    const add = (thisObj, name, ordinal, text = "") => {
        text = String(text);
        const enumInstance = new class EnumProperty {
            get ordinal() { return ordinal; }
            get name() { return name; }
            get text() { return text; }
            [Symbol.toPrimitive](hint) { return (hint === "number") ? this.ordinal : this.text; }
            valueOf() { return this.ordinal; }
            toString() { return `object Enum ${this.name}`; }
        }();

        console.assert(typeof thisObj.name === "undefined", `duplicate enum [${name}]`);
        console.assert(typeof thisObj.ordinal === "undefined", `duplicate ordinal [${ordinal}] for enum [${name}]`);

        thisObj[name] = thisObj[ordinal] = enumInstance;
    };

    return class Enum {
        constructor(firstOrdinal = 0, ordinalSupplier = previousOrdinal => previousOrdinal + 1) {
            this._firstOrdinal = firstOrdinal;
            this._ordinalSupplier = ordinalSupplier;
        }

        init(enumDef = []) {
            let ordinal;
            const firstOrdinal = this._firstOrdinal;
            const ordinalSupplier = this._ordinalSupplier;

            Object.getOwnPropertyNames(this).forEach(propName => delete this[propName]);

            const ordinals = [];
            for (let enumDefObj of (Array.isArray(enumDef) ? enumDef : [ enumDef ])) {
                for (let [name, text] of Object.entries(enumDefObj)) {
                    ordinal = Number.parseInt((typeof ordinal === "undefined") ? firstOrdinal : ordinalSupplier(ordinal));
                    add(this, name, ordinal, text);
                    ordinals.push(ordinal);
                }
            }
            const enums = [ ...new Set(ordinals) ].sort().map(ordinal => this[ordinal]);

            this[Symbol.iterator] = () => enums[Symbol.iterator]();

            return Object.freeze(this);
        }
    };
})();