@cedx/enum
Advanced tools
Comparing version 5.5.0 to 6.0.0
(function (exports) { | ||
'use strict'; | ||
'use strict'; | ||
class Enum { | ||
static create(typeDef) { | ||
const enumType = new class extends Enum { | ||
}; | ||
for (const [key, value] of Object.entries(typeDef)) | ||
if (typeDef.hasOwnProperty(key) && ['boolean', 'number', 'string'].includes(typeof value)) | ||
enumType[key] = value; | ||
return Object.freeze(enumType); | ||
} | ||
assert(value) { | ||
if (this.isDefined(value)) | ||
return value; | ||
throw new TypeError(`Invalid enumerated value: ${value}`); | ||
} | ||
coerce(value, defaultValue) { | ||
return this.isDefined(value) ? value : defaultValue; | ||
} | ||
entries() { | ||
return Object.entries(this); | ||
} | ||
isDefined(value) { | ||
return this.values().includes(value); | ||
} | ||
getIndex(value) { | ||
return this.values().indexOf(value); | ||
} | ||
getName(value) { | ||
const index = this.getIndex(value); | ||
return index >= 0 ? this.names()[index] : ''; | ||
} | ||
names() { | ||
return Object.keys(this); | ||
} | ||
values() { | ||
return Object.values(this); | ||
} | ||
/** | ||
* Defines the shape of an enumerated value. | ||
* @typedef {boolean|number|string} EnumValue | ||
*/ | ||
/** | ||
* Provides helper methods for enumerations. | ||
* @abstract | ||
*/ | ||
class Enum { | ||
/** | ||
* Creates an enumeration from the specified type definition. | ||
* @param {Object<string, EnumValue>} typeDef An object defining the shape of the enumerated type. | ||
* @return {Enum} The newly created enumeration. | ||
*/ | ||
static create(typeDef) { | ||
const enumType = new class extends Enum {}; | ||
for (const [key, value] of Object.entries(typeDef)) | ||
if (['boolean', 'number', 'string'].includes(typeof value)) enumType[key] = value; | ||
return Object.freeze(enumType); | ||
} | ||
exports.Enum = Enum; | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise throws an exception. | ||
* @param {EnumValue} value The value of a constant in the specified enumeration. | ||
* @return {EnumValue} The specified enumerated constant. | ||
* @throws {TypeError} No such constant was found. | ||
*/ | ||
assert(value) { | ||
if (this.isDefined(value)) return value; | ||
throw new TypeError(`Invalid enumerated value: ${value}`); | ||
} | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise returns the given default value. | ||
* @param {EnumValue} value The value of a constant in the specified enumeration. | ||
* @param {?EnumValue} defaultValue The default value to return if the specified constant does not exist. | ||
* @return {?EnumValue} The specified enumerated constant, or the default value if no such constant is found. | ||
*/ | ||
coerce(value, defaultValue = null) { | ||
return this.isDefined(value) ? value : defaultValue; | ||
} | ||
/** | ||
* Gets an array of the `[name, value]` pairs of the constants in the specified enumeration. | ||
* @return {Array[]} An array that contains the `[name, value]` pairs of the constants in the specified enumeration. | ||
*/ | ||
entries() { | ||
return Object.entries(this); | ||
} | ||
/** | ||
* Gets an indication whether a constant with a specified value exists in the specified enumeration. | ||
* @param {EnumValue} 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`. | ||
*/ | ||
isDefined(value) { | ||
return this.values().includes(value); | ||
} | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* @param {EnumValue} value The value of a constant in the specified enumeration. | ||
* @return {number} The zero-based position of the constant that has the specified value, or `-1` if no such constant is found. | ||
*/ | ||
getIndex(value) { | ||
return this.values().indexOf(value); | ||
} | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
* @param {EnumValue} 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. | ||
*/ | ||
getName(value) { | ||
const index = this.getIndex(value); | ||
return index >= 0 ? this.names()[index] : ''; | ||
} | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
* @return {string[]} An array that contains the names of the constants in the specified enumeration. | ||
*/ | ||
names() { | ||
return Object.keys(this); | ||
} | ||
/** | ||
* Gets an array of the values of the constants in the specified enumeration. | ||
* @return {EnumValue[]} An array that contains the values of the constants in the specified enumeration. | ||
*/ | ||
values() { | ||
return Object.values(this); | ||
} | ||
} | ||
exports.Enum = Enum; | ||
}(this.window = this.window || {})); |
@@ -1,1 +0,1 @@ | ||
(function(a){"use strict";class b{static create(a){const c=new class extends b{};for(const[b,d]of Object.entries(a))a.hasOwnProperty(b)&&["boolean","number","string"].includes(typeof d)&&(c[b]=d);return Object.freeze(c)}assert(a){if(this.isDefined(a))return a;throw new TypeError(`Invalid enumerated value: ${a}`)}coerce(a,b){return this.isDefined(a)?a:b}entries(){return Object.entries(this)}isDefined(a){return this.values().includes(a)}getIndex(a){return this.values().indexOf(a)}getName(a){const b=this.getIndex(a);return 0<=b?this.names()[b]:""}names(){return Object.keys(this)}values(){return Object.values(this)}}a.Enum=b})(this.window=this.window||{}); | ||
(function(a){'use strict';class b{static create(a){const c=new class extends b{};for(const[b,d]of Object.entries(a))["boolean","number","string"].includes(typeof d)&&(c[b]=d);return Object.freeze(c)}assert(a){if(this.isDefined(a))return a;throw new TypeError(`Invalid enumerated value: ${a}`)}coerce(a,b=null){return this.isDefined(a)?a:b}entries(){return Object.entries(this)}isDefined(a){return this.values().includes(a)}getIndex(a){return this.values().indexOf(a)}getName(a){const b=this.getIndex(a);return 0<=b?this.names()[b]:""}names(){return Object.keys(this)}values(){return Object.values(this)}}a.Enum=b})(this.window=this.window||{}); |
# Changelog | ||
## Version [6.0.0](https://github.com/cedx/enum.js/compare/v5.5.0...v6.0.0) | ||
- Breaking change: dropped support for [CommonJS modules](https://nodejs.org/api/modules.html). | ||
- Breaking change: raised the required [Node.js](https://nodejs.org) version. | ||
- Breaking change: reverted the source code to [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript). | ||
- Replaced [TypeDoc](https://typedoc.org) documentation generator by [JSDoc](https://jsdoc.app). | ||
- Replaced [TSLint](https://palantir.github.io/tslint) static analyzer by [ESLint](https://eslint.org). | ||
- Updated the package dependencies. | ||
## Version [5.5.0](https://github.com/cedx/enum.js/compare/v5.4.0...v5.5.0) | ||
@@ -4,0 +12,0 @@ - Modified the package layout. |
# Enums <small>for JS</small> | ||
![Runtime](https://img.shields.io/badge/node-%3E%3D10.15-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.com/cedx/enum.js.svg) | ||
![Runtime](https://img.shields.io/node/v/@cedx/enum.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.com/cedx/enum.js.svg) | ||
@@ -15,2 +15,5 @@ ## Yet another implementation of enumerated types | ||
!!! info | ||
This library is packaged as [ECMAScript modules](https://nodejs.org/api/esm.html). | ||
For detailed instructions, see the [installation guide](installation.md). |
@@ -8,3 +8,3 @@ # Installation | ||
!!! warning | ||
Enums for JS requires Node.js >= **10.15.0**. | ||
Enums for JS requires Node.js >= **12.4.0**. | ||
@@ -15,6 +15,6 @@ You can verify if you're already good to go with the following commands: | ||
node --version | ||
# v11.13.0 | ||
# v12.4.0 | ||
npm --version | ||
# 6.7.0 | ||
# 6.9.0 | ||
``` | ||
@@ -36,12 +36,8 @@ | ||
### 2. Import it | ||
Now in your [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) or [TypeScript](https://www.typescriptlang.org) code, you can use: | ||
Now in your [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) code, you can use: | ||
```ts | ||
```js | ||
import {Enum} from '@cedx/enum'; | ||
``` | ||
!!! info | ||
This library is packaged as [CommonJS modules](https://nodejs.org/api/modules.html) (`.js` files) and [ECMAScript modules](https://nodejs.org/api/esm.html) (`.mjs` files). | ||
To consume it in a browser, you must use a dedicated tool chain, like a build system coupled with a bundler. | ||
### 3. Use it | ||
@@ -48,0 +44,0 @@ See the [usage information](usage.md). |
path: blob/master | ||
source: src/enum.ts | ||
source: lib/enum.js | ||
@@ -9,5 +9,8 @@ # Usage | ||
```ts | ||
/** Specifies the day of the week. */ | ||
const DayOfWeek = Enum.create<number>({ | ||
```js | ||
/** | ||
* Specifies the days of the week. | ||
* @enum {number} | ||
*/ | ||
const DayOfWeek = Enum.create({ | ||
sunday: 0, | ||
@@ -26,4 +29,3 @@ monday: 1, | ||
!!! warning | ||
Only scalar values (booleans, numbers, and strings) are retained | ||
when iterating on the enumerable properties of the provided object. | ||
Only scalar values (booleans, numbers, and strings) are retained when iterating on the properties of the provided object. | ||
@@ -35,3 +37,3 @@ This instance has the same values as the provided object, and some additional helper methods. The new object is also freezed to prevent any attempt at modifying its shape. | ||
```ts | ||
```js | ||
DayOfWeek.isDefined(DayOfWeek.sunday); // true | ||
@@ -43,8 +45,8 @@ DayOfWeek.isDefined(123); // false | ||
```ts | ||
```js | ||
DayOfWeek.assert(DayOfWeek.monday); // DayOfWeek.monday | ||
DayOfWeek.assert(123); // (throws TypeError) | ||
DayOfWeek.assert(123); // (throws `TypeError`) | ||
DayOfWeek.coerce(DayOfWeek.monday); // DayOfWeek.monday | ||
DayOfWeek.coerce(123); // undefined | ||
DayOfWeek.coerce(123); // null | ||
DayOfWeek.coerce(123, DayOfWeek.tuesday); // DayOfWeek.tuesday | ||
@@ -55,3 +57,3 @@ ``` | ||
```ts | ||
```js | ||
DayOfWeek.getIndex(DayOfWeek.wednesday); // 3 | ||
@@ -63,3 +65,3 @@ DayOfWeek.getIndex(123); // -1 | ||
```ts | ||
```js | ||
DayOfWeek.getName(DayOfWeek.thursday); // "thursday" | ||
@@ -71,3 +73,3 @@ DayOfWeek.getName(123); // "" (empty) | ||
```ts | ||
```js | ||
DayOfWeek.entries(); | ||
@@ -74,0 +76,0 @@ // [["sunday", 0], ["monday", 1], ["tuesday", 2], ["wednesday", 3], ["thursday", 4], ["friday", 5], ["saturday", 6]] |
136
lib/enum.js
@@ -1,41 +0,97 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class Enum { | ||
static create(typeDef) { | ||
const enumType = new class extends Enum { | ||
}; | ||
for (const [key, value] of Object.entries(typeDef)) | ||
if (typeDef.hasOwnProperty(key) && ['boolean', 'number', 'string'].includes(typeof value)) | ||
enumType[key] = value; | ||
return Object.freeze(enumType); | ||
} | ||
assert(value) { | ||
if (this.isDefined(value)) | ||
return value; | ||
throw new TypeError(`Invalid enumerated value: ${value}`); | ||
} | ||
coerce(value, defaultValue) { | ||
return this.isDefined(value) ? value : defaultValue; | ||
} | ||
entries() { | ||
return Object.entries(this); | ||
} | ||
isDefined(value) { | ||
return this.values().includes(value); | ||
} | ||
getIndex(value) { | ||
return this.values().indexOf(value); | ||
} | ||
getName(value) { | ||
const index = this.getIndex(value); | ||
return index >= 0 ? this.names()[index] : ''; | ||
} | ||
names() { | ||
return Object.keys(this); | ||
} | ||
values() { | ||
return Object.values(this); | ||
} | ||
/** | ||
* Defines the shape of an enumerated value. | ||
* @typedef {boolean|number|string} EnumValue | ||
*/ | ||
/** | ||
* Provides helper methods for enumerations. | ||
* @abstract | ||
*/ | ||
export class Enum { | ||
/** | ||
* Creates an enumeration from the specified type definition. | ||
* @param {Object<string, EnumValue>} typeDef An object defining the shape of the enumerated type. | ||
* @return {Enum} The newly created enumeration. | ||
*/ | ||
static create(typeDef) { | ||
const enumType = new class extends Enum {}; | ||
for (const [key, value] of Object.entries(typeDef)) | ||
if (['boolean', 'number', 'string'].includes(typeof value)) enumType[key] = value; | ||
return Object.freeze(enumType); | ||
} | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise throws an exception. | ||
* @param {EnumValue} value The value of a constant in the specified enumeration. | ||
* @return {EnumValue} The specified enumerated constant. | ||
* @throws {TypeError} No such constant was found. | ||
*/ | ||
assert(value) { | ||
if (this.isDefined(value)) return value; | ||
throw new TypeError(`Invalid enumerated value: ${value}`); | ||
} | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise returns the given default value. | ||
* @param {EnumValue} value The value of a constant in the specified enumeration. | ||
* @param {?EnumValue} defaultValue The default value to return if the specified constant does not exist. | ||
* @return {?EnumValue} The specified enumerated constant, or the default value if no such constant is found. | ||
*/ | ||
coerce(value, defaultValue = null) { | ||
return this.isDefined(value) ? value : defaultValue; | ||
} | ||
/** | ||
* Gets an array of the `[name, value]` pairs of the constants in the specified enumeration. | ||
* @return {Array[]} An array that contains the `[name, value]` pairs of the constants in the specified enumeration. | ||
*/ | ||
entries() { | ||
return Object.entries(this); | ||
} | ||
/** | ||
* Gets an indication whether a constant with a specified value exists in the specified enumeration. | ||
* @param {EnumValue} 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`. | ||
*/ | ||
isDefined(value) { | ||
return this.values().includes(value); | ||
} | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* @param {EnumValue} value The value of a constant in the specified enumeration. | ||
* @return {number} The zero-based position of the constant that has the specified value, or `-1` if no such constant is found. | ||
*/ | ||
getIndex(value) { | ||
return this.values().indexOf(value); | ||
} | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
* @param {EnumValue} 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. | ||
*/ | ||
getName(value) { | ||
const index = this.getIndex(value); | ||
return index >= 0 ? this.names()[index] : ''; | ||
} | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
* @return {string[]} An array that contains the names of the constants in the specified enumeration. | ||
*/ | ||
names() { | ||
return Object.keys(this); | ||
} | ||
/** | ||
* Gets an array of the values of the constants in the specified enumeration. | ||
* @return {EnumValue[]} An array that contains the values of the constants in the specified enumeration. | ||
*/ | ||
values() { | ||
return Object.values(this); | ||
} | ||
} | ||
exports.Enum = Enum; | ||
//# sourceMappingURL=enum.js.map |
@@ -1,7 +0,1 @@ | ||
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./enum")); | ||
//# sourceMappingURL=index.js.map | ||
export * from './enum.js'; |
@@ -6,8 +6,7 @@ { | ||
"license": "MIT", | ||
"main": "lib/index", | ||
"module": "lib/index.mjs", | ||
"main": "lib/index.js", | ||
"name": "@cedx/enum", | ||
"repository": "github:cedx/enum.js", | ||
"types": "lib/index.d.ts", | ||
"version": "5.5.0", | ||
"type": "module", | ||
"version": "6.0.0", | ||
"author": { | ||
@@ -19,30 +18,23 @@ "email": "cedric@belin.io", | ||
"devDependencies": { | ||
"@cedx/coveralls": "^8.6.0", | ||
"@types/chai": "^4.1.7", | ||
"@types/node": "^11.13.4", | ||
"@cedx/coveralls": "^9.0.0", | ||
"babel-minify": "^0.5.0", | ||
"chai": "^4.2.0", | ||
"del": "^4.1.0", | ||
"gulp": "^4.0.0", | ||
"gulp-rename": "^1.4.0", | ||
"http-server": "^0.11.1", | ||
"del": "^4.1.1", | ||
"eslint": "^5.16.0", | ||
"esm": "^3.2.25", | ||
"gulp": "^4.0.2", | ||
"jsdoc": "^3.6.2", | ||
"karma": "^4.1.0", | ||
"karma-coverage": "^1.1.2", | ||
"karma-firefox-launcher": "^1.1.0", | ||
"karma-mocha": "^1.3.0", | ||
"karma-typescript": "^4.0.0", | ||
"mocha": "^6.1.3", | ||
"mocha-typescript": "^1.1.17", | ||
"nyc": "^13.3.0", | ||
"rollup": "^1.10.0", | ||
"rollup-plugin-node-resolve": "^4.2.3", | ||
"source-map-support": "^0.5.12", | ||
"ts-node": "^8.1.0", | ||
"tslint": "^5.16.0", | ||
"typedoc": "^0.14.2", | ||
"typescript": "^3.4.3" | ||
"karma-rollup-preprocessor": "^7.0.0", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.1", | ||
"rollup": "^1.15.0", | ||
"rollup-plugin-commonjs": "^10.0.0", | ||
"rollup-plugin-node-resolve": "^5.0.1" | ||
}, | ||
"engines": { | ||
"node": ">=10.15.0", | ||
"npm": ">=6.4.0" | ||
"node": ">=12.4.0", | ||
"npm": ">=6.9.0" | ||
}, | ||
@@ -52,2 +44,3 @@ "keywords": [ | ||
"enumeration", | ||
"esm", | ||
"javascript", | ||
@@ -59,3 +52,3 @@ "js", | ||
"coverage": "gulp coverage", | ||
"prepare": "gulp build", | ||
"prepack": "gulp build", | ||
"start": "gulp watch", | ||
@@ -62,0 +55,0 @@ "test": "gulp test" |
# Enums for JS | ||
![Runtime](https://img.shields.io/badge/node-%3E%3D10.15-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.com/cedx/enum.js.svg) | ||
![Runtime](https://img.shields.io/node/v/@cedx/enum.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.com/cedx/enum.js.svg) | ||
Yet another implementation of enumerated types for [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript). | ||
> This library is packaged as [ECMAScript modules](https://nodejs.org/api/esm.html). | ||
## Documentation | ||
@@ -7,0 +9,0 @@ - [User guide](https://dev.belin.io/enum.js) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
32817
17
217
19
1
Yes
16
2