babel-plugin-objective-enums

1.0.4 • Public • Published

babel-plugin-objective-enums

Babel plugin allowing to use enum syntax in JavaScript projects. It wraps objective-enums and it requires Babel 7.

Some code fragments are from babel-plugin-transform-typescript.

Installation

$ npm install --save-dev babel-plugin-objective-enums

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["babel-plugin-objective-enums"]
}

Via CLI

$ babel --plugins babel-plugin-objective-enums script.js

Via Node API

require('babel-core').transform('code', {
  plugins: ['babel-plugin-objective-enums']
});

All-in-one Example

enum Colors {
  Red = '#FF0000',
  Yellow = {r: 255, g: 255, b: 0},
  Green = 0x008000,
  Blue = true,
  White,
  Black = White * 50
}
 
// Get color value and name
console.log(Colors.Red.value + ' - ' + Colors.Red.toString()); // #FF0000 - Red
 
// 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"]
 
// Get common elements' names of allowed and selected colors
const selected = Colors.Red | Colors.Yellow | Colors.Black;
console.log(Colors.intersect(allowed, selectedColors)); // ["Yellow"]

Package Sidebar

Install

npm i babel-plugin-objective-enums

Weekly Downloads

0

Version

1.0.4

License

MIT

Unpacked Size

11.6 kB

Total Files

8

Last publish

Collaborators

  • traffician