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 🙏

© 2025 – Pkg Stats / Ryan Hefner

enumerate

v0.0.1

Published

Javascript Pseudo-Enums inspired by Swift

Readme

Enumerate

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way withing your code.

Enumerate is JS Pseudo-Enums inspired by Swift.

While we aren't very concerned with type-safety in JS it is sometimes still useful to have a small convention around defining a common type for a group of related values.

Enumerate provides this convention.

Version

v0.0.1

Dependencies

  • Underscore or Lodash ~latest

Available Files

The bundled version comes with Underscore 1.8.3 bundled in.

Installation

Html

<script src="static/js/vendor/enumerate/enumerate.min.js" ></script>
<script src="static/js/main.js"></script>
// main.js
(function(window, Enumerate){

    // ...

})(window, Enumerate);

RequireJS


// standard define
define(["app/vendor/enumerate/enumerate"], function(Enumerate){

    // ...

});

// or
// commonJS style imports
var Enumerate = require("app/vendor/enumerate/enumerate");

Node

var Enumerate = require("enumerate");

Usage

Check out the specs for more usage examples!

Check out src/enumerate.js for docs / comments!

There are essentially two ways to construct an Enumerate.Enum type:

  • As an associative enum
  • As an enum with a raw value

Associative Value Enum Example

Sometimes it's useful to store associated values alongside enum values. This enables the storage of additional custom information and permits this information to vary each time.


var ProductCode = new Enumerate.Enum([
    "Barcode",
    "QrCode",
    "ISBN"
]);

var Product = function(productCode) {
    this.productCode = productCode;
}

var toy = new Product( ProductCode.Barcode("85909-51226") );

console.log(toy.productCode.value()); // => 85909-51226

Raw Value Enums

Sometimes you just need a simple data structure to store some basic values for a small set of read-only object keys. This is where raw value enums come in handy.

Raw value enums can store any valid JS type.

Raw value enums should share the same data type by convention.


var direction = new Enumerate.Enum({
    North: "north",
    South: "south",
    East: "east",
    West: "west"
});

var dir = direction.North;

switch(dir) {
    case direction.North:
        console.log("To Canada!");
        break;

    case direction.South:
        console.log("To Mexico!");
        break;

    case direction.East:
        console.log("To Europe!");
        break;

    case direction.West:
        console.log("To Japan!");

    default:
        console.log("Going Nowhere!");
}

// => To Canada!

Feeling Iterable?

// iterate over enum as a list of EnumValues

_.each(direction.values(), function(enumValue){
    alert(enumValue.value());
});

// iterate over enum as a list of keys

_.each(direction.keys(), function(key){
    alert(key);
});

// iterate over enum as a list of {key, EnumValue} pairs

_.each(direction.keyValues(), function(kv){
    alert(kv.key + ": " + kv.enumValue.value())
});

Custom Types

Enumerate.EnumValue and Enumerate.Enum are extendable via a familiar .extend method.

var DirectionEnumValue = Enumerate.EnumValue.extend({
    toString: function() {
        var orig = Enumerate.EnumValue.prototype.toString.call(this);
        return "[DirectionEnumValue] " + orig;
    }
});

var DirectionEnum = Enumerate.Enum.extend({
    ValueType: DirectionEnumValue,

    toString: function() {
        var all = _(this.values()).map(function(enumValue){
            return enumValue.toString();
        });

        return all.join(" | ");
    }
});

var direction = new DirectionEnum({
    north: "north",
    south: "south",
    east: "east",
    west: "west"
});

console.log(direction.toString());
// => [DirectionEnumValue] north | [DirectionEnumValue] south | [DirectionEnumValue] east | [DirectionEnumValue] west