您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Enumeration class. Each enum propertiy has the properties "ordinal", "name" and "text".
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/391854/746195/Enum.js
- // ==UserScript==
- // @name Enum
- // @namespace hoehleg.userscripts.private
- // @version 0.2
- // @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 initializing = Symbol("initializing");
- const createEnumInstance = (enumClass, name, ordinal, text = "") => {
- text = String(text);
- return Object.assign(new enumClass(initializing), {
- get ordinal() { return ordinal; },
- get name() { return name; },
- get text() { return text; },
- });
- };
- return class EnumBase {
- constructor(processIndicator) {
- if (processIndicator !== initializing) {
- throw TypeError("Instantiation of abstract enum class");
- }
- }
- valueOf() { return this.ordinal; }
- toString() { return this.text || this.name; }
- static init(enumDef = [], firstOrdinal = 0, ordinalSupplier = previousOrdinal => previousOrdinal + 1) {
- if (Object.isFrozen(this)) {
- throw TypeError("Reinitialization of finalized enum class");
- }
- let ordinal;
- const ordinals = [];
- for (let enumDefObj of (Array.isArray(enumDef) ? enumDef : [ enumDef ])) {
- if (typeof enumDefObj !== 'object') {
- enumDefObj = { [enumDefObj]: "" };
- }
- for (let [name, text] of Object.entries(enumDefObj)) {
- ordinal = Number.parseInt((typeof ordinal === "undefined") ? firstOrdinal : ordinalSupplier(ordinal));
- console.assert(typeof this[name] === "undefined", `duplicate enum [${name}]`);
- console.assert(typeof this[ordinal] === "undefined", `duplicate ordinal [${ordinal}] for enum [${name}]`);
- ordinals.push(ordinal);
- this[name] = createEnumInstance(this, name, ordinal, text);
- Object.defineProperty(this, ordinal, { value: this[name] });
- }
- }
- const enums = ordinals.sort().map(ordinal => this[ordinal]);
- Object.defineProperty(this, Symbol.iterator, { value: () => enums[Symbol.iterator]() });
- return Object.freeze(this);
- }
- };
- })();