Objective-like enums
Yet another JavaScript library introducing Enumerated Types 😊
It was inspired by C#'s enums and that's why it allows for comparing types, serialization and flagging.
Installation
npm install --save objective-enums
Contributing
All pull requests are welcome!
Library is distributed as Babel-compiled code. If you want to contribute, clone GitHub repo, make changes and create pull request.
You can build the code by using npm run dev
or npm run prod
commands. There is also file watching feature - npm run watch
, so you don't have to rebuild all sources again after modyfing some scripts.
Testing
@TODO
Examples
Quick start
; const Colors = 'Red' 'Yellow' 'Green' 'Blue';const allowedColors = ColorsGreen | ColorsBlue; // Get color value and nameconsole; // 1 - Red // Check if red is an allowed colorconsole; // false // Get names of allowed colorsconsole; // ["Green", "Blue"] // Get common elements' names of allowed and selected colorsconst selectedColors = ColorsRed | ColorsGreen | ColorsYellow;console; // ["Green"]
Flagging operations
; const Colors = 'Red' 'Yellow' 'Green' 'Blue'; // Only green and blue are allowed colorslet allowed = ColorsGreen | ColorsBlue; // Get names of allowed colorsconsole; // ["Green", "Blue"] // Add yellow to allowed colorsallowed |= ColorsYellow;console; // ["Green", "Blue", "Yellow"] // Remove blue from allowed colorsallowed &= ~ColorsBlue;console; // ["Green", "Yellow"]
Custom values
; const Colors = Red: '#FF0000' Yellow: r: 255 g: 255 b: 0 Green: 0x008000 Blue: true; console; // {r: 255, g: 255, b: 0}
Custom typing and comparing
; const JewelleryEnum = {};const Jewellery = 'Amethysts' 'Diamonds' 'Emeralds' 'Gems'; const Cards = new {} 'Clubs' 'Diamonds' 'Spades' 'Hearts'; console; // true console; // Cards console; // JewelleryElement console; // CardsElement console; // false const someStones = JewelleryDiamonds | JewelleryGems;console;// NotAnElementOf: Spades is not an element of Jewellery