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

enum_js

v0.1.1

Published

Enum class for javascript

Downloads

4

Readme

enumjs

Build Status Coverage Status

A Enum class library in javascript.

quick start

define with string or string array

var OS = new Enum('LINUX,WINDOWS');              // LINUX: 0, WINDOWS: 1
var MOBILE = new Enum(['ANDROID', 'IOS']);       // ANDROID: 0, IOS: 1

OS.LINUX.equals(0);           // true
+MOBILE.IOS === 1;            // true

The simplest way to create an Enum is using a string, in which you can change the seperators , with ; or \s. Or you can use an array to store your keys, which is recommanded.

define with object array

var PETS = new Enum([
    { key: 'DOG', name: 'DingDong', value: 100 },
    { key: 'CAT', name: 'Lily', value: 200 },
    { key: 'LIZARD', name: 'Keith', value: 300 },
]);     // DOG: 100, CAT: 200, LIZARD: 300

PETS.DOG.getName();               // DingDong
PETS.find(300).getName();        // Keith

In some cases, you might connect your enum keys to some other info, so you can just create your enum with an Object array, storing your relative infomation with your enum keys.

You can name the custom property whatever you want, but you can only access the propery via the get method, just like the javabean style, instead you cannot modify the property after defination.

usage

compare

The value of the Enum class is not a Number type, it cannot be compare to a number with === directly. Instead, method equals or eq is recommanded, or you can convert it to Number type explicitly.

var ENUMS = new ('A,B');

ENUMS.A == 0;           // true, not recommanded
ENUMS.A === 0;          // false
+ENUMS.A === 0;         // true
ENUMS.getValue() === 0; // true
ENUMS.v() === 0;        // true
ENUMS.A.equals(0);      // true
ENUMS.A.eq(0);          // true

get

The enum item can be accessed by it's value. An Enum item can

var GREETING = new ([
    { key: 'ENGLISH', greeting: 'Hello', value: 100 },
    { key: 'DEUTSCH', greeting: 'Guten tag', value: 200 },
    { key: 'CHINESE', greeting: '你好', value: 300 },
]);

GREETING.find(300);                         // GREETING.CHINESE
GREETING.find(300).getGreeting();           // 你好
GREETING.CHINESE.getGreeting();                // 你好
GREETING.find(function(item){
    return item.k() === 'CHINESE';
    // return item.k() === 'CHINESE';
}).getGreeting();                               // 你好

forEach

GREETING.forEach(function(item, key){
    console.log(key, item.v());
    // ENGLISH 100
    // DEUTSCH 200
    // CHINESE 300
});

map

var arr = GREETING.map(function(item, key){
    return key;
});
// [ 'ENGLISH', 'DEUTSCH', 'CHINESE' ]

filter

var TYPE = new Enum('A,B,C');
var arr = TYPE.filter(function(item, key){
    return item.v() > 0;
});
// [TYPE.B, TYPE.C]

keys

var TYPE = new Enum('A,B,C');
var keys = TYPE.keys();
// ['A', 'B', 'C']

values

var TYPE = new Enum('A,B,C');
var keys = TYPE.values();
// [0, 1, 2]

pick

var FRUIT = new Enum([
  {
    key: 'GRAPE',
    name: 'grape',
    color: 'purple'
  }
]);

var grape = FRUIT.GRAPE.pick(['name', 'color'])
// { name: 'grape', color: 'purple' }

pickAs

var FRUIT = new Enum([
  {
    key: 'GRAPE',
    name: 'grape',
    color: 'purple'
  }
]);

var grape = FRUIT.GRAPE.pickAs({
  name: 'fruit',
  color: 'colour'
});
// { fruit: 'grape', colour: 'purple' }