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.

  • 2.3.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.js 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:

import {Enum} from '@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.TUESDAY); // true
DayOfWeek.isDefined('Foo'); // false

Get the name associated to an enumerated value:

DayOfWeek.getName(DayOfWeek.TUESDAY); // "TUESDAY"
DayOfWeek.getName('Bar'); // "" (empty)

Get information about the enumerated type:

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 Apache License, version 2.0.

Keywords

FAQs

Package last updated on 11 Jul 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