Comparing version 0.2.7 to 1.0.0
492
lib/enum.js
@@ -1,301 +0,317 @@ | ||
(function (root, module, global, define) { | ||
"use strict"; | ||
"use strict"; | ||
var endianness = require('os').endianness(); | ||
/** | ||
* Represents an Item of an Enum. | ||
* @param {String} key The Enum key. | ||
* @param {Number} value The Enum value. | ||
*/ | ||
function EnumItem(key, value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
EnumItem.prototype = { | ||
/*constructor reference so that, this.constructor===EnumItem//=>true */ | ||
constructor: EnumItem, | ||
/** | ||
* Represents an Item of an Enum. | ||
* @param {String} key The Enum key. | ||
* @param {Number} value The Enum value. | ||
* Checks if the flagged EnumItem has the passing object. | ||
* @param {EnumItem || String || Number} value The object to check with. | ||
* @return {Boolean} The check result. | ||
*/ | ||
function EnumItem(key, value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
has: function(value) { | ||
if (value instanceof EnumItem || (typeof(value) === 'object' && value.key !== undefined && value.value !== undefined)) { | ||
return (this.value & value.value) !== 0; | ||
} else if (typeof(value) === 'string') { | ||
return this.key.indexOf(value) >= 0; | ||
} else { | ||
return (this.value & value) !== 0; | ||
} | ||
}, | ||
EnumItem.prototype = { | ||
/** | ||
* Checks if the EnumItem is the same as the passing object. | ||
* @param {EnumItem || String || Number} key The object to check with. | ||
* @return {Boolean} The check result. | ||
*/ | ||
is: function(key) { | ||
if (key instanceof EnumItem || (typeof(key) === 'object' && key.key !== undefined && key.value !== undefined)) { | ||
return this.key === key.key; | ||
} else if (typeof(key) === 'string') { | ||
return this.key === key; | ||
} else { | ||
return this.value === key; | ||
} | ||
}, | ||
/*constructor reference so that, this.constructor===EnumItem//=>true */ | ||
constructor: EnumItem, | ||
/** | ||
* Returns String representation of this EnumItem. | ||
* @return {String} String representation of this EnumItem. | ||
*/ | ||
toString: function() { | ||
return this.key; | ||
}, | ||
/** | ||
* Checks if the flagged EnumItem has the passing object. | ||
* @param {EnumItem || String || Number} value The object to check with. | ||
* @return {Boolean} The check result. | ||
*/ | ||
has: function(value) { | ||
if (value instanceof EnumItem || (typeof(value) === 'object' && value.key !== undefined && value.value !== undefined)) { | ||
return (this.value & value.value) !== 0; | ||
} else if (typeof(value) === 'string') { | ||
return this.key.indexOf(value) >= 0; | ||
} else { | ||
return (this.value & value) !== 0; | ||
} | ||
}, | ||
/** | ||
* Returns JSON object representation of this EnumItem. | ||
* @return {String} JSON object representation of this EnumItem. | ||
*/ | ||
toJSON: function() { | ||
return this.key; | ||
}, | ||
/** | ||
* Checks if the EnumItem is the same as the passing object. | ||
* @param {EnumItem || String || Number} key The object to check with. | ||
* @return {Boolean} The check result. | ||
*/ | ||
is: function(key) { | ||
if (key instanceof EnumItem || (typeof(key) === 'object' && key.key !== undefined && key.value !== undefined)) { | ||
return this.key === key.key; | ||
} else if (typeof(key) === 'string') { | ||
return this.key === key; | ||
} else { | ||
return this.value === key; | ||
} | ||
}, | ||
/** | ||
* Returns the value to compare with. | ||
* @return {String} The value to compare with. | ||
*/ | ||
valueOf: function() { | ||
return this.value; | ||
} | ||
/** | ||
* Returns String representation of this EnumItem. | ||
* @return {String} String representation of this EnumItem. | ||
*/ | ||
toString: function() { | ||
return this.key; | ||
}, | ||
}; | ||
/** | ||
* Returns JSON object representation of this EnumItem. | ||
* @return {String} JSON object representation of this EnumItem. | ||
*/ | ||
toJSON: function() { | ||
return this.key; | ||
}, | ||
/** | ||
* Returns the value to compare with. | ||
* @return {String} The value to compare with. | ||
*/ | ||
valueOf: function() { | ||
return this.key; | ||
} | ||
/** | ||
* Represents an Enum with enum items. | ||
* @param {Array || Object} map This are the enum items. | ||
* @param {String || Object} options This are options. [optional] | ||
*/ | ||
function Enum(map, options) { | ||
}; | ||
if (options && typeof(options) === 'string') { | ||
options = { name: options }; | ||
} | ||
this._options = options || {}; | ||
this._options.separator = this._options.separator || ' | '; | ||
this._options.endianness = this._options.endianness || endianness; | ||
/** | ||
* Represents an Enum with enum items. | ||
* @param {Array || Object} map This are the enum items. | ||
* @param {String || Object} options This are options. [optional] | ||
*/ | ||
function Enum(map, options) { | ||
this.enums = []; | ||
if (options && typeof(options) === 'string') { | ||
options = { name: options }; | ||
if (map.length) { | ||
var array = map; | ||
map = {}; | ||
for (var i = 0; i < array.length; i++) { | ||
map[array[i]] = Math.pow(2, i); | ||
} | ||
} | ||
this._options = options || {}; | ||
this._options.separator = this._options.separator || ' | '; | ||
for (var member in map) { | ||
if ((this._options.name && member === 'name') || member === '_options' || member === 'get' || member === 'getKey' || member === 'getValue' || member === 'enums' || member === 'isFlaggable') { | ||
throw new Error('Enum key "' + member + '" is a reserved word!'); | ||
} | ||
this[member] = new EnumItem(member, map[member]); | ||
this.enums.push(this[member]); | ||
} | ||
this.enums = []; | ||
if (this._options.name) { | ||
this.name = this._options.name; | ||
} | ||
if (map.length) { | ||
var array = map; | ||
map = {}; | ||
var self = this; | ||
for (var i = 0; i < array.length; i++) { | ||
map[array[i]] = Math.pow(2, i); | ||
} | ||
} | ||
function isFlaggable() { | ||
for (var i = 0, len = self.enums.length; i < len; i++) { | ||
var e = self.enums[i]; | ||
for (var member in map) { | ||
if ((this._options.name && member === 'name') || member === '_options' || member === 'get' || member === 'getKey' || member === 'getValue' || member === 'enums' || member === 'isFlaggable') { | ||
throw new Error('Enum key "' + member + '" is a reserved word!'); | ||
if (!((e.value !== 0) && !(e.value & (e.value - 1)))) { | ||
return false; | ||
} | ||
this[member] = new EnumItem(member, map[member]); | ||
this.enums.push(this[member]); | ||
} | ||
return true; | ||
} | ||
if (this._options.name) { | ||
this.name = this._options.name; | ||
} | ||
this.isFlaggable = isFlaggable(); | ||
this.freezeEnums(); //this will make instances of Enum non-extensible | ||
} | ||
var self = this; | ||
Enum.prototype = { | ||
function isFlaggable() { | ||
for (var i = 0, len = self.enums.length; i < len; i++) { | ||
var e = self.enums[i]; | ||
/*constructor reference so that, this.constructor===Enum//=>true */ | ||
constructor: Enum, | ||
if (!((e.value !== 0) && !(e.value & (e.value - 1)))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
/* implement the "ref type interface", so that Enum types can | ||
* be used in `node-ffi` function declarations and invokations. | ||
* In C, these Enums act as `uint32_t` types. | ||
* | ||
* https://github.com/TooTallNate/ref#the-type-interface | ||
*/ | ||
size: 4, | ||
indirection: 1, | ||
/** | ||
* Returns the appropriate EnumItem key. | ||
* @param {EnumItem || String || Number} key The object to get with. | ||
* @return {String} The get result. | ||
*/ | ||
getKey: function(value) { | ||
var item = this.get(value); | ||
if (item) { | ||
return item.key; | ||
} else { | ||
return 'Undefined'; | ||
} | ||
}, | ||
this.isFlaggable = isFlaggable(); | ||
this.freezeEnums(); //this will make instances of Enum non-extensible | ||
} | ||
/** | ||
* Returns the appropriate EnumItem value. | ||
* @param {EnumItem || String || Number} key The object to get with. | ||
* @return {Number} The get result. | ||
*/ | ||
getValue: function(key) { | ||
var item = this.get(key); | ||
if (item) { | ||
return item.value; | ||
} else { | ||
return null; | ||
} | ||
}, | ||
Enum.prototype = { | ||
/** | ||
* Returns the appropriate EnumItem. | ||
* @param {EnumItem || String || Number} key The object to get with. | ||
* @return {EnumItem} The get result. | ||
*/ | ||
get: function(key, offset) { | ||
if (key === null || key === undefined) return null; | ||
/*constructor reference so that, this.constructor===Enum//=>true */ | ||
constructor: Enum, | ||
// Buffer instance support, part of the ref Type interface | ||
if (Buffer.isBuffer(key)) { | ||
key = key['readUInt32' + this._options.endianness](offset || 0); | ||
} | ||
/** | ||
* Returns the appropriate EnumItem key. | ||
* @param {EnumItem || String || Number} key The object to get with. | ||
* @return {String} The get result. | ||
*/ | ||
getKey: function(value) { | ||
var item = this.get(value); | ||
if (item) { | ||
return item.key; | ||
} else { | ||
return 'Undefined'; | ||
if (key instanceof EnumItem || (typeof(key) === 'object' && key.key !== undefined && key.value !== undefined)) { | ||
var foundIndex = this.enums.indexOf(key); | ||
if (foundIndex >= 0) { | ||
return key; | ||
} | ||
}, | ||
/** | ||
* Returns the appropriate EnumItem value. | ||
* @param {EnumItem || String || Number} key The object to get with. | ||
* @return {Number} The get result. | ||
*/ | ||
getValue: function(key) { | ||
var item = this.get(key); | ||
if (item) { | ||
return item.value; | ||
} else { | ||
if (!this.isFlaggable || (this.isFlaggable && key.key.indexOf(this._options.separator) < 0)) { | ||
return null; | ||
} | ||
}, | ||
return this.get(key.key); | ||
} else if (typeof(key) === 'string') { | ||
if (key.indexOf(this._options.separator) > 0) { | ||
var parts = key.split(this._options.separator); | ||
/** | ||
* Returns the appropriate EnumItem. | ||
* @param {EnumItem || String || Number} key The object to get with. | ||
* @return {EnumItem} The get result. | ||
*/ | ||
get: function(key) { | ||
if (key === null || key === undefined) return null; | ||
var value = 0; | ||
for(var i = 0; i < parts.length; i++) { | ||
var part = parts[i]; | ||
if (key instanceof EnumItem || (typeof(key) === 'object' && key.key !== undefined && key.value !== undefined)) { | ||
var foundIndex = this.enums.indexOf(key); | ||
if (foundIndex >= 0) { | ||
return key; | ||
value |= this[part].value; | ||
} | ||
if (!this.isFlaggable || (this.isFlaggable && key.key.indexOf(this._options.separator) < 0)) { | ||
return null; | ||
} | ||
return this.get(key.key); | ||
} else if (typeof(key) === 'string') { | ||
if (key.indexOf(this._options.separator) > 0) { | ||
var parts = key.split(this._options.separator); | ||
var value = 0; | ||
for(var i = 0; i < parts.length; i++) { | ||
var part = parts[i]; | ||
value |= this[part].value; | ||
} | ||
return new EnumItem(key, value); | ||
} else { | ||
return this[key]; | ||
} | ||
return new EnumItem(key, value); | ||
} else { | ||
for (var m in this) { | ||
if (this.hasOwnProperty(m)) { | ||
if (this[m].value === key) { | ||
return this[m]; | ||
} | ||
return this[key]; | ||
} | ||
} else { | ||
for (var m in this) { | ||
if (this.hasOwnProperty(m)) { | ||
if (this[m].value === key) { | ||
return this[m]; | ||
} | ||
} | ||
} | ||
var result = null; | ||
var result = null; | ||
if (this.isFlaggable) { | ||
for (var n in this) { | ||
if (this.hasOwnProperty(n)) { | ||
if ((key & this[n].value) !== 0) { | ||
if (result) { | ||
result += this._options.separator; | ||
} else { | ||
result = ''; | ||
} | ||
result += n; | ||
if (this.isFlaggable) { | ||
for (var n in this) { | ||
if (this.hasOwnProperty(n)) { | ||
if ((key & this[n].value) !== 0) { | ||
if (result) { | ||
result += this._options.separator; | ||
} else { | ||
result = ''; | ||
} | ||
result += n; | ||
} | ||
} | ||
} | ||
return this.get(result || null); | ||
} | ||
}, | ||
/** | ||
* Define freezeEnums() as a property of the prototype. | ||
* make enumerable items nonconfigurable and deep freeze the properties. Throw Error on property setter. | ||
*/ | ||
freezeEnums: function() { | ||
function freezer(o) { | ||
var props = Object.getOwnPropertyNames(o); | ||
props.forEach( function(p){ | ||
if (!Object.getOwnPropertyDescriptor(o, p).configurable) { | ||
return; | ||
} | ||
return this.get(result || null); | ||
} | ||
}, | ||
Object.defineProperties(o, p, {writable:false, configurable:false}); | ||
}) | ||
return o; | ||
} | ||
/** | ||
* Sets the Enum "value" onto the give `buffer` at the specified `offset`. | ||
* Part of the ref "Type interface". | ||
* | ||
* @param {Buffer} buffer The Buffer instance to write to. | ||
* @param {Number} offset The offset in the buffer to write to. Default 0. | ||
* @param {EnumItem || String || Number} value The EnumItem to write. | ||
*/ | ||
set: function (buffer, offset, value) { | ||
var item = this.get(value); | ||
if (item) { | ||
return buffer['writeUInt32' + this._options.endianness](item.value, offset || 0); | ||
} | ||
}, | ||
function getPropertyValue(value) { | ||
return value; | ||
} | ||
function deepFreezeEnums(o) { | ||
if (typeof o !== 'object' || o === null || Object.isFrozen(o) || Object.isSealed(o) ){ | ||
/** | ||
* Define freezeEnums() as a property of the prototype. | ||
* make enumerable items nonconfigurable and deep freeze the properties. Throw Error on property setter. | ||
*/ | ||
freezeEnums: function() { | ||
function freezer(o) { | ||
var props = Object.getOwnPropertyNames(o); | ||
props.forEach( function(p){ | ||
if (!Object.getOwnPropertyDescriptor(o, p).configurable) { | ||
return; | ||
} | ||
for (var key in o) { | ||
if (o.hasOwnProperty(key)) { | ||
o.__defineGetter__(key, getPropertyValue.bind(null, o[key])); | ||
o.__defineSetter__(key, function throwPropertySetError(value){throw TypeError("Cannot redefine property; Enum Type is not extensible.")}); | ||
deepFreezeEnums(o[key]); | ||
} | ||
Object.defineProperties(o, p, {writable:false, configurable:false}); | ||
}) | ||
return o; | ||
} | ||
function getPropertyValue(value) { | ||
return value; | ||
} | ||
function deepFreezeEnums(o) { | ||
if (typeof o !== 'object' || o === null || Object.isFrozen(o) || Object.isSealed(o) ){ | ||
return; | ||
} | ||
for (var key in o) { | ||
if (o.hasOwnProperty(key)) { | ||
o.__defineGetter__(key, getPropertyValue.bind(null, o[key])); | ||
o.__defineSetter__(key, function throwPropertySetError(value){throw TypeError("Cannot redefine property; Enum Type is not extensible.")}); | ||
deepFreezeEnums(o[key]); | ||
} | ||
if (Object.freeze) { | ||
Object.freeze(o); | ||
} else { | ||
freezer(o); | ||
} | ||
} | ||
if (Object.freeze) { | ||
Object.freeze(o); | ||
} else { | ||
freezer(o); | ||
} | ||
} | ||
deepFreezeEnums(this); | ||
deepFreezeEnums(this); | ||
return this; | ||
}, | ||
}; | ||
return this; | ||
}, | ||
}; | ||
if (module && module.exports) { | ||
module.exports = Enum; | ||
} else if (define) { | ||
define(function () { | ||
return Enum; | ||
}); | ||
} else { | ||
root.Enum = Enum; | ||
/** | ||
* Registers the Enum Type globally in node.js. | ||
* @param {String} key Global variable. [optional] | ||
*/ | ||
Enum.register = function(key) { | ||
key = key || 'Enum'; | ||
if (!global[key]) { | ||
global[key] = Enum; | ||
} | ||
}; | ||
if (module && module.exports && global) { | ||
if (typeof (window) !== 'undefined') { | ||
window.Enum = Enum; | ||
} | ||
/** | ||
* Registers the Enum Type globally in node.js. | ||
* @param {String} key Global variable. [optional] | ||
*/ | ||
Enum.register = function(key) { | ||
key = key || 'Enum'; | ||
if (!global[key]) { | ||
global[key] = Enum; | ||
} | ||
}; | ||
} | ||
}( | ||
this, | ||
typeof(module) !== 'undefined' ? module : undefined, | ||
typeof(global) !== 'undefined' ? global : undefined, | ||
typeof(define) !== 'undefined' ? define : undefined | ||
)); | ||
module.exports = Enum; |
{ | ||
"author": "adrai", | ||
"name": "enum", | ||
"version": "0.2.7", | ||
"version": "1.0.0", | ||
"private": false, | ||
@@ -13,11 +13,13 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": ">= 1.0.1", | ||
"expect.js": ">= 0.1.2", | ||
"smoosh": ">= 0.4.1" | ||
"gulp": "^3.8.11", | ||
"gulp-browserify": "^0.5.1", | ||
"gulp-rename": "^1.2.0", | ||
"gulp-uglify": "^1.1.0", | ||
"mocha": ">= 1.0.1" | ||
}, | ||
"description": "Enum is a javascript module that introduces the Enum Type. It works for node.js and in the browser.", | ||
"keywords" : [ | ||
"keywords": [ | ||
"enum" | ||
@@ -39,5 +41,5 @@ ], | ||
], | ||
"scripts" : { | ||
"test" : "mocha" | ||
"scripts": { | ||
"test": "mocha" | ||
} | ||
} |
185
README.md
@@ -16,4 +16,4 @@ # Introduction | ||
|:------------|:----------------|:---------| | ||
| `enum-0.2.7.js` | *uncompressed, with comments* | [Download](https://raw.github.com/adrai/enum/master/enum-0.2.7.js) | | ||
| `enum-0.2.7.min.js` | *compressed, without comments* | [Download](https://raw.github.com/adrai/enum/master/enum-0.2.7.min.js) | | ||
| `enum-1.0.0.js` | *uncompressed, with comments* | [Download](https://raw.github.com/adrai/enum/master/enum-1.0.0.js) | | ||
| `enum-1.0.0.min.js` | *compressed, without comments* | [Download](https://raw.github.com/adrai/enum/master/enum-1.0.0.min.js) | | ||
@@ -24,3 +24,3 @@ # Installation (node.js) | ||
# Installation (browser, library is AMD compatible) | ||
# Installation (browser) | ||
@@ -31,18 +31,19 @@ <script src="enum.js"></script> | ||
// use it as module | ||
var Enum = require('enum'); | ||
````js | ||
// use it as module | ||
var Enum = require('enum'); | ||
// or extend node.js with this new type | ||
require('enum').register(); | ||
// or extend node.js with this new type | ||
require('enum').register(); | ||
// define a simple enum (automatically flaggable -> A: 0x01, B: 0x02, C: 0x04) | ||
//Uses bitwise 'OR' operation in between the values and creates enumerated constants. For example, if 'Read':1, 'Write':2, then ReadWrite= Read | Write = 1 | 2 = 3; | ||
var myEnum = new Enum(['A', 'B', 'C']); | ||
// define a simple enum (automatically flaggable -> A: 0x01, B: 0x02, C: 0x04) | ||
//Uses bitwise 'OR' operation in between the values and creates enumerated constants. For example, if 'Read':1, 'Write':2, then ReadWrite= Read | Write = 1 | 2 = 3; | ||
var myEnum = new Enum(['A', 'B', 'C']); | ||
//define a flagged enum object to create a multicolor option; just pass an array | ||
var myEnum = new Enum([Black', 'Red', 'Green', 'Blue']); | ||
myEnum; //=> Enum {_options: Object, enums: Array[4], Black: EnumItem, Red: EnumItem, Green: EnumItem….....} | ||
myEnum.isFlaggable; //=> true | ||
//define a flagged enum object to create a multicolor option; just pass an array | ||
var myEnum = new Enum(['Black', 'Red', 'Green', 'Blue']); | ||
myEnum; //=> Enum {_options: Object, enums: Array[4], Black: EnumItem, Red: EnumItem, Green: EnumItem….....} | ||
myEnum.isFlaggable; //=> true | ||
for(var i=1; i<8; i++){ console.log(myEnum.get(i).value + '=> '+ myEnum.get(i).key)} | ||
for(var i=1; i<8; i++){ console.log(myEnum.get(i).value + '=> '+ myEnum.get(i).key)} | ||
1=> Black | ||
@@ -56,20 +57,24 @@ 2=> Red | ||
// define an enum with own values | ||
var myEnum = new Enum({'A': 1, 'B': 2, 'C': 4}); | ||
// define an enum with own values | ||
var myEnum = new Enum({'A': 1, 'B': 2, 'C': 4}); | ||
// if defining a flaggable enum, you can define your own separator (default is ' | ') | ||
var myEnum = new Enum(['A', 'B', 'C'], { separator: ' | ' }); | ||
// if defining a flaggable enum, you can define your own separator (default is ' | ') | ||
var myEnum = new Enum(['A', 'B', 'C'], { separator: ' | ' }); | ||
// if you want your enum to have a name define it in the options | ||
var myEnum = new Enum(['A', 'B', 'C'], { name: 'MyEnum' }); | ||
// if you want your enum to have a name define it in the options | ||
var myEnum = new Enum(['A', 'B', 'C'], { name: 'MyEnum' }); | ||
// or | ||
var myEnum = new Enum(['A', 'B', 'C'], 'MyEnum'); | ||
// if you want your enum to have an explicit "endianness", define it in the options | ||
// (defaults to `os.endianness()`) | ||
var myEnum = new Enum(['A', 'B', 'C'], { endianness: 'BE' }); | ||
//define enum type without flag | ||
var myEnum = new Enum({'None': 0, 'Black':1, 'Red': 2, 'Red2': 3, 'Green': 4, 'Blue': 5}); | ||
myEnum; //=> Enum {_options: Object, enums: Array[6], None: EnumItem, Black: EnumItem, Red: EnumItem…........} | ||
myEnum.isFlaggable; //=> false | ||
// or | ||
var myEnum = new Enum(['A', 'B', 'C'], 'MyEnum'); | ||
for(var i=0; i<=5; i++){ console.log(myEnum.get(i).value + '=> '+ myEnum.get(i).key)} | ||
//define enum type without flag | ||
var myEnum = new Enum({'None': 0, 'Black':1, 'Red': 2, 'Red2': 3, 'Green': 4, 'Blue': 5}); | ||
myEnum; //=> Enum {_options: Object, enums: Array[6], None: EnumItem, Black: EnumItem, Red: EnumItem…........} | ||
myEnum.isFlaggable; //=> false | ||
for(var i=0; i<=5; i++){ console.log(myEnum.get(i).value + '=> '+ myEnum.get(i).key)} | ||
0=> None | ||
@@ -82,93 +87,93 @@ 1=> Black | ||
// get your item | ||
myEnum.A | ||
// get your item | ||
myEnum.A | ||
// or | ||
myEnum.get('A') | ||
// or | ||
myEnum.get('A') | ||
// or | ||
myEnum.get(1) | ||
// or | ||
myEnum.get(1) | ||
// or | ||
myEnum.get('A | B') | ||
// or | ||
myEnum.get('A | B') | ||
// or | ||
myEnum.get(3) | ||
// or | ||
myEnum.get(3) | ||
// get your value | ||
myEnum.A.value | ||
// get your value | ||
myEnum.A.value | ||
// get your key | ||
myEnum.A.key | ||
// get your key | ||
myEnum.A.key | ||
// get all items | ||
myEnum.enums // returns all enums in an array | ||
// get all items | ||
myEnum.enums // returns all enums in an array | ||
// compare | ||
myEnum.A.is(myEnum.A) | ||
// compare | ||
myEnum.A.is(myEnum.A) | ||
// or | ||
myEnum.A.is('A') | ||
// or | ||
myEnum.A.is('A') | ||
// or | ||
myEnum.A.is(1) | ||
// or | ||
myEnum.A.is(1) | ||
// or | ||
myEnum.A == 'A' | ||
// or | ||
myEnum.A == 'A' | ||
// or | ||
myEnum.A == myEnum.A | ||
// or | ||
myEnum.A == myEnum.A | ||
// or | ||
myEnum.A === myEnum.A | ||
// or | ||
myEnum.A === myEnum.A | ||
// check flag | ||
var myItem = myEnum.get(3); // or [myEnum.get('A | B')] | ||
myItem.has(myEnum.A) | ||
// check flag | ||
var myItem = myEnum.get(3); // or [myEnum.get('A | B')] | ||
myItem.has(myEnum.A) | ||
// or | ||
myItem.has('A') | ||
// or | ||
myItem.has('A') | ||
// or | ||
myItem.has(1) | ||
// or | ||
myItem.has(1) | ||
// other functions | ||
myItem.toString() // returns A | C | ||
myItem.toJSON() // returns A | C | ||
myItem.valueOf() // returns A | C | ||
// other functions | ||
myItem.toString() // returns 'A | C' | ||
myItem.toJSON() // returns '"A | C"' | ||
myItem.valueOf() // returns 3 | ||
JSON.stringify(myItem) // returns A | C | ||
JSON.stringify(myItem) // returns '"A | C"' | ||
//Type Safety: | ||
//Newly created enumerable objects are Type-Safe in a way that it's non-configurable and no longer extensible. | ||
//Each EnumItem has a beack-reference to a constructor and they are implicitly final. | ||
Object.getOwnPropertyDescriptor(myEnum, 'Red'); //=> Object {value: EnumItem, writable: false, enumerable: true, configurable: false} | ||
Object.isExtensible(myEnum); //=> false | ||
myEnum instanceof Enum; //=> true | ||
//Type Safety: | ||
//Newly created enumerable objects are Type-Safe in a way that it's non-configurable and no longer extensible. | ||
//Each EnumItem has a beack-reference to a constructor and they are implicitly final. | ||
Object.getOwnPropertyDescriptor(myEnum, 'Red'); //=> Object {value: EnumItem, writable: false, enumerable: true, configurable: false} | ||
Object.isExtensible(myEnum); //=> false | ||
myEnum instanceof Enum; //=> true | ||
//Instances of Enum created with 'new' from similar objects are not equal | ||
myEnum1=new Enum({'A':1, 'B':2, 'C':4}); | ||
myEnum2=new Enum({'A':1, 'B':2, 'C':4}); | ||
myEnum1 == myEnum2 //=> false | ||
myEnum1.A == myEnum2.A //=> false | ||
myEnum1.A.value == myEnum2.A.value //=> true | ||
//Instances of Enum created with 'new' from similar objects are not equal | ||
myEnum1=new Enum({'A':1, 'B':2, 'C':4}); | ||
myEnum2=new Enum({'A':1, 'B':2, 'C':4}); | ||
myEnum1 == myEnum2 //=> false | ||
myEnum1.A == myEnum2.A //=> false | ||
myEnum1.A.value == myEnum2.A.value //=> true | ||
//This enum object has no properties other than those defined during its creation. Existing Data is 'Persistent' and preserves the original version | ||
myEnum.B.value; //=> 2 | ||
myEnum.B = 5; //=> Throws TypeError | ||
delete myEnum.B; //=> false | ||
myEnum.D = 6; //=> doesn't add to the enum object, silently ignores | ||
myEnum.D; // undefined | ||
//This enum object has no properties other than those defined during its creation. Existing Data is 'Persistent' and preserves the original version | ||
myEnum.B.value; //=> 2 | ||
myEnum.B = 5; //=> Throws TypeError | ||
delete myEnum.B; //=> false | ||
myEnum.D = 6; //=> doesn't add to the enum object, silently ignores | ||
myEnum.D; // undefined | ||
//Try to define new property throws TypeError | ||
Object.defineProperty(myEnum, D, {value:6, writable:false, enumerable:true}); | ||
//=>TypeError: Cannot define property:D, object is not extensible. | ||
//Try to define new property throws TypeError | ||
Object.defineProperty(myEnum, D, {value:6, writable:false, enumerable:true}); | ||
//=>TypeError: Cannot define property:D, object is not extensible. | ||
```` | ||
# License | ||
@@ -175,0 +180,0 @@ |
var expect = expect || require('expect.js'), | ||
endianness = this.Enum ? 'LE' : require('os').endianness(), | ||
e = this.Enum || require('../index'); | ||
@@ -150,4 +151,4 @@ | ||
expect(myEnum.A == 'A').to.be.ok(); | ||
expect(myEnum.C == 'C').to.be.ok(); | ||
expect(myEnum.A == myEnum.A.value).to.be.ok(); | ||
expect(myEnum.C == myEnum.C.value).to.be.ok(); | ||
@@ -313,8 +314,18 @@ }); | ||
it('call valueOf and get the key', function() { | ||
it('call valueOf and get the value', function() { | ||
expect(myEnum.A.valueOf()).to.eql('A'); | ||
expect(myEnum.A.valueOf()).to.eql(myEnum.A.value); | ||
}); | ||
it('use JavaScript | operator', function() { | ||
expect(myEnum.A | myEnum.B).to.eql(myEnum.getValue('A | B')); | ||
expect(myEnum.A | myEnum.C).to.eql(myEnum.getValue('A | C')); | ||
expect(myEnum.A | myEnum.B | myEnum.C).to.eql(myEnum.getValue('A | B | C')); | ||
}); | ||
it('stringify JSON', function() { | ||
@@ -481,4 +492,46 @@ | ||
describe('ref Type interface', function () { | ||
it('should define a `size` Number', function () { | ||
var myEnum = new e(['A', 'B', 'C']); | ||
expect(myEnum.size).to.be.a('number'); | ||
}); | ||
it('should define an `indirection` Number', function () { | ||
var myEnum = new e(['A', 'B', 'C']); | ||
expect(myEnum.indirection).to.be.a('number'); | ||
}); | ||
it('should work with Buffer for `get()`', function () { | ||
var myEnum = new e(['A', 'B', 'C']); | ||
var buffer = new Buffer(myEnum.size); | ||
buffer['writeUInt32' + endianness](myEnum.B.value, 0); | ||
expect(myEnum.get(buffer)).to.eql(myEnum.B); | ||
}); | ||
it('should work with Buffer for `set()`', function () { | ||
var myEnum = new e(['A', 'B', 'C']); | ||
var buffer = new Buffer(myEnum.size); | ||
myEnum.set(buffer, 0, myEnum.B); | ||
expect(buffer['readUInt32' + endianness](0)).to.eql(myEnum.B.value); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
229904
3768
195
6
7
1