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

@cedx/enum

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cedx/enum

A simple implementation of enumerated types.

  • 3.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20
decreased by-71.83%
Maintainers
1
Weekly downloads
 
Created
Source

Enums for JS

Runtime Release License Downloads Dependencies Coverage Build

Yet another implementation of enumerated types for JavaScript.

This implementation does not try to reproduce the semantics of traditional enumerations, like the ones found in C# or Java languages. It just gives a set of static methods to ease working with the values of an object literal representing an enumerated type.

Requirements

The latest Node.js and npm versions. If you plan to play with the sources, you will also need the latest Gulp version.

Installing via npm

From a command prompt, run:

$ npm install --save @cedx/enum

Usage

Create the enumeration

Just use the Enum.create() method with an object literal containing scalar values (e.g. only booleans, numbers and strings):

const {Enum} = require('@cedx/enum');

/**
 * Specifies the day of the week.
 * @type {object}
 */
const DayOfWeek = Enum.create({
  sunday: 0,
  monday: 1,
  tuesday: 2,
  wednesday: 3,
  thursday: 4,
  friday: 5,
  saturday: 6
});

The Enum.create() method creates an anonymous class from the specified object. This class has the same values as the provided object, and some additional helper methods.

The created class has a constructor throwing a TypeError: it prohibits its instantiation. This class is also freezed to prevent any attempt at modifying its shape.

Work with the enumeration

Check whether a value is defined among the enumerated type:

DayOfWeek.isDefined(DayOfWeek.sunday); // true
DayOfWeek.isDefined('foo'); // false

Ensure that a value is defined among the enumerated type:

DayOfWeek.assert(DayOfWeek.monday); // DayOfWeek.monday
DayOfWeek.assert('foo'); // (throws TypeError)

DayOfWeek.coerce(DayOfWeek.monday); // DayOfWeek.monday
DayOfWeek.coerce('bar'); // null
DayOfWeek.coerce('baz', DayOfWeek.tuesday); // DayOfWeek.tuesday

Get the zero-based position of a value in the enumerated type declaration:

DayOfWeek.getIndex(DayOfWeek.wednesday); // 3
DayOfWeek.getIndex('foo'); // -1

Get the name associated to an enumerated value:

DayOfWeek.getName(DayOfWeek.thursday); // "thursday"
DayOfWeek.getName('foo'); // "" (empty)

Get information about the enumerated type:

DayOfWeek.getEntries();
// [["sunday", 0], ["monday", 1], ["tuesday", 2], ["wednesday", 3], ["thursday", 4], ["friday", 5], ["saturday", 6]]

DayOfWeek.getNames();
// ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]

DayOfWeek.getValues();
// [0, 1, 2, 3, 4, 5, 6]

See also

License

Enums for JS is distributed under the MIT License.

Keywords

FAQs

Package last updated on 03 Nov 2017

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