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

enumerations

v1.1.0

Published

Type-Safe enums in JavaScript with custom properties and methods, immutability support, error checking, etc.

Downloads

56

Readme

Enumerations

A JavaScript implementation of the Type-Safe Enumeration Design Pattern.

NPM Version Build Status Test Coverage

This lightweight library takes the Java implementation of enums as an inspiration for leveraging fully functional enumerations, with support for customized properties and methods, immutability, error checking, etc.

Installation

npm install enumerations

Requirements

NodeJS 6 or higher.

Usage

A Basic Enum

After creating a class that extends from the Enum base class and calling the method #values(...constants), your enumeration is created with the provided constant values. Such as in the Java programing language, each constant is an instance of the enum class.

Furthermore, each constant value, as well the own enum class, become immutable; is no longer possible adding, modifying or removing properties.

import Enum from 'enumerations'

class PrimaryCollor extends Enum {
}

PrimaryColor.values('BLUE', 'RED', 'YELLOW')

A More Advanced Example

It's possible to implement the strategy design pattern through enums. Let's have a look on a very simple example where the four basic arithmetic operations are modelled as enum constants, each one with its own implementation of a method apply which takes two numbers as parameters:

import {AssertionError} from 'assert'
import Enum from 'enumerations'

class ArithmeticOperation extends Enum {
  
  apply(a, b) {
    throw new AssertionError('Not implemented yet')
  }

  toString() {
    return this.operator
  }
}

ArithmeticOperation.values({
  SUM: {

    operator: '+',

    apply: (a, b) => a + b
  }
}, {
  SUBTRACTION: {

    operator: '-',

    apply: (a, b) => a - b
  }
}, {
  MULTIPLICATION: {

    operator: '*',

    apply: (a, b) => a * b
  }
}, {
  DIVISION: {

    operator: '/',

    apply: (a, b) => a / b
  }
})

const x = 6
const y = 2

for (let operation of ArithmeticOperation) {
  console.log('%d %s %d = %d', x, operation.operator, y, operation.apply(x, y))
}

Overriding a method and calling the super

Let's imagine a scenario where in a sales system, each employee of a sales chain receives a commission of 20% over each sold product. However, for sales supervisors the company offers an additional bonus of $50.00 over the regular commission. This model can be designed in the following way:

import Enum from 'enumerations'

class EmployeeType extends Enum {
  computeCommission(amount) {
    return amount * 0.20
  }
}

EmployeeType.values({
  SALES_SUPERVISOR: {
    computeCommission(amount) {
      return super.computeCommission(amount) + 50.00
    }
  }
},

'SHOP_ASSISTANT',

'SALES_PERSON')

Documentation

See the API Docs for a complete reference of the properties and methods inherited by enums.

##Examples

Check out the examples/folder for more details. Additionally, take a look on the unit tests at test/ folder. They contain very comprhensive examples of the capabilities of this library.

License

Copyright (c) 2016 Alan Ghelardi.

Licensed under the MIT license.