enumerated-type
Advanced tools
Comparing version 0.5.2 to 0.5.4
80
index.js
'use strict'; | ||
const utilTypeFuncs = require('util-type-funcs'); | ||
const isFunction = utilTypeFuncs.isFunction; | ||
const isString = utilTypeFuncs.isString; | ||
const isValid = utilTypeFuncs.isValid; | ||
const isFunction = utilTypeFuncs.isFunction; | ||
const isString = utilTypeFuncs.isString; | ||
const isValid = utilTypeFuncs.isValid; | ||
@@ -152,41 +152,45 @@ const forEachBreak = require("for-each-break"); | ||
const enumValue = new enumValueConstructor(name, value); | ||
if (!isFunction(value)) { | ||
const enumValue = new enumValueConstructor(name, value); | ||
if (keyName !== UNDEFINED) { | ||
if (value[keyName] === UNDEFINED) { | ||
throw `IllegalArgument, key ${keyName} of each enum value cannot be undefined`; | ||
} | ||
if (keyName !== UNDEFINED) { | ||
if (value[keyName] === UNDEFINED) { | ||
throw `IllegalArgument, key ${keyName} of each enum value cannot be undefined`; | ||
} | ||
if (keyNameValues[value[keyName]] !== UNDEFINED) { | ||
throw `IllegalArgument, enum with ${keyName} of '${value[keyName]}' is already defined by ${keyNameValues[value[keyName]]._name}`; | ||
} | ||
keyNameValues[value[keyName]] = enumValue; | ||
} else { | ||
if (value === UNDEFINED) { | ||
throw `IllegalArgument, value of each enum value cannot be undefined`; | ||
} | ||
if (keyNameValues[value[keyName]] !== UNDEFINED) { | ||
throw `IllegalArgument, enum with ${keyName} of '${value[keyName]}' is already defined by ${keyNameValues[value[keyName]]._name}`; | ||
} | ||
keyNameValues[value[keyName]] = enumValue; | ||
} else { | ||
if (value === UNDEFINED) { | ||
throw `IllegalArgument, value of each enum value cannot be undefined`; | ||
} | ||
if (keyNameValues[value] !== UNDEFINED) { | ||
throw `IllegalArgument, enum with value of '${value}' is already defined by ${keyNameValues[value]._name}`; | ||
if (keyNameValues[value] !== UNDEFINED) { | ||
throw `IllegalArgument, enum with value of '${value}' is already defined by ${keyNameValues[value]._name}`; | ||
} | ||
keyNameValues[value] = enumValue; | ||
} | ||
keyNameValues[value] = enumValue; | ||
} | ||
Object.defineProperty(this, name, { | ||
value: enumValue, | ||
writable: false, | ||
enumerable: true, | ||
}); | ||
Object.defineProperty(this, name, { | ||
value: enumValue, | ||
writable: false, | ||
enumerable: true, | ||
}); | ||
if (typeof value === 'object') { | ||
if (isFunction(value)) { | ||
functionValues++; | ||
if (typeof value === 'object') { | ||
objectValues++; | ||
} else { | ||
objectValues++; | ||
nonObjectValues++; | ||
} | ||
enumValues.push(enumValue); | ||
} else { | ||
nonObjectValues++; | ||
Object.defineProperty(this, name, { | ||
value: value, | ||
writable: false, | ||
enumerable: true, | ||
}); | ||
functionValues++; | ||
} | ||
enumValues.push(enumValue); | ||
} | ||
@@ -253,23 +257,23 @@ | ||
Enum.prototype.forEach = function forEach(callback, thisArg, defaultValue) { | ||
return forEachBreak.forEach.call(this.values, callback, thisArg, defaultValue) | ||
return forEachBreak.forEach.call(this.values, callback, thisArg, defaultValue); | ||
}; | ||
Enum.prototype.map = function map(callback, thisArg) { | ||
return forEachBreak.map.call(this.values, callback, thisArg) | ||
return forEachBreak.map.call(this.values, callback, thisArg); | ||
}; | ||
Enum.prototype.filter = function filter(callback, thisArg) { | ||
return forEachBreak.filter.call(this.values, callback, thisArg) | ||
return forEachBreak.filter.call(this.values, callback, thisArg); | ||
}; | ||
Enum.prototype.forEachRight = function forEachRight(callback, thisArg, defaultValue) { | ||
return forEachBreak.forEachRight.call(this.values, callback, thisArg, defaultValue) | ||
return forEachBreak.forEachRight.call(this.values, callback, thisArg, defaultValue); | ||
}; | ||
Enum.prototype.mapRight = function mapRight(callback, thisArg) { | ||
return forEachBreak.mapRight.call(this.values, callback, thisArg) | ||
return forEachBreak.mapRight.call(this.values, callback, thisArg); | ||
}; | ||
Enum.prototype.filterRight = function filterRight(callback, thisArg) { | ||
return forEachBreak.filterRight.call(this.values, callback, thisArg) | ||
return forEachBreak.filterRight.call(this.values, callback, thisArg); | ||
}; | ||
@@ -276,0 +280,0 @@ |
{ | ||
"name": "enumerated-type", | ||
"version": "0.5.2", | ||
"version": "0.5.4", | ||
"description": "enum type for javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -52,5 +52,11 @@ # enumerated-type | ||
// this will be ignored and a function that resolves stepTypeId to Enum type will be used | ||
stepTypeId(value) { | ||
return StepTypeValue; | ||
}, | ||
// this will be available as a function property of the enum, and will no be part of its values | ||
customFunction(works) { | ||
return `Yes, it ${works}!`; | ||
} | ||
}; | ||
@@ -57,0 +63,0 @@ |
@@ -5,2 +5,3 @@ # Version History | ||
- [0.5.4](#054) | ||
- [0.5.2](#052) | ||
@@ -15,2 +16,7 @@ - [0.5.0](#050) | ||
## 0.5.4 | ||
* Add: function properties in values object do not get added to the enum values but are added to | ||
the properties of the enum. Gives ability ot define custom static functions. | ||
## 0.5.2 | ||
@@ -17,0 +23,0 @@ |
20567
253
175