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

enumeration

v0.1.2

Published

A javascript-based implementation of Enumeration Type.

Downloads

736

Readme

Enumeration

A javascript-based implementation of Enumeration Type.

  • For array of strings, the value of the items in the enumeration starts with zero (0) and increments by one for the succeeding items. To override the start value, set startAt property of the options (optional) parameter.

  • Each enumeration item has a function .toStringValue() which by default returns a capitalized string of the enumeration key. To customize string value, instantiate enumeration with an array of objects.

  • Accepted Parameters

    • Array of Strings ['Element0', 'Element1', .., 'ElementN']
    • Array of Objects [{Element0: 100}, {Element1: 200}, .., {ElementN: X}]
    • Object {Element0: 100, Element1: 200, .., ElementN: X}

Installation

via npm:

$ npm install enumeration

Examples

Require enumeration module

var Enumeration = require('enumeration');

3 Ways to instantiate enumeration

1. Array of strings

By default, the value of the first item is zero (0). Each successive item's value is increased by 1. To override the default start value, set the startAt property of the options parameter to your desired value (e.g. {startAt: 1}).

Each enumeration item has a function called .toStringValue() which by default returns a capitalized string of the enumeration item. To specify custom string values, pass an array of objects (see second way to instantiate).

Syntax

new Enumeration(arrayOfStrings, [options])

Sample Code

//create enumeration with array of strings    
var Colors = new Enumeration(['RED', 'GREEN', 'BLUE']);

//returns 0
Colors.RED

//returns 2
Colors.BLUE

//override default start value
var Colors = new Enumeration(['RED', 'GREEN', 'BLUE'], {startAt: 1});

//returns 1
Colors.RED

//returns 3
Colors.BLUE

//returns "Green"
Colors.GREEN.toStringValue();

**2. Array of objects **

To specify custom string values, add a property named stringValue to each item in the array of objects

Syntax

new Enumeration(arrayOfObjects)

Sample Code

//create enumeration with array of objects
var Colors = new Enumeration([{RED: 0}, {GREEN: 1}, {BLUE: 2}]);

//returns 1
Colors.GREEN    

//returns "Green"
Colors.GREEN.toStringValue()    


//specify custom string values
var Colors = new Enumeration([{RED: 0, stringValue: 'Red Roses'}, {GREEN: 1, stringValue: 'Green Light'}, {BLUE: 2, stringValue: 'Blue Book'}]);

//returns "Green Light"
Colors.GREEN.toStringValue()    

3. Object

If you have created your own javascript enumeration before, chances are this is the format that you are more familiar with.

Syntax

new Enumeration(object)

Sample Code

//create enumeration with object
var Colors = new Enumeration({RED: 0, GREEN: 1, BLUE: 2});

//returns 1
Colors.GREEN    

//returns "Green"
Colors.GREEN.toStringValue() 

Methods

//create enumeration with specified custom string values
var Colors = new Enumeration([{RED: 0, stringValue: 'Red Roses'}, {GREEN: 1, stringValue: 'Green Light'}, {BLUE: 2, stringValue: 'Blue Book'}]);


//returns enumeration keys in an array ["RED", "GREEN", "BLUE"]
Colors.getKeys();


//returns enumeration values in an array [0, 1, 2]
Colors.getValues();

//returns enumeration string values ["Red Roses", "Green Light", "Blue Book"]
Colors.getStringValues();

Others

//returns {"RED":0,"GREEN":100,"BLUE":200}
JSON.stringify(Colors)