Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/traffician/objective-enums
Objective-like Enumerated Types in JavaScript and Node
https://github.com/traffician/objective-enums
ecmascript enum enumerated-types javascript nodejs objective-like
Last synced: 12 days ago
JSON representation
Objective-like Enumerated Types in JavaScript and Node
- Host: GitHub
- URL: https://github.com/traffician/objective-enums
- Owner: Traffician
- License: mit
- Created: 2018-05-26T17:59:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-09-05T16:52:18.000Z (over 6 years ago)
- Last Synced: 2024-10-12T09:37:49.089Z (3 months ago)
- Topics: ecmascript, enum, enumerated-types, javascript, nodejs, objective-like
- Language: JavaScript
- Homepage:
- Size: 26.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```javascript
import Enum from 'objective-enums';const Colors = new Enum('Red', 'Yellow', 'Green', 'Blue');
const allowedColors = Colors.Green | Colors.Blue;// Get color value and name
console.log(Colors.Red + ' - ' + Colors.Red.toString()); // 1 - Red// Check if red is an allowed color
console.log(Colors.hasFlag(allowedColors, Colors.red)); // false// Get names of allowed colors
console.log(Colors.match(allowedColors)); // ["Green", "Blue"]// Get common elements' names of allowed and selected colors
const selectedColors = Colors.Red | Colors.Green | Colors.Yellow;
console.log(Colors.intersect(allowedColors, selectedColors)); // ["Green"]
```## Flagging operations
```javascript
import Enum from 'objective-enums';const Colors = new Enum('Red', 'Yellow', 'Green', 'Blue');
// Only green and blue are allowed colors
let allowed = Colors.Green | Colors.Blue;// Get names of allowed colors
console.log(Colors.match(allowed)); // ["Green", "Blue"]// Add yellow to allowed colors
allowed |= Colors.Yellow;
console.log(Colors.match(allowed)); // ["Green", "Blue", "Yellow"]// Remove blue from allowed colors
allowed &= ~Colors.Blue;
console.log(Colors.match(allowed)); // ["Green", "Yellow"]
```## Custom values
```javascript
import Enum from 'objective-enums';const Colors = new Enum({
Red: '#FF0000',
Yellow: {r: 255, g: 255, b: 0},
Green: 0x008000,
Blue: true
});console.log(Colors.Yellow.value); // {r: 255, g: 255, b: 0}
```## Custom typing and comparing
```javascript
import Enum from 'objective-enums';const JewelleryEnum = class Jewellery extends Enum {};
const Jewellery = new JewelleryEnum('Amethysts', 'Diamonds', 'Emeralds', 'Gems');const Cards = new (class Cards extends Enum {})([
'Clubs', 'Diamonds', 'Spades', 'Hearts'
]);console.log(Jewellery instanceof Enum); // true
console.log(Cards.constructor.name); // Cards
console.log(Jewellery.Amethysts.constructor.name); // JewelleryElement
console.log(Cards.Spades.constructor.name); // CardsElement
console.log(Jewellery.Diamonds === Cards.Diamonds); // false
const someStones = Jewellery.Diamonds | Jewellery.Gems;
console.log(Jewellery.hasFlag(someStones, 'Spades'));
// NotAnElementOf: Spades is not an element of Jewellery```