Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

enum-js

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enum-js

Javascript enum object

  • 0.1.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Enum.js

Simple Enumerable object in javascript.

Usage

Creating custom Enum

Enum.extend function expect two parameters:

  • first is a static properties aka Enum constants,
  • second is a custom prototype methods.

var HTTPCodeEnum = Enum.extend({
    CONTINUE:         "100",
    OK:               "200",
    MULTIPLE_CHOISES: "300",
    BAD_REQUEST:      "400",
    INTERNAL_ERROR:   "500"
});

You can also pass in first object:

  • static functions, that will can be accessed like MyEnum.myFunction()
  • static objects,
  • static values, needed for internal usage. To prevent parsing them as Enum constant, add double underscore as prefix, like: __myInternalValue

Enum throws error in some usual cases. So there is an ability to use custom Errors for your custom Enums, to catch them upper and check with instanceOf function:


var HTTPCodeEnum = Enum.extend({
    ... // constants

    __error: MyOwnError // reference to your Error constructor
});

If __error does not present in first parameter object, then Enum will use its own EnumError constructor. Anyway, u can always get the reference to the Error function via Enum.__error

Simple usage

function ajaxCallback(data) {
    if (data.code == HTTPCodeEnum.OK) {
        // ...
    }
}

Enum usage:

function ajaxCallback(data) {

    // Throws a EnumError if catch non existing code
    var status = new HTTPCodeEnum(data.code);

    // Returns mapped message
    // getMessage must be implemented in HTTPCodeEnum.prototype
    console.log(status.getMessage());
}

// Exceptions handling

function indexController() {
    try {
        var request = $.ajax(...).done(ajaxCallback);
    } catch(e) {
        if (e instanceof HTTPCodeEnum.__error) {
            console.log('Catched non existing HTTP code!', e.message);
        }
    }
}

Keywords

FAQs

Package last updated on 20 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc