Enum

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

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/391854/746956/Enum.js

Autor
Gerrit
Verzija
0.3
Napravljeno
01/11/2019
Ažurirano
05/11/2019
Licenca
Nije dostupno

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"