Greasy Fork is available in English.

Enum

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

Tätä skriptiä ei tulisi asentaa suoraan. Se on kirjasto muita skriptejä varten sisällytettäväksi metadirektiivillä // @require https://update.greasyfork.org/scripts/391854/746956/Enum.js.

Tekijä
Gerrit
Versio
0.3
Luotu
1.11.2019
Päivitetty
5.11.2019
Lisenssi
N/A

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"