Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

Enumjs

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Enumjs

A simple function for creating enum object in javascript

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Enumjs

Create an enum from a object of key/values.

Install with npm.

npm install enumjs --save

Use with node.js, browserify or webpack, etc:

var Enum = require('Enumjs');

Or you can use a <script> tag to include the index.js and it will create a global Enum object, or define that object for require.js if it exists.

Usage

Once the package is installed you can create instances by passing the key/value object to the constructor.

var Enum = require('Enumjs');

const Suits = Enum({
  HEART: 'HEART',
  DIAMOND: 'DIAMOND',
  SPADE: 'SPADE',
  CLUB: 'CLUB',
});

Your new Enum is a plain object that's been frozen so no one can edit the fields or values on it. It is enumerable, immutable, and you can the in operator, hasOwnProperty() just like normal.

console.log('HEART' in Suits)
-> true
console.log(Suits.hasOwnProperty('HEART')
-> true
console.log(Suits.HEART)
-> 'HEART'
console.log(Suits[0])
-> 'HEART'
console.log(Suits.length)
-> 4

Also provided are helper methods Enum.coalesce(enum: Enum, field: string) and Enum.enforce(enum: Enum, field: string). Each of these test whether the field provided is a member of the enum and return the value if it is. If the value is not a member then coalesce returns null, while enforce throws an error.

console.log(Enum.coalesce(Suits, 'HEART'))
-> 'HEART'
console.log(Enum.coalesce(Suits, 'foobar'))
-> null
console.log(Enum.enforce(Suits, 'SPADE'))
-> 'SPADE'
console.log(Enum.enforce(Suits, 'bizbaz')) // throws Error

Flow.js Types

To turn Enum objects into flow types simply create a flow type using the $Keys feature:

type SuitType = $Keys<typeof Suits>;

If your values are not the same as the keys then you'll probably just want to type out the flow type as a union of values.

const Suits = Enum({
  HEART: 'suit_hearts',
  DIAMOND: 'suit_diamonds',
  SPADE: 'suit_spades',
  CLUB: 'suit_clubs',
});

type SuitType =
  'suit_diamonds' | 'suit_diamonds' |
  'suit_spades' | 'suit_clubs';

Polyfills

Within index.js some new methods are used that are not available on older browsers. See Object.create Object.entries Object.freeze Object.values

For Tests: Similarly the test file also requires Object.keys.

Array.isArray: see MDN for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill.

Object.keys: see MDN for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill. This is only used in dedupe.js.

Keywords

FAQs

Package last updated on 03 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc