@js-bits/enumerate
Advanced tools
Comparing version 0.3.0 to 0.4.0
21
index.js
@@ -10,3 +10,3 @@ const converters = new Map(); | ||
if (typeof type !== 'function') { | ||
throw new Error('Invalid Converter'); | ||
throw new Error('Invalid converter'); | ||
} | ||
@@ -29,5 +29,16 @@ | ||
if (result !== accumulator) { | ||
throw new Error('Invalid Converter'); | ||
throw new Error('Invalid converter'); | ||
} | ||
return Object.freeze(result); | ||
const proxy = new Proxy(Object.freeze(result), { | ||
get(...args) { | ||
const [target, prop] = args; | ||
if (!(prop in target)) { | ||
throw new Error(`Invalid enum key: ${prop}`); | ||
} | ||
return Reflect.get(...args); | ||
}, | ||
}); | ||
return proxy; | ||
}; | ||
@@ -37,3 +48,3 @@ | ||
if (args.length > 2) { | ||
throw new Error('Invalid Arguments'); | ||
throw new Error('Invalid arguments'); | ||
} | ||
@@ -43,3 +54,3 @@ | ||
if (args.length !== 1) { | ||
throw new Error('Invalid Arguments'); | ||
throw new Error('Invalid arguments'); | ||
} | ||
@@ -46,0 +57,0 @@ |
@@ -12,3 +12,3 @@ import { cyan } from '@js-bits/log-in-color'; | ||
`; | ||
test('should return an object with specified properties', () => { | ||
test('should return an object with specified keys', () => { | ||
expect(Episode).toHaveProperty('NEW_HOPE'); | ||
@@ -20,3 +20,3 @@ expect(Episode).toHaveProperty('EMPIRE'); | ||
describe('return object properties', () => { | ||
describe('return object keys', () => { | ||
test('should have unique values', () => { | ||
@@ -45,3 +45,3 @@ const { NEW_HOPE, EMPIRE, JEDI } = Episode; | ||
describe('symbol converter', () => { | ||
describe('return object properties', () => { | ||
describe('return object keys', () => { | ||
test('should have corresponding symbol values', () => { | ||
@@ -61,3 +61,3 @@ const Enum = enumerate(Symbol)`OPTION1 OPTION2 OPTION3`; | ||
describe('symbol.for converter', () => { | ||
describe('return object properties', () => { | ||
describe('return object keys', () => { | ||
test('should have corresponding symbol values', () => { | ||
@@ -77,6 +77,6 @@ const Enum = enumerate(Symbol.for)`OPTION1 OPTION2 OPTION3`; | ||
describe('number converter', () => { | ||
describe('return object properties', () => { | ||
describe('return object keys', () => { | ||
test('should have incrementing number values', () => { | ||
const Enum = enumerate(Number)`ZERO ONE TWO THREE`; | ||
expect(Enum).toEqual({ | ||
expect({ ...Enum }).toEqual({ | ||
ZERO: 0, | ||
@@ -92,6 +92,6 @@ ONE: 1, | ||
describe('string converter', () => { | ||
describe('return object properties', () => { | ||
describe('return object keys', () => { | ||
test('should have corresponding string values', () => { | ||
const Enum = enumerate(String)`A B C D`; | ||
expect(Enum).toEqual({ | ||
expect({ ...Enum }).toEqual({ | ||
A: 'A', | ||
@@ -107,3 +107,3 @@ B: 'B', | ||
describe('custom converter', () => { | ||
describe('return object properties', () => { | ||
describe('return object keys', () => { | ||
test('should have generated values', () => { | ||
@@ -115,3 +115,3 @@ const enumerateTens = enumerate((acc, item) => { | ||
const Enum = enumerateTens`CODE1 CODE2 CODE3`; | ||
expect(Enum).toEqual({ | ||
expect({ ...Enum }).toEqual({ | ||
CODE1: 0, | ||
@@ -130,3 +130,3 @@ CODE2: 10, | ||
const Enum = enumerateUpperCase`red green blue`; | ||
expect(Enum).toEqual({ | ||
expect({ ...Enum }).toEqual({ | ||
RED: 'red', | ||
@@ -158,3 +158,3 @@ GREEN: 'green', | ||
describe('when new property is assigned to the return object', () => { | ||
describe('when a new key is assigned to the return object', () => { | ||
test('should throw an error', () => { | ||
@@ -167,2 +167,11 @@ const result = enumerate`A B C`; | ||
}); | ||
describe('when an unknown key is accessed from the return object', () => { | ||
test('should throw an error', () => { | ||
const result = enumerate`A B C`; | ||
expect(() => { | ||
const { D } = result; | ||
}).toThrow('Invalid enum key: D'); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "@js-bits/enumerate", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Easy to use, Symbol-based enum implementation", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# Easy to use, Symbol-based enum implementation | ||
Just list values you need and `enumerate` tag function will create corresponding constants with unique values for your convenience. | ||
Just list values you need and `enumerate` tag function will create an object with corresponding keys and unique values for your convenience. | ||
@@ -70,2 +70,3 @@ ## Installation | ||
console.log(getEpisodeName(STAR_WARS.IV)); // A New Hope | ||
console.log(getEpisodeName(STAR_WARS.X)); // Error: Invalid enum key: X | ||
``` | ||
@@ -72,0 +73,0 @@ |
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
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
10969
215
110