# enumerations

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

Latest version **1.1.0** (published 2016-10-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install enumerations
pnpm add enumerations
yarn add enumerations
bun add enumerations
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2016-10-17 |
| First published | 2016-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alan Ghelardi |
| Maintainers | alan-ghelardi |
| Keywords | enum, enumeration, utility |

## Links

- npm: https://www.npmjs.com/package/enumerations
- Repository: https://github.com/alan-ghelardi/enumerations
- Homepage: https://github.com/alan-ghelardi/enumerations#readme
- Issues: https://github.com/alan-ghelardi/enumerations/issues
- npm.io page: https://npm.io/package/enumerations

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2016-10-17
- 0.10.2 — 2016-04-23
- 0.10.1 — 2016-04-23
- 0.10.0 — 2016-04-23

## README

# Enumerations

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

[![NPM Version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]

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

```shell
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.

```js
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:  

```js
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: 

```js
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](docs/enum.md) 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.

[npm-image]: https://img.shields.io/badge/enumerations-stable-brightgreen.svg
[npm-url]: https://www.npmjs.com/package/enumerations
[travis-image]: https://travis-ci.org/alan-ghelardi/enumerations.svg?branch=master
[travis-url]: https://travis-ci.org/alan-ghelardi/enumerations
[coveralls-image]:https://coveralls.io/repos/github/alan-ghelardi/enumerations/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/alan-ghelardi/enumerations?branch=master

---
_Source: https://npm.io/package/enumerations · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
