npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

Enumjs

v1.0.3

Published

A simple function for creating enum object in javascript

Downloads

11

Readme

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

Flow Types

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.