@js-bits/enumerate
Advanced tools
Comparing version 0.5.2 to 0.6.0
47
index.js
const converters = new Map(); | ||
class EnumType { | ||
// eslint-disable-next-line class-methods-use-this | ||
get [Symbol.toStringTag]() { | ||
return this.type.name; | ||
} | ||
constructor(type, args) { | ||
this.type = type; | ||
this.args = args; | ||
} | ||
} | ||
converters.set(Function, (acc, item) => { | ||
const fn = (...args) => new EnumType(fn, args); | ||
Object.defineProperty(fn, 'name', { value: item }); | ||
return fn; | ||
}); | ||
converters.set(String, (acc, item) => item); | ||
@@ -9,3 +27,8 @@ converters.set(Symbol, (acc, item) => Symbol(item)); | ||
const convert = (list, type = Symbol) => { | ||
if (typeof type !== 'function') { | ||
let enumType = type; | ||
let enumArgs = []; | ||
if (typeof enumType === 'object' && enumType instanceof EnumType) { | ||
enumType = type.type; | ||
enumArgs = type.args; | ||
} else if (typeof enumType !== 'function') { | ||
throw new Error('Invalid converter'); | ||
@@ -15,10 +38,10 @@ } | ||
let converter; | ||
const valueConverter = converters.get(type); | ||
const valueConverter = converters.get(enumType); | ||
if (valueConverter) { | ||
converter = (acc, item) => { | ||
acc[item] = valueConverter(acc, item); | ||
acc[item] = valueConverter(acc, item, ...enumArgs); | ||
return acc; | ||
}; | ||
} else { | ||
converter = type; | ||
converter = enumType; | ||
} | ||
@@ -69,2 +92,18 @@ const values = list.trim().split(/[\s\n,]+/); | ||
// dynamically created types | ||
const TYPES = enumerate(Function)` | ||
LowerCase | ||
UpperCase | ||
Increment | ||
`; | ||
converters.set(TYPES.LowerCase, (acc, item) => item.toLowerCase()); | ||
converters.set(TYPES.UpperCase, (acc, item) => item.toUpperCase()); | ||
converters.set( | ||
TYPES.Increment, | ||
(acc, item, increment = 1, start = increment) => start + Object.keys(acc).length * increment | ||
); | ||
Object.assign(enumerate, TYPES); | ||
export default enumerate; | ||
@@ -71,0 +110,0 @@ |
import enumerate from './index.js'; | ||
// import enumerate from './dist/index.cjs'; | ||
// const enumerate = require('./dist/index.cjs'); | ||
const { LowerCase, UpperCase, Increment } = enumerate; | ||
describe(`enumerate`, () => { | ||
@@ -18,2 +18,15 @@ const Episode = enumerate` | ||
test('type names', () => { | ||
expect(LowerCase.name).toEqual('LowerCase'); | ||
expect(UpperCase.name).toEqual('UpperCase'); | ||
expect(Increment.name).toEqual('Increment'); | ||
}); | ||
test('enum type', () => { | ||
const enumType = Increment(10); | ||
expect(enumType).toHaveProperty('args', [10]); | ||
expect(enumType).toHaveProperty('type', Increment); | ||
expect(String(enumType)).toEqual('[object Increment]'); | ||
}); | ||
describe('return object keys', () => { | ||
@@ -42,3 +55,3 @@ test('should have unique values', () => { | ||
describe('symbol converter', () => { | ||
describe('Symbol converter', () => { | ||
describe('return object keys', () => { | ||
@@ -58,3 +71,3 @@ test('should have corresponding symbol values', () => { | ||
describe('symbol.for converter', () => { | ||
describe('Symbol.for converter', () => { | ||
describe('return object keys', () => { | ||
@@ -74,3 +87,3 @@ test('should have corresponding symbol values', () => { | ||
describe('number converter', () => { | ||
describe('Number converter', () => { | ||
describe('return object keys', () => { | ||
@@ -89,3 +102,3 @@ test('should have incrementing number values', () => { | ||
describe('string converter', () => { | ||
describe('String converter', () => { | ||
describe('return object keys', () => { | ||
@@ -104,2 +117,70 @@ test('should have corresponding string values', () => { | ||
describe('LowerCase converter', () => { | ||
describe('return object keys', () => { | ||
test('should have lower-cased string values', () => { | ||
const Enum = enumerate(LowerCase)`CODE_A CODE_B CODE_C CODE_D`; | ||
expect({ ...Enum }).toEqual({ | ||
CODE_A: 'code_a', | ||
CODE_B: 'code_b', | ||
CODE_C: 'code_c', | ||
CODE_D: 'code_d', | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('UpperCase converter', () => { | ||
describe('return object keys', () => { | ||
test('should have upper-cased string values', () => { | ||
const Enum = enumerate(UpperCase)`code_a code_b code_c code_d`; | ||
expect({ ...Enum }).toEqual({ | ||
code_a: 'CODE_A', | ||
code_b: 'CODE_B', | ||
code_c: 'CODE_C', | ||
code_d: 'CODE_D', | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Increment converter', () => { | ||
describe('return object keys', () => { | ||
describe('when no arguments provided', () => { | ||
test('should have incremented by 1 values', () => { | ||
const Enum = enumerate(Increment())`A B C D`; | ||
expect({ ...Enum }).toEqual({ | ||
A: 1, | ||
B: 2, | ||
C: 3, | ||
D: 4, | ||
}); | ||
}); | ||
}); | ||
describe('when one argument is provided', () => { | ||
test('should have incremented by specified number values', () => { | ||
const Enum = enumerate(Increment(10))`A B C D`; | ||
expect({ ...Enum }).toEqual({ | ||
A: 10, | ||
B: 20, | ||
C: 30, | ||
D: 40, | ||
}); | ||
}); | ||
}); | ||
describe('when two argument is provided', () => { | ||
test('should have incremented by a specified number values, starting from a specified number', () => { | ||
const Enum = enumerate(Increment(100, 199))`A B C D`; | ||
expect({ ...Enum }).toEqual({ | ||
A: 199, | ||
B: 299, | ||
C: 399, | ||
D: 499, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('custom converter', () => { | ||
@@ -106,0 +187,0 @@ describe('return object keys', () => { |
{ | ||
"name": "@js-bits/enumerate", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "Easy to use, Symbol-based enum implementation", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -79,3 +79,3 @@ # Easy to use, Symbol-based enum implementation | ||
## Primitive values | ||
## Primitive enum converters | ||
@@ -100,15 +100,50 @@ By default `enumerate` converts values to [Symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol): | ||
## Advanced enum converters | ||
There are several advanced converters also available. | ||
```javascript | ||
const { LowerCase, UpperCase, Increment } = enumerate; | ||
console.log(enumerate(LowerCase)` | ||
VALUE1 | ||
VALUE2 | ||
VALUE3 | ||
`); // { VALUE1: 'value1', VALUE2: 'value2', VALUE3: 'value3' } | ||
console.log(enumerate(UpperCase)` | ||
value1 | ||
value2 | ||
value3 | ||
`); // { value1: 'VALUE1', value2: 'VALUE2', value3: 'VALUE3' } | ||
console.log(enumerate(Increment(10))` | ||
VALUE1 | ||
VALUE2 | ||
VALUE3 | ||
`); // { VALUE1: 10, VALUE2: 20, VALUE3: 30 } | ||
// the second argument here is a start value (equals to he first arguments if not specified) | ||
console.log(enumerate(Increment(10, 19))` | ||
VALUE1 | ||
VALUE2 | ||
VALUE3 | ||
`); // { VALUE1: 19, VALUE2: 29, VALUE3: 39 } | ||
``` | ||
## Customization | ||
Or you can implement your custom converter: | ||
```javascript | ||
const enum10s = enumerate((acc, item) => { | ||
acc[item] = (Object.keys(acc).length + 1) * 10; | ||
const customEnum = enumerate((acc, item) => { | ||
acc[`-${item}-`] = `-${(Object.keys(acc).length + 1) * 10}-`; | ||
return acc; | ||
}); | ||
console.log(enum10s` | ||
console.log(customEnum` | ||
CODE1 | ||
CODE2 | ||
CODE3 | ||
`); // { CODE1: 10, CODE2: 20, CODE3: 30 } | ||
`); // { '-CODE1-': '-10-', '-CODE2-': '-20-', '-CODE3-': '-30-' } | ||
``` | ||
@@ -115,0 +150,0 @@ |
Sorry, the diff of this file is not supported yet
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
18890
422
155