Skip to content

Commit

Permalink
Merge pull request #11 from hckrnews/feature/first-draft
Browse files Browse the repository at this point in the history
Add the create method
  • Loading branch information
w3nl authored Oct 5, 2021
2 parents 31d8279 + b4e613f commit 0a39cfa
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 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.4.0",
"version": "1.4.1",
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
Expand Down
50 changes: 49 additions & 1 deletion src/__tests__/enum.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)
Expand All @@ -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')
})
})
23 changes: 19 additions & 4 deletions src/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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))
}
}

0 comments on commit 0a39cfa

Please sign in to comment.