enumify-x
Enumerated type library.
Author: Xotic750 Xotic750@gmail.com
License: MIT
Copyright: Xotic750
- enumify-x
module.exports
⇒Object
⏏~create
⇒function
module.exports
⇒ Object
⏏
An enumeration is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over. Influenced by Python's Enum implimentation.
Create an enum name/value. Not usually called directly.
Kind: Exported member
Returns: Object
- The enum.
See: https://docs.python.org/3/library/enum.html
Param | Type | Description |
---|---|---|
name | string |
The name of the enum. |
* |
value The value of the enum. |
module.exports~create
⇒ function
Creates an enumeration collection. Primary method.
Kind: inner property of module.exports
Returns: function
- The enumeration collection.
Param | Type | Description |
---|---|---|
typeName | string |
The name of the enum collection. |
properties | Array |
Initialiser array. |
options | Object |
Options to determine behaviour. |
Example
; // Creating an Enum// Example allows duplicate values, known as aliases.// Member values can be anything: number, string, etc.. If the exact value is// unimportant you may use auto instances and an appropriate value will be// chosen for you. Care must be taken if you mix auto with other values.//// The class color is an enumeration (or enum)// The attributes color.RED, color.GREEN, etc., are enumeration members// (or enum members) and are functionally constants.// The enum members have names and values (the name of color.RED is RED,// value of color.BLUE is 10, etc.)const color = Enum; console; // { name: 'YELLOW', value: 1 }console; // 'BLUE'console; // 10console; // true// Enumeration members have human readable string representations.colorPINK; // 'color.PINK'// Enums also have a human readable string representations.color; // 'color { "RED", "YELLOW", "BLUE", "PINK", "BLACK" }'// The type of an enumeration member is the enumeration it belongs to.console; // true// You can access by value too.console; // color.BLUE // Enumeration members are hashable, so they can be used as property names.const apples = {};applescolorRED = 'Red Delicious';applescolorYELLOW = 'Golden Delicious';console; // {color.RED: 'Red Delicious', color.YELLOW: 'Golden Delicious'} // No aliases are allowed in this example.const opts = unique: true; // Having two enum members with the same name is invalidEnum; // However, two enum members are allowed to have the same value. Given two// members A and B with the same value (and A defined first), B is an alias// to A. By-value lookup of the value of A and B will return A. By-name// lookup of B will also return A. as seen in the definition of color. const color1 = Enum; // Depending on the value types used, enumerations are serialisable.JSON; // '[{"name":"RED","value":0},{"name":"YELLOW","value":1}]' // Enumerations support iteration, in definition order.// The forEach() method executes a provided function once per each// name/value pair in the Enum object, in insertion order.// Iterating over the members of an enum does not provide the aliases.color1; // Where supported, for..of can be used.// Iterating over the members of an enum does not provide the aliases.for const name value of color1 console; // Otherwise, standard iterator pattern.// Iterating over the members of an enum does not provide the aliases.const iter = color1Symboliterator;let next = iternext;while nextdone === false const enumMember = nextvalue; console; next = iternext; // To iterate all items, including aliases.const allenumMembers = color1;allenumMembers; // Lookups can be performed on the value and not just the name.console; // trueconsole; // true // Values can be anything, but names must be a string.const anotherEnum = Enum; // Enums can be clonedconst cloneEnum = Enum;console; // falseconsole; // falseconsole; // trueconsole; // true // Options// unique: {boolean} - whether aliases are allowed.// auto: {Function} - if you wish to define your own auto value allocation.// classMethods: {Object<Function>} - to defined methods on the enum.// instanceMethods: {Object<Function>} - to defined methods on the enum members. // ------------------------------------------------------ const opts1 = { return { return name; } ; }; const subject1 = Enum;console; // { name: 'RED', value: 'RED'} // ------------------------------------------------------ const opts2 = classMethods: { return thisRED; } ; const subject2 = Enum;console; // true // ------------------------------------------------------ const opts3 = instanceMethods: { return `Description: `; } ; const subject3 = Enum;console; // true