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.
$ npm install --save-dev babel-plugin-objective-enums
.babelrc
{
"plugins": ["babel-plugin-objective-enums"]
}
$ babel --plugins babel-plugin-objective-enums script.js
require('babel-core').transform('code', {
plugins: ['babel-plugin-objective-enums']
});
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"]