Greasy Fork is available in English.

Enum

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

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/391854/746956/Enum.js

Autor
Gerrit
Wersja
0.3
Utworzono
01-11-2019
Zaktualizowano
05-11-2019
Licencja
Brak licencji

Usage

Object initialization

const COLORS = class COLOR extends Enum {}.init({ "RED": "red" , "GREEN": "green" , "BLUE": "blue" }); console.log(COLORS[0] === COLORS.RED); // "true" console.log(COLORS[1] === COLORS.GREEN); // "true" console.log(COLORS[2] === COLORS.BLUE); // "true" console.log(COLORS.GREEN.name); // "GREEN" console.log(COLORS.GREEN.text); // "green" console.assert(COLORS.GREEN.ordinal); // "1" console.assert(COLORS.GREEN + ""); // "green" console.assert(COLORS.GREEN * 1); // "1"

Array initialization

const COLORS = class COLOR extends Enum {}.init([ "RED", "GREEN", "BLUE" ]); console.log(COLORS[2].name) // "BLUE" console.log(COLORS[2].text); // "" console.log(COLORS[2].ordinal); // "2" console.log(COLORS[2] === COLORS.BLUE); // "true" console.log(COLORS.BLUE + ""); // "BLUE" console.log(COLORS.BLUE * 1); // "2"

Ordinal value generator

class FLAGS extends Enum {}; FLAGS.init([ "FIRST", "SECOND", "THIRD", "FOURTH" ], 1, ord => ord<<1); console.log(FLAGS.FOURTH | FLAGS.SECOND); // "10"