# enumtype

> Enumeration type implementation

Latest version **0.0.1** (published 2018-01-28) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2018-01-28 |
| First published | 2018-01-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | sbhalim |
| Maintainers | sbhalim |
| Keywords | enum, enumeration, datatype, type, structure |

## Links

- npm: https://www.npmjs.com/package/enumtype
- Repository: https://github.com/sbhalim/es-enumtype
- Homepage: https://github.com/sbhalim/es-enumtype#readme
- Issues: https://github.com/sbhalim/es-enumtype/issues
- npm.io page: https://npm.io/package/enumtype

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.1 (latest) — 2018-01-28

## README

# Enum Type for ECMAScript

This enum type implementation focuses on elegance of code, avoids unnecessary features and ambiguity. Thus things like nested enum will never be implemented.

## Installation

```
npm i -S enumtype
```

## Usage

### Enum definition and access

```javascript

const EnumType = require('enumtype');

const MyAccessType = EnumType.define([
    'READ',
    'WRITE',
    'READWRITE'
]);

let accessType = MyAccessType.READ;
let invalidAccessType = MyAccessType.EXECUTE; // This will throw RangeError

// Print all element names
for (let name in MyAccessType) {
    console.log(name);
}
```

### Validation

```javascript
const EnumType = require('enumtype');
const MyEnum = EnumType.define(['ONE', 'TWO']);

// Verify if an object is an enum
console.log('The right way to verify enum object: ', MyEnum instanceof EnumType);

// Use in operator to validate both enum labels and values
console.log('Label check:', 'ONE' in MyEnum); // true
let oneValue = MyEnum.ONE;
console.log('Value check:', oneValue in MyEnum); // true

// Elements from different definitions are not equal
const MyEnum2 = EnumType.define(['ONE', 'TWO']);
console.log('Are two similar enum definitions equal?', MyEnum.ONE === MyEnum2.ONE); // false
```

### Serialization

```javascript
// Don't use JSON.stringify to get JSON representation of enum
console.log('JSON of MyEnum', EnumType.toJson(MyEnum));
```

### Caveats

```enumtype``` uses ```Proxy``` to enforce valid element access by throwing error when an undefined element is accessed. On the other hand ```JSON.stringify``` implementation of current NodeJS (last checked on v8.x) seems to check the existence of ```toJSON``` method by actually accessing it, not using something like ```in``` operator. Therefore calling ```JSON.stringify```directly on ```enumtype``` object will throw an error.

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