Enum

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

2019-11-01 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.greasyfork.org/scripts/391854/745818/Enum.js을(를) 사용하여 포함하는 라이브러리입니다.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 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);
        }
    };
})();