Git Product home page Git Product logo

enumjs's Introduction

Enumjs

Current Version Build Status Greenkeeper badge

Create an enum from a object of key/values.

Install with npm.

# Capitalization matters!
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.make({
  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 use 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

Configure flow by adding the path to Enumjs/interfaces into your .flowconfig file:

[libs]
node_modules/Enumjs/interfaces/

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 can use $Keys and $Values to differentiate between the two sets:

type SuitType = $Keys<typeof Suits>;
type SuitValue = $Values<typeof Suits>;

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

Unit Tests: The tests also requires Object.keys.

enumjs's People

Contributors

greenkeeper[bot] avatar philippotto avatar ryan953 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

scalableminds

enumjs's Issues

An in-range update of object.values is breaking the build 🚨

The devDependency object.values was updated from 1.0.4 to 1.1.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

object.values is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 12 commits.

  • 4d32953 v1.1.0
  • 81de647 [Tests] up to node v11.6, v10.15, v8.15, v6.16
  • 1bf1dbe [Dev Deps] update eslint, @ljharb/eslint-config, covert, tape
  • 428fb50 [Tests] use npm audit instead of nsp
  • a9d5f94 [Tests] remove jscs
  • 103a907 [Tests] up to node v11.1, v10.13, v9.11, v8.12, v7.10, v6.14, v4.9; use nvm install-latest-npm
  • 13c5dd1 [Dev Deps] update eslint, @ljharb/eslint-config, nsp, tape
  • f35eb85 Only apps should have lockfiles
  • ed730e3 [New] add auto entry point
  • faa4051 [Deps] update define-properties, es-abstract, function-bind, has
  • 7cbc7f3 [Dev Deps] add missing object-keys dev dep
  • 66caa4a [Tests] up to node v7.4, v4.7; improve test matrix

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of object.entries is breaking the build 🚨

The devDependency object.entries was updated from 1.0.4 to 1.1.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

object.entries is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 12 commits.

  • e19be64 v1.1.0
  • 89415ba [Tests] up to node v11.6, v10.15, v8.15, v6.16
  • 074677d [Dev Deps] update eslint, @ljharb/eslint-config, covert, tape
  • bc3e6a7 [Tests] use npm audit instead of nsp
  • 052aed1 [Tests] remove jscs
  • 0015678 [Tests] up to node v11.1, v10.13, v9.11, v8.12, v7.10, v6.14, v4.9; use nvm install-latest-npm
  • 91f9ee6 [Dev Deps] update eslint, @ljharb/eslint-config, nsp, tape
  • 9799c0d [New] add auto entry point`
  • db5d97e [Deps] update define-properties, es-abstract, function-bind, has
  • d192ab7 Only apps should have lockfiles
  • 6f19e66 [Tests] up to node v7.4, v4.7; improve test matrix
  • 3ae9c91 Exclude test.html from the npm package.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.