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 - npm Package Compare versions

Comparing version 2.4.0 to 3.0.0

6

CHANGELOG.md
# Changelog
This file contains highlights of what changes on each version of the [Enums for JS](https://github.com/cedx/enum.js) library.
## Version 3.0.0
- Added the `assert()` method.
- Added the `coerce()` method.
- Added the `getEntries()` method.
- Added the `getIndex()` method.
## Version 2.4.0

@@ -5,0 +11,0 @@ - Removed the dependency on [Babel](https://babeljs.io) compiler.

66

lib/enum.js

@@ -9,2 +9,14 @@ 'use strict';

/**
* Returns the specified value if it exists in the specified enumeration, otherwise throws an exception.
* @param {object} enumType An enumerated type.
* @param {*} value The value of a constant in the specified enumeration.
* @return {*} The specified enumerated constant.
* @throws {TypeError} No such constant was found.
*/
static assert(enumType, value) {
if (Enum.isDefined(enumType, value)) return value;
throw new TypeError(`Invalid enumerated value: ${value}`);
}
/**
* Creates an enumeration from the specified type definition.

@@ -18,3 +30,7 @@ * @param {object} typeDef An object defining the shape of the enumerated type.

constructor() { throw new TypeError('This type is not instantiable.'); }
static assert(value) { return Enum.assert(enumType, value); }
static coerce(value, defaultValue = null) { return Enum.coerce(enumType, value, defaultValue); }
static isDefined(value) { return Enum.isDefined(enumType, value); }
static getEntries() { return Enum.getEntries(enumType); }
static getIndex(value) { return Enum.getIndex(enumType, value); }
static getName(value) { return Enum.getName(enumType, value); }

@@ -26,5 +42,5 @@ static getNames() { return Enum.getNames(enumType); }

for (let prop in typeDef) {
let type = typeof typeDef[prop];
if (type == 'boolean' || type == 'number' || type == 'string') enumType[prop] = typeDef[prop];
for (let [key, value] of Object.entries(typeDef)) {
let type = typeof value;
if (type == 'boolean' || type == 'number' || type == 'string') enumType[key] = value;
}

@@ -36,5 +52,16 @@

/**
* Returns an indication whether a constant with a specified value exists in the specified enumeration.
* Returns the specified value if it exists in the specified enumeration, otherwise returns the given default value.
* @param {object} enumType An enumerated type.
* @param {*} value The value of a constant in the specified enumeration.
* @param {*} [defaultValue] The default value to return if the specified constant does not exist.
* @return {*} The specified enumerated constant, or the default value if no such constant is found.
*/
static coerce(enumType, value, defaultValue = null) {
return Enum.isDefined(enumType, value) ? value : defaultValue;
}
/**
* Gets an indication whether a constant with a specified value exists in the specified enumeration.
* @param {object} enumType An enumerated type.
* @param {*} value The value of a constant in the specified enumeration.
* @return {boolean} `true` if a constant in the specified enumeration has the specified value, otherwise `false`.

@@ -47,14 +74,33 @@ */

/**
* Retrieves the name of the constant in the specified enumeration that has the specified value.
* Gets an array of the `[name, value]` pairs of the constants in the specified enumeration.
* @param {object} enumType An enumerated type.
* @return {Array} An array that contains the `[name, value]` pairs of the constants in the specified enumeration.
*/
static getEntries(enumType) {
return Object.entries(enumType);
}
/**
* Gets the zero-based position of the constant in the specified enumeration that has the specified value.
* @param {object} enumType An enumerated type.
* @param {*} value The value of a constant in the specified enumeration.
* @return {string} A string containing the name of the enumerated constant that has the specified value, or an empty string if no such constant is found.
* @return {number} The zero-based position of the constant that has the specified value, or `-1` if no such constant is found.
*/
static getIndex(enumType, value) {
return Enum.getValues(enumType).indexOf(value);
}
/**
* Gets the name of the constant in the specified enumeration that has the specified value.
* @param {object} enumType An enumerated type.
* @param {*} value The value of a constant in the specified enumeration.
* @return {string} A string containing the name of the constant that has the specified value, or an empty string if no such constant is found.
*/
static getName(enumType, value) {
let index = Enum.getValues(enumType).indexOf(value);
return index < 0 ? '' : Enum.getNames(enumType)[index];
let index = Enum.getIndex(enumType, value);
return index >= 0 ? Enum.getNames(enumType)[index] : '';
}
/**
* Retrieves an array of the names of the constants in the specified enumeration.
* Gets an array of the names of the constants in the specified enumeration.
* @param {object} enumType An enumerated type.

@@ -68,3 +114,3 @@ * @return {string[]} An array that contains the names of the constants in the specified enumeration.

/**
* Retrieves an array of the values of the constants in the specified enumeration.
* Gets an array of the values of the constants in the specified enumeration.
* @param {object} enumType An enumerated type.

@@ -71,0 +117,0 @@ * @return {Array} An array that contains the values of the constants in the specified enumeration.

20

package.json

@@ -10,19 +10,19 @@ {

"repository": "cedx/enum.js",
"version": "2.4.0",
"version": "3.0.0",
"devDependencies": {
"@cedx/coveralls": "^2.0.0",
"@cedx/gulp-david": "^8.0.0",
"chai": "^4.1.0",
"@cedx/coveralls": "^3.1.0",
"@cedx/gulp-david": "^9.0.0",
"chai": "^4.1.1",
"del": "^3.0.0",
"esdoc": "^0.5.2",
"esdoc": "^1.0.1",
"esdoc-node": "^1.0.2",
"estraverse": "^4.2.0",
"esdoc-standard-plugin": "^1.0.0",
"gulp": "^3.9.1",
"gulp-eslint": "^4.0.0",
"mocha": "^3.4.2",
"nsp": "^2.6.3",
"mocha": "^3.5.0",
"nsp": "^2.7.0",
"nyc": "^11.1.0"
},
"engines": {
"node": ">=8.0.0"
"node": ">=8.3.0"
},

@@ -37,5 +37,5 @@ "keywords": [

"scripts": {
"coverage": "coveralls --file=var/lcov.info",
"coverage": "coveralls var/lcov.info",
"test": "nyc --report-dir=var --reporter=lcovonly mocha --recursive"
}
}
# Enums for JS
![Runtime](https://img.shields.io/badge/node-%3E%3D8.0-brightgreen.svg) ![Release](https://img.shields.io/npm/v/@cedx/enum.svg) ![License](https://img.shields.io/npm/l/@cedx/enum.svg) ![Downloads](https://img.shields.io/npm/dt/@cedx/enum.svg) ![Dependencies](https://david-dm.org/cedx/enum.js.svg) ![Coverage](https://coveralls.io/repos/github/cedx/enum.js/badge.svg) ![Build](https://travis-ci.org/cedx/enum.js.svg)
![Runtime](https://img.shields.io/badge/node-%3E%3D8.3-brightgreen.svg) ![Release](https://img.shields.io/npm/v/@cedx/enum.svg) ![License](https://img.shields.io/npm/l/@cedx/enum.svg) ![Downloads](https://img.shields.io/npm/dt/@cedx/enum.svg) ![Dependencies](https://david-dm.org/cedx/enum.js.svg) ![Coverage](https://coveralls.io/repos/github/cedx/enum.js/badge.svg) ![Build](https://travis-ci.org/cedx/enum.js.svg)

@@ -10,3 +10,3 @@ Yet another implementation of enumerated types for [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript).

The latest [Node.js](https://nodejs.org) and [npm](https://www.npmjs.com) versions.
If you plan to play with the sources, you will also need the latest [Gulp.js](http://gulpjs.com) version.
If you plan to play with the sources, you will also need the latest [Gulp](http://gulpjs.com) version.

@@ -23,3 +23,3 @@ ## Installing via [npm](https://www.npmjs.com)

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

@@ -52,11 +52,29 @@ ```javascript

```javascript
DayOfWeek.isDefined(DayOfWeek.TUESDAY); // true
DayOfWeek.isDefined('Foo'); // false
DayOfWeek.isDefined(DayOfWeek.SUNDAY); // true
DayOfWeek.isDefined('foo'); // false
```
Ensure that a value is defined among the enumerated type:
```javascript
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:
```javascript
DayOfWeek.getIndex(DayOfWeek.WEDNESDAY); // 3
DayOfWeek.getIndex('foo'); // -1
```
Get the name associated to an enumerated value:
```javascript
DayOfWeek.getName(DayOfWeek.TUESDAY); // "TUESDAY"
DayOfWeek.getName('Bar'); // "" (empty)
DayOfWeek.getName(DayOfWeek.THURSDAY); // "THURSDAY"
DayOfWeek.getName('foo'); // "" (empty)
```

@@ -67,2 +85,5 @@

```javascript
DayOfWeek.getEntries();
// [["SUNDAY", 0], ["MONDAY", 1], ["TUESDAY", 2], ["WEDNESDAY", 3], ["THURSDAY", 4], ["FRIDAY", 5], ["SATURDAY", 6]]
DayOfWeek.getNames();

@@ -69,0 +90,0 @@ // ["SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"]

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