diff --git a/package-lock.json b/package-lock.json index 801ddaf..f41afe1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@hckrnews/enum", - "version": "1.5.0", + "version": "1.5.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@hckrnews/enum", - "version": "1.5.0", + "version": "1.5.1", "license": "LGPL-3.0", "devDependencies": { "@babel/core": "^7.12.10", @@ -2576,9 +2576,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "17.0.10", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.10.tgz", - "integrity": "sha512-S/3xB4KzyFxYGCppyDt68yzBU9ysL88lSdIah4D6cptdcltc4NCPCAMc0+PCpg/lLIyC7IPvj2Z52OJWeIUkog==", + "version": "17.0.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.12.tgz", + "integrity": "sha512-4YpbAsnJXWYK/fpTVFlMIcUIho2AYCi4wg5aNPrG1ng7fn/1/RZfCIpRCiBX+12RVa34RluilnvCqD+g3KiSiA==", "dev": true }, "node_modules/@types/prettier": { @@ -3733,9 +3733,9 @@ } }, "node_modules/electron-to-chromium": { - "version": "1.4.51", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.51.tgz", - "integrity": "sha512-JNEmcYl3mk1tGQmy0EvL5eik/CKSBuzAyGP0QFdG6LIgxQe3II0BL1m2zKc2MZMf3uGqHWE1TFddJML0RpjSHQ==", + "version": "1.4.53", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.53.tgz", + "integrity": "sha512-rFveSKQczlcav+H3zkKqykU6ANseFwXwkl855jOIap5/0gnEcuIhv2ecz6aoTrXavF6I/CEBeRnBnkB51k06ew==", "dev": true }, "node_modules/emittery": { @@ -8168,9 +8168,9 @@ } }, "node_modules/pirates": { - "version": "4.0.4", - "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.4.tgz", - "integrity": "sha512-ZIrVPH+A52Dw84R0L3/VS9Op04PuQ2SEoJL6bkshmiTic/HldyW9Tf7oH5mhJZBK7NmDx27vSMrYEXPXclpDKw==", + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.5.tgz", + "integrity": "sha512-8V9+HQPupnaXMA23c5hvl69zXvTwTzyAYasnkb0Tts4XvO4CliqONMOnvlq26rkhLC3nWDFBJf73LU1e1VZLaQ==", "dev": true, "engines": { "node": ">= 6" @@ -12641,9 +12641,9 @@ "dev": true }, "@types/node": { - "version": "17.0.10", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.10.tgz", - "integrity": "sha512-S/3xB4KzyFxYGCppyDt68yzBU9ysL88lSdIah4D6cptdcltc4NCPCAMc0+PCpg/lLIyC7IPvj2Z52OJWeIUkog==", + "version": "17.0.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.12.tgz", + "integrity": "sha512-4YpbAsnJXWYK/fpTVFlMIcUIho2AYCi4wg5aNPrG1ng7fn/1/RZfCIpRCiBX+12RVa34RluilnvCqD+g3KiSiA==", "dev": true }, "@types/prettier": { @@ -13546,9 +13546,9 @@ } }, "electron-to-chromium": { - "version": "1.4.51", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.51.tgz", - "integrity": "sha512-JNEmcYl3mk1tGQmy0EvL5eik/CKSBuzAyGP0QFdG6LIgxQe3II0BL1m2zKc2MZMf3uGqHWE1TFddJML0RpjSHQ==", + "version": "1.4.53", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.53.tgz", + "integrity": "sha512-rFveSKQczlcav+H3zkKqykU6ANseFwXwkl855jOIap5/0gnEcuIhv2ecz6aoTrXavF6I/CEBeRnBnkB51k06ew==", "dev": true }, "emittery": { @@ -16876,9 +16876,9 @@ "dev": true }, "pirates": { - "version": "4.0.4", - "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.4.tgz", - "integrity": "sha512-ZIrVPH+A52Dw84R0L3/VS9Op04PuQ2SEoJL6bkshmiTic/HldyW9Tf7oH5mhJZBK7NmDx27vSMrYEXPXclpDKw==", + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.5.tgz", + "integrity": "sha512-8V9+HQPupnaXMA23c5hvl69zXvTwTzyAYasnkb0Tts4XvO4CliqONMOnvlq26rkhLC3nWDFBJf73LU1e1VZLaQ==", "dev": true }, "pkg-conf": { diff --git a/package.json b/package.json index 0509119..b747746 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@hckrnews/enum", "description": "Create vanilla JavaScript enums", - "version": "1.5.0", + "version": "1.5.1", "author": { "name": "Pieter Wigboldus", "url": "https://hckr.news/" diff --git a/src/enum.js b/src/enum.js index 40abfe4..b6a568d 100644 --- a/src/enum.js +++ b/src/enum.js @@ -5,18 +5,40 @@ export default class Enum { this.output = 'key' } + /** + * @return {string} + */ get name () { return this.constructor.name } + /** + * @return {array} + */ static get options () { return Object.fromEntries(Object.entries(this)) } + /** + * Create an enum result by key + * + * @param {string} key + * @param {object} options + * + * @return {Enum} + */ static create (key, options = {}) { return this.fromKey(key, options) } + /** + * Get an enum result by key + * + * @param {string} key + * @param {object} options + * + * @return {Enum} + */ static fromKey (key, options = {}) { const newEnum = new this() if (!newEnum.isValidKey(key)) { @@ -29,6 +51,14 @@ export default class Enum { return newEnum } + /** + * Get an enum result by value + * + * @param {any} value + * @param {object} options + * + * @return {Enum} + */ static fromValue (value, options = {}) { const newEnum = new this() if (!newEnum.isValidValue(value)) { @@ -41,16 +71,31 @@ export default class Enum { return newEnum } + /** + * Set the key and value of the enum + * + * @param {{ key: string, value: any }} keyValue + */ setKeyValue ({ key, value }) { this.setValues() this.setKey(key) this.setValue(value) } + /** + * Set the options of the enum + * + * @param {object} options + */ setOptions (options) { this.output = options?.output || this.output } + /** + * Get the options inverted + * + * @return {object} + */ get invertedOptions () { return Object.fromEntries( Object.entries(this.constructor) @@ -58,72 +103,162 @@ export default class Enum { ) } + /** + * Set the enum key + * + * @param {string} key + */ setKey (key) { this.key = key } + /** + * Set the enum value + * + * @param {any} value + */ setValue (value) { this.value = value } + /** + * Set the values + */ setValues () { Object.entries(this.constructor).forEach(([key, value]) => { this[key] = value }) } + /** + * Check if the key is known in the enum + * + * @param {string} key + * + * @return {boolean} + */ isValidKey (key) { return Object.hasOwnProperty.call(this.constructor, key) && Object.propertyIsEnumerable.call(this.constructor, key) } + /** + * Check if the value is known in the enum + * + * @param {any} value + * + * @return {boolean} + */ isValidValue (value) { return this.values.includes(value) } + /** + * Check if the value is the current enum value + * + * @param {any} value + * + * @return {boolean} + */ is (value) { return this.value === value } + /** + * Check if the enum has these values + * + * @param {array} values + * + * @return {boolean} + */ in (values) { return values.includes(this.value) } + /** + * Check if the enum has this key + * + * @param {string} key + * + * @return {boolean} + */ static hasKey (key) { const newEnum = new this() return newEnum.isValidKey(key) } + /** + * Check if the enum has this value + * + * @param {any} value + * + * @return {boolean} + */ static hasValue (value) { const newEnum = new this() return newEnum.isValidValue(value) } + /** + * Get all the enum keys + * + * @return {array} + */ get keys () { return Object.keys(this.constructor) } + /** + * Get all the enum values + * + * @return {array} + */ get values () { return Object.values(this.constructor) } + /** + * Get the number of enum items + * + * @return {number} + */ get length () { return Object.keys(this.constructor).length } + /** + * Get the enum value + * + * @return {any} + */ valueOf () { return this.value } + /** + * Get the enum key (or value) as a string + * + * @return {string} + */ toString () { return this[this.output].toString() } + /** + * Get the enum key (or value) + * + * @return {any} + */ toJSON () { return this[this.output] } + /** + * Get the enum items as an object + * + * @return {object} + */ static toJSON () { return Object.fromEntries(Object.entries(this)) }