Skip to content

rxluz/js-enum

Repository files navigation

@rxluz/js-enum Tweet

An ENUM implementation in JS

Build Status codecov Commitizen friendly

Usage

Example (es module)

import jsENUM from '@rxluz/js-enum';

const direction = jsENUM('UP', 'DOWN', 'LEFT', 'RIGHT');

const myDirection = direction.DOWN;

Example (commonjs)

var jsENUM = require('@rxluz/js-enum').default;

const direction = jsENUM('UP', 'DOWN', 'LEFT', 'RIGHT');

const myDirection = direction.DOWN;

Using custom values

The default value to each element of your enum will be a sequential number (eg. UP: 0, DOWN: 1, LEFT: 2, RIGHT, 3), but you could change this putting a custom value to each element

const direction = jsENUM(['UP', 'UP'], 'DOWN', 'LEFT', 'RIGHT');

const myDirection = direction.DOWN;

In the example above the values will be (eg. UP: UP, DOWN: 1, LEFT: 2, RIGHT, 3)

Install

With npm installed, run

$ npm i @rxluz/js-enum --save

Acknowledgments

  • This project was inspired by a chat in NodeJS Brasil Telegram Group.
  • rxluz

See Also

License

MIT