Skip to content

Commit

Permalink
Merge pull request #20 from hckrnews/feature/enum-name
Browse files Browse the repository at this point in the history
Add docblocks
  • Loading branch information
w3nl authored Jan 25, 2022
2 parents 0caff5f + 9e169cc commit 14b2daf
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 21 deletions.
40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
135 changes: 135 additions & 0 deletions src/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -41,89 +71,194 @@ 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)
.map(([key, value]) => [value, key])
)
}

/**
* 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))
}
Expand Down

0 comments on commit 14b2daf

Please sign in to comment.