diff --git a/README.md b/README.md index 7b0c104..59d15bd 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,13 @@ encoding.in(['TEXT']) // true encoding.in([42]) // false encoding.valueOf() // 42 -encoding.toString() // '42' -encoding.toJSON() // 42 +encoding.toString() // 'test' +encoding.toJSON() // 'test' +JSON.stringify(encoding) // '"test"' + +const encoding = Encoding.create('test') +encoding.key // 'test' +encoding.value // 'TEXT' const encoding = Encoding.fromValue('TEXT') encoding.key // test @@ -56,6 +61,15 @@ Encoding.hasKey('test') // teue Encoding.hasKey('TEXT') // false Encoding.hasValue('test') // teue Encoding.hasValue('TEXT') // false + +Encoding.toJSON() // { test: 'TEXT', another: 42 } +JSON.stringify(Encoding) // '{"test":"TEXT","another":42}' + +const encoding = Encoding.create('test', { output: 'value' }) +encoding.valueOf() // 42 +encoding.toString() // '42' +encoding.toJSON() // 42 +JSON.stringify(encoding) // '42' ``` [npm-url]: https://www.npmjs.com/package/@hckrnews/enum diff --git a/package-lock.json b/package-lock.json index da01ff8..36cdcd7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@hckrnews/enum", - "version": "1.4.0", + "version": "1.4.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@hckrnews/enum", - "version": "1.4.0", + "version": "1.4.1", "license": "LGPL-3.0", "devDependencies": { "@babel/core": "^7.12.10", diff --git a/package.json b/package.json index e46b18b..ab23d66 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@hckrnews/enum", "description": "Create vanilla JavaScript enums", - "version": "1.4.0", + "version": "1.4.1", "author": { "name": "Pieter Wigboldus", "url": "https://hckr.news/" diff --git a/src/__tests__/enum.spec.js b/src/__tests__/enum.spec.js index fec1d53..b2c58a9 100644 --- a/src/__tests__/enum.spec.js +++ b/src/__tests__/enum.spec.js @@ -196,7 +196,7 @@ describe('Test the encoding enum', () => { static 'test3' = 3 } - const example = Example.fromKey('test2') + const example = Example.create('test2') expect(example.key).toEqual('test2') expect(example.value).toEqual(2) expect(example.values).toEqual([1, 2, 3]) @@ -205,6 +205,53 @@ describe('Test the encoding enum', () => { test2: 2, test3: 3 }) + expect(Example.toJSON()).toEqual({ + test1: 1, + test2: 2, + test3: 3 + }) + expect(JSON.stringify(Example)).toEqual('{"test1":1,"test2":2,"test3":3}') + expect(Example.test1).toEqual(1) + expect(Example.test2).toEqual(2) + expect(Example.test3).toEqual(3) + expect(Example.options.test2).toEqual(2) + expect(example.keys).toEqual(['test1', 'test2', 'test3']) + expect(example.test2).toEqual(2) + expect(example.length).toEqual(3) + expect(example.is(Example.test2)).toEqual(true) + expect(example.is(2)).toEqual(true) + expect(example.is('something')).toEqual(false) + expect(example.in([Example.test2])).toEqual(true) + expect(example.in([2])).toEqual(true) + expect(example.in(['something'])).toEqual(false) + expect(example.valueOf()).toEqual(2) + expect(example.toString()).toEqual('test2') + expect(example.toJSON()).toEqual('test2') + expect(JSON.stringify(example)).toEqual('"test2"') + }) + + it('It should use the value for toString and toJSON', () => { + class Example extends Enum { + static 'test1' = 1 + static 'test2' = 2 + static 'test3' = 3 + } + + const example = Example.create('test2', { output: 'value' }) + expect(example.key).toEqual('test2') + expect(example.value).toEqual(2) + expect(example.values).toEqual([1, 2, 3]) + expect(Example.options).toEqual({ + test1: 1, + test2: 2, + test3: 3 + }) + expect(Example.toJSON()).toEqual({ + test1: 1, + test2: 2, + test3: 3 + }) + expect(JSON.stringify(Example)).toEqual('{"test1":1,"test2":2,"test3":3}') expect(Example.test1).toEqual(1) expect(Example.test2).toEqual(2) expect(Example.test3).toEqual(3) @@ -221,5 +268,6 @@ describe('Test the encoding enum', () => { expect(example.valueOf()).toEqual(2) expect(example.toString()).toEqual('2') expect(example.toJSON()).toEqual(2) + expect(JSON.stringify(example)).toEqual('2') }) }) diff --git a/src/enum.js b/src/enum.js index 0eddd08..d228a46 100644 --- a/src/enum.js +++ b/src/enum.js @@ -2,30 +2,37 @@ export default class Enum { constructor () { this.key = null this.value = null + this.output = 'key' } static get options () { return Object.fromEntries(Object.entries(this)) } - static fromKey (key) { + static create (key, options = {}) { + return this.fromKey(key, options) + } + + static fromKey (key, options = {}) { const newEnum = new this() if (!newEnum.isValidKey(key)) { throw new Error(`Invalid enum key ${key}`) } const value = newEnum.constructor[key] newEnum.setKeyValue({ key, value }) + newEnum.setOptions(options) return newEnum } - static fromValue (value) { + static fromValue (value, options = {}) { const newEnum = new this() if (!newEnum.isValidValue(value)) { throw new Error(`Invalid enum value ${value}`) } const key = newEnum.invertedOptions[value] newEnum.setKeyValue({ key, value }) + newEnum.setOptions(options) return newEnum } @@ -36,6 +43,10 @@ export default class Enum { this.setValue(value) } + setOptions (options) { + this.output = options?.output || this.output + } + get invertedOptions () { return Object.fromEntries( Object.entries(this.constructor) @@ -102,10 +113,14 @@ export default class Enum { } toString () { - return this.value.toString() + return this[this.output].toString() } toJSON () { - return this.value + return this[this.output] + } + + static toJSON () { + return Object.fromEntries(Object.entries(this)) } }