@cedx/enum
Advanced tools
Comparing version 6.0.0 to 6.1.0
Cédric Belin <cedric@belin.io> |
(function (exports) { | ||
'use strict'; | ||
'use strict'; | ||
/** | ||
* 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); | ||
/** A symbol indicating that an object is an enumeration. */ | ||
const isEnum = Symbol('Enum'); | ||
/** Provides helper methods for enumerations. */ | ||
class Enum { | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise throws an exception. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @return The specified enumerated constant. | ||
* @throws [[TypeError]] No such constant was found. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static assert(enumType, value) { | ||
if (Enum.isDefined(enumType, 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 enumType An enumerated type. | ||
* @param value The value to coerce. | ||
* @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. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static coerce(enumType, value, defaultValue) { | ||
return Enum.isDefined(enumType, value) ? value : defaultValue; | ||
} | ||
/** | ||
* Creates an enumeration from the specified type definition. | ||
* @param typeDef An object defining the shape of the enumerated type. | ||
* @return The newly created enumeration. | ||
* @typeparam T The type of the enumerated values. | ||
*/ | ||
static create(typeDef) { | ||
const descriptor = { configurable: false, enumerable: false, writable: false }; | ||
const enumType = {}; | ||
Reflect.defineProperty(enumType, isEnum, { ...descriptor, value: true }); | ||
const scalarTypes = ['boolean', 'number', 'string']; | ||
for (const [name, value] of Object.entries(typeDef)) | ||
if (scalarTypes.includes(typeof value)) | ||
Reflect.defineProperty(enumType, name, { ...descriptor, enumerable: true, value }); | ||
const methods = ['assert', 'coerce', 'entries', 'getIndex', 'getName', 'isDefined', 'names', 'values']; | ||
for (const name of methods) { | ||
const method = Reflect.get(Enum, name).bind(enumType, enumType); | ||
Reflect.defineProperty(enumType, name, { ...descriptor, value: method }); | ||
} | ||
return Object.freeze(enumType); | ||
} | ||
/** | ||
* Gets an array of the `[name, value]` pairs of the constants in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @return An array that contains the `[name, value]` pairs of the constants in the specified enumeration. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static entries(enumType) { | ||
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType) | ||
? Object.entries(enumType) | ||
: Enum.names(enumType).map(name => [name, Reflect.get(enumType, name)]); | ||
} | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @return The zero-based position of the constant that has the specified value, or `-1` if no such constant is found. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static getIndex(enumType, value) { | ||
return Enum.values(enumType).indexOf(value); | ||
} | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @return A string containing the name of the constant that has the specified value, or an empty string if no such constant is found. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static getName(enumType, value) { | ||
const index = Enum.getIndex(enumType, value); | ||
return index >= 0 ? Enum.names(enumType)[index] : ''; | ||
} | ||
/** | ||
* Gets an indication whether a constant with a specified value exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @return `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static isDefined(enumType, value) { | ||
return Enum.values(enumType).includes(value); | ||
} | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @return An array that contains the names of the constants in the specified enumeration. | ||
*/ | ||
static names(enumType) { | ||
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType) | ||
? Object.keys(enumType) | ||
: Object.values(enumType).filter(value => typeof value == 'string'); | ||
} | ||
/** | ||
* Gets an array of the values of the constants in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @return An array that contains the values of the constants in the specified enumeration. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static values(enumType) { | ||
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType) | ||
? Object.values(enumType) | ||
: Object.values(enumType).filter(value => typeof value == 'number'); | ||
} | ||
/** | ||
* Returns a value indicating whether the specified enumeration is a TypeScript one. | ||
* @param enumType An enumerated type. | ||
* @return `true` if the specified enumeration is a TypeScript one, otherwise `false`. | ||
*/ | ||
static _hasEnumSymbol(enumType) { | ||
return Reflect.has(enumType, isEnum) && (Reflect.get(enumType, isEnum) === true); | ||
} | ||
/** | ||
* Returns a value indicating whether the specified enumeration is a string one. | ||
* @param enumType An enumerated type. | ||
* @return `true` if the specified enumeration is a string one, otherwise `false`. | ||
*/ | ||
static _isStringEnum(enumType) { | ||
return Object.values(enumType).every(value => typeof value == 'string'); | ||
} | ||
} | ||
/** | ||
* 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}`); | ||
} | ||
exports.Enum = Enum; | ||
/** | ||
* 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))["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||{}); | ||
(function(a){'use strict';const b=Symbol("Enum");class c{static assert(a,b){if(c.isDefined(a,b))return b;throw new TypeError(`Invalid enumerated value: ${b}`)}static coerce(a,b,d){return c.isDefined(a,b)?b:d}static create(a){const d={configurable:!1,enumerable:!1,writable:!1},e={};Reflect.defineProperty(e,b,{...d,value:!0});const f=["boolean","number","string"];for(const[b,c]of Object.entries(a))f.includes(typeof c)&&Reflect.defineProperty(e,b,{...d,enumerable:!0,value:c});const g=["assert","coerce","entries","getIndex","getName","isDefined","names","values"];for(const b of g){const a=Reflect.get(c,b).bind(e,e);Reflect.defineProperty(e,b,{...d,value:a})}return Object.freeze(e)}static entries(a){return c._hasEnumSymbol(a)||c._isStringEnum(a)?Object.entries(a):c.names(a).map(b=>[b,Reflect.get(a,b)])}static getIndex(a,b){return c.values(a).indexOf(b)}static getName(a,b){const d=c.getIndex(a,b);return 0<=d?c.names(a)[d]:""}static isDefined(a,b){return c.values(a).includes(b)}static names(a){return c._hasEnumSymbol(a)||c._isStringEnum(a)?Object.keys(a):Object.values(a).filter(a=>"string"==typeof a)}static values(a){return c._hasEnumSymbol(a)||c._isStringEnum(a)?Object.values(a):Object.values(a).filter(a=>"number"==typeof a)}static _hasEnumSymbol(a){return Reflect.has(a,b)&&!0===Reflect.get(a,b)}static _isStringEnum(a){return Object.values(a).every(a=>"string"==typeof a)}}a.Enum=c})(this.window=this.window||{}); |
# Changelog | ||
## Version [6.1.0](https://github.com/cedx/enum.js/compare/v6.0.0...v6.1.0) | ||
- Added support for [TypeScript enums](https://www.typescriptlang.org/docs/handbook/enums.html). | ||
- Due to strong user demand, restored the [TypeScript](https://www.typescriptlang.org) source code. | ||
- Raised the [Node.js](https://nodejs.org) constraint. | ||
- Replaced the [JSDoc](https://jsdoc.app) documentation generator by [TypeDoc](https://typedoc.org). | ||
## Version [6.0.0](https://github.com/cedx/enum.js/compare/v5.5.0...v6.0.0) | ||
@@ -7,4 +13,4 @@ - Breaking change: dropped support for [CommonJS modules](https://nodejs.org/api/modules.html). | ||
- 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). | ||
- Replaced the [TypeDoc](https://typedoc.org) documentation generator by [JSDoc](https://jsdoc.app). | ||
- Replaced the [TSLint](https://palantir.github.io/tslint) static analyzer by [ESLint](https://eslint.org). | ||
- Updated the package dependencies. | ||
@@ -18,3 +24,3 @@ | ||
- Added support for [ECMAScript modules](https://nodejs.org/api/esm.html). | ||
- Replaced [Webpack](https://webpack.js.org) bundler by [Rollup](https://rollupjs.org) and [Babel Minify](https://github.com/babel/minify). | ||
- Replaced the [Webpack](https://webpack.js.org) bundler by [Rollup](https://rollupjs.org) and [Babel Minify](https://github.com/babel/minify). | ||
- Updated the package dependencies. | ||
@@ -30,3 +36,3 @@ | ||
## Version [5.1.1](https://github.com/cedx/enum.js/compare/v5.1.0...v5.1.1) | ||
- Fixed the [GitHub issue #2](https://github.com/cedx/enum.js/issues/2): relaxed the engine constraint. | ||
- Fixed the [issue #2](https://github.com/cedx/enum.js/issues/2): relaxed the engine constraint. | ||
@@ -40,4 +46,4 @@ ## Version [5.1.0](https://github.com/cedx/enum.js/compare/v5.0.0...v5.1.0) | ||
- Ported the unit tests to classes with experimental decorators. | ||
- Replaced [ESDoc](https://esdoc.org) documentation generator by [TypeDoc](https://typedoc.org). | ||
- Replaced [ESLint](https://eslint.org) static analyzer by [TSLint](https://palantir.github.io/tslint). | ||
- Replaced the [ESDoc](https://esdoc.org) documentation generator by [TypeDoc](https://typedoc.org). | ||
- Replaced the [ESLint](https://eslint.org) static analyzer by [TSLint](https://palantir.github.io/tslint). | ||
- Updated the package dependencies. | ||
@@ -44,0 +50,0 @@ |
@@ -0,0 +0,0 @@ # See also |
@@ -5,5 +5,8 @@ # Enums <small>for JS</small> | ||
## Yet another implementation of enumerated types | ||
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 factory method to ease working with the values of an object literal representing an enumerated type. | ||
This implementation provides an `Enum` class that gives a set of static methods to ease working with the values of an object literal representing an enumerated type. | ||
For [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) users, this class provides a factory method for creating concrete enumerated types. | ||
For [TypeScript](https://www.typescriptlang.org) users, this class provides helper methods for querying the members of an [enum](https://www.typescriptlang.org/docs/handbook/enums.html). | ||
## Quick start | ||
@@ -10,0 +13,0 @@ Install the latest version of **Enums for JS** with [npm](https://www.npmjs.com): |
@@ -8,3 +8,3 @@ # Installation | ||
!!! warning | ||
Enums for JS requires Node.js >= **12.4.0**. | ||
Enums for JS requires Node.js >= **12.7.0**. | ||
@@ -15,6 +15,6 @@ You can verify if you're already good to go with the following commands: | ||
node --version | ||
# v12.4.0 | ||
# v12.7.0 | ||
npm --version | ||
# 6.9.0 | ||
# 6.10.0 | ||
``` | ||
@@ -36,5 +36,5 @@ | ||
### 2. Import it | ||
Now in your [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) code, you can use: | ||
Now in your [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) or [TypeScript](https://www.typescriptlang.org) code, you can use: | ||
```js | ||
```ts | ||
import {Enum} from '@cedx/enum'; | ||
@@ -44,3 +44,3 @@ ``` | ||
### 3. Use it | ||
See the [usage information](usage.md). | ||
See the [usage information](usage/javascript.md). | ||
@@ -47,0 +47,0 @@ ## Installing from a content delivery network |
222
lib/enum.js
@@ -1,97 +0,129 @@ | ||
/** | ||
* Defines the shape of an enumerated value. | ||
* @typedef {boolean|number|string} EnumValue | ||
*/ | ||
/** | ||
* Provides helper methods for enumerations. | ||
* @abstract | ||
*/ | ||
/** A symbol indicating that an object is an enumeration. */ | ||
const isEnum = Symbol('Enum'); | ||
/** Provides helper methods for enumerations. */ | ||
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); | ||
} | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise throws an exception. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @return The specified enumerated constant. | ||
* @throws [[TypeError]] No such constant was found. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static assert(enumType, value) { | ||
if (Enum.isDefined(enumType, 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 enumType An enumerated type. | ||
* @param value The value to coerce. | ||
* @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. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static coerce(enumType, value, defaultValue) { | ||
return Enum.isDefined(enumType, value) ? value : defaultValue; | ||
} | ||
/** | ||
* Creates an enumeration from the specified type definition. | ||
* @param typeDef An object defining the shape of the enumerated type. | ||
* @return The newly created enumeration. | ||
* @typeparam T The type of the enumerated values. | ||
*/ | ||
static create(typeDef) { | ||
const descriptor = { configurable: false, enumerable: false, writable: false }; | ||
const enumType = {}; | ||
Reflect.defineProperty(enumType, isEnum, { ...descriptor, value: true }); | ||
const scalarTypes = ['boolean', 'number', 'string']; | ||
for (const [name, value] of Object.entries(typeDef)) | ||
if (scalarTypes.includes(typeof value)) | ||
Reflect.defineProperty(enumType, name, { ...descriptor, enumerable: true, value }); | ||
const methods = ['assert', 'coerce', 'entries', 'getIndex', 'getName', 'isDefined', 'names', 'values']; | ||
for (const name of methods) { | ||
const method = Reflect.get(Enum, name).bind(enumType, enumType); | ||
Reflect.defineProperty(enumType, name, { ...descriptor, value: method }); | ||
} | ||
return Object.freeze(enumType); | ||
} | ||
/** | ||
* Gets an array of the `[name, value]` pairs of the constants in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @return An array that contains the `[name, value]` pairs of the constants in the specified enumeration. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static entries(enumType) { | ||
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType) | ||
? Object.entries(enumType) | ||
: Enum.names(enumType).map(name => [name, Reflect.get(enumType, name)]); | ||
} | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @return The zero-based position of the constant that has the specified value, or `-1` if no such constant is found. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static getIndex(enumType, value) { | ||
return Enum.values(enumType).indexOf(value); | ||
} | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @return A string containing the name of the constant that has the specified value, or an empty string if no such constant is found. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static getName(enumType, value) { | ||
const index = Enum.getIndex(enumType, value); | ||
return index >= 0 ? Enum.names(enumType)[index] : ''; | ||
} | ||
/** | ||
* Gets an indication whether a constant with a specified value exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @return `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static isDefined(enumType, value) { | ||
return Enum.values(enumType).includes(value); | ||
} | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @return An array that contains the names of the constants in the specified enumeration. | ||
*/ | ||
static names(enumType) { | ||
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType) | ||
? Object.keys(enumType) | ||
: Object.values(enumType).filter(value => typeof value == 'string'); | ||
} | ||
/** | ||
* Gets an array of the values of the constants in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @return An array that contains the values of the constants in the specified enumeration. | ||
* @typeparam T The type of the specified enumeration. | ||
*/ | ||
static values(enumType) { | ||
return Enum._hasEnumSymbol(enumType) || Enum._isStringEnum(enumType) | ||
? Object.values(enumType) | ||
: Object.values(enumType).filter(value => typeof value == 'number'); | ||
} | ||
/** | ||
* Returns a value indicating whether the specified enumeration is a TypeScript one. | ||
* @param enumType An enumerated type. | ||
* @return `true` if the specified enumeration is a TypeScript one, otherwise `false`. | ||
*/ | ||
static _hasEnumSymbol(enumType) { | ||
return Reflect.has(enumType, isEnum) && (Reflect.get(enumType, isEnum) === true); | ||
} | ||
/** | ||
* Returns a value indicating whether the specified enumeration is a string one. | ||
* @param enumType An enumerated type. | ||
* @return `true` if the specified enumeration is a string one, otherwise `false`. | ||
*/ | ||
static _isStringEnum(enumType) { | ||
return Object.values(enumType).every(value => typeof value == 'string'); | ||
} | ||
} |
export * from './enum.js'; |
@@ -0,0 +0,0 @@ # MIT License |
@@ -10,3 +10,4 @@ { | ||
"type": "module", | ||
"version": "6.0.0", | ||
"types": "lib/index.d.ts", | ||
"version": "6.1.0", | ||
"author": { | ||
@@ -18,23 +19,27 @@ "email": "cedric@belin.io", | ||
"devDependencies": { | ||
"@cedx/coveralls": "^9.0.0", | ||
"@cedx/coveralls": "^9.1.0", | ||
"@types/chai": "^4.1.7", | ||
"@types/gulp": "^4.0.6", | ||
"@types/gulp-replace": "^0.0.31", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^12.6.9", | ||
"@typescript-eslint/eslint-plugin": "^1.13.0", | ||
"@typescript-eslint/parser": "^1.13.0", | ||
"babel-minify": "^0.5.0", | ||
"chai": "^4.2.0", | ||
"del": "^4.1.1", | ||
"eslint": "^5.16.0", | ||
"esm": "^3.2.25", | ||
"del": "^5.0.0", | ||
"eslint": "^6.1.0", | ||
"gulp": "^4.0.2", | ||
"jsdoc": "^3.6.2", | ||
"karma": "^4.1.0", | ||
"karma-firefox-launcher": "^1.1.0", | ||
"karma-mocha": "^1.3.0", | ||
"karma-rollup-preprocessor": "^7.0.0", | ||
"mocha": "^6.1.4", | ||
"gulp-replace": "^1.0.0", | ||
"mocha": "^6.2.0", | ||
"nyc": "^14.1.1", | ||
"rollup": "^1.15.0", | ||
"rollup-plugin-commonjs": "^10.0.0", | ||
"rollup-plugin-node-resolve": "^5.0.1" | ||
"rollup": "^1.18.0", | ||
"source-map-support": "^0.5.13", | ||
"ts-node": "^8.3.0", | ||
"typedoc": "^0.15.0", | ||
"typescript": "^3.5.3" | ||
}, | ||
"engines": { | ||
"node": ">=12.4.0", | ||
"npm": ">=6.9.0" | ||
"node": ">=12.7.0", | ||
"npm": ">=6.10.0" | ||
}, | ||
@@ -51,3 +56,3 @@ "keywords": [ | ||
"coverage": "gulp coverage", | ||
"prepack": "gulp build", | ||
"prepack": "gulp", | ||
"start": "gulp watch", | ||
@@ -54,0 +59,0 @@ "test": "gulp test" |
# Enums for JS | ||
![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). | ||
Yet another implementation of enumerated types for [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) and [TypeScript](https://www.typescriptlang.org). | ||
@@ -6,0 +6,0 @@ > This library is packaged as [ECMAScript modules](https://nodejs.org/api/esm.html). |
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
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
50487
19
451
21