Comparing version 1.0.0 to 1.1.0
@@ -11,2 +11,3 @@ var gulp = require('gulp'); | ||
.pipe(browserify({ | ||
standalone: 'Enum', | ||
insertGlobals : true, | ||
@@ -13,0 +14,0 @@ debug : !gulp.env.production |
@@ -5,2 +5,19 @@ "use strict"; | ||
function isType(type, value) { | ||
return typeof(value) === type; | ||
} | ||
function isObject(value) { | ||
return isType('object', value); | ||
} | ||
function isString(value) { | ||
return isType('string', value); | ||
} | ||
function isEnumItem(value) { | ||
return (value instanceof EnumItem || (isObject(value) && value.key !== undefined && value.value !== undefined)); | ||
} | ||
/** | ||
@@ -11,5 +28,8 @@ * Represents an Item of an Enum. | ||
*/ | ||
function EnumItem(key, value) { | ||
function EnumItem(key, value, options) { | ||
this.key = key; | ||
this.value = value; | ||
this._options = options || {}; | ||
this._options.ignoreCase = this._options.ignoreCase || false; | ||
} | ||
@@ -28,5 +48,8 @@ | ||
has: function(value) { | ||
if (value instanceof EnumItem || (typeof(value) === 'object' && value.key !== undefined && value.value !== undefined)) { | ||
if (isEnumItem(value)) { | ||
return (this.value & value.value) !== 0; | ||
} else if (typeof(value) === 'string') { | ||
} else if (isString(value)) { | ||
if (this._options.ignoreCase) { | ||
return this.key.toLowerCase().indexOf(value.toLowerCase()) >= 0; | ||
} | ||
return this.key.indexOf(value) >= 0; | ||
@@ -44,5 +67,8 @@ } else { | ||
is: function(key) { | ||
if (key instanceof EnumItem || (typeof(key) === 'object' && key.key !== undefined && key.value !== undefined)) { | ||
if (isEnumItem(key)) { | ||
return this.key === key.key; | ||
} else if (typeof(key) === 'string') { | ||
} else if (isString(key)) { | ||
if (this._options.ignoreCase) { | ||
return this.key.toLowerCase() === key.toLowerCase(); | ||
} | ||
return this.key === key; | ||
@@ -88,3 +114,3 @@ } else { | ||
if (options && typeof(options) === 'string') { | ||
if (options && isString(options)) { | ||
options = { name: options }; | ||
@@ -96,2 +122,3 @@ } | ||
this._options.endianness = this._options.endianness || endianness; | ||
this._options.ignoreCase = this._options.ignoreCase || false; | ||
@@ -113,6 +140,16 @@ this.enums = []; | ||
} | ||
this[member] = new EnumItem(member, map[member]); | ||
this[member] = new EnumItem(member, map[member], { ignoreCase: this._options.ignoreCase }); | ||
this.enums.push(this[member]); | ||
} | ||
if (this._options.ignoreCase) { | ||
this.getLowerCaseEnums = function () { | ||
var res = {}; | ||
for (var i = 0, len = this.enums.length; i < len; i++) { | ||
res[this.enums[i].key.toLowerCase()] = this.enums[i]; | ||
} | ||
return res; | ||
}; | ||
} | ||
if (this._options.name) { | ||
@@ -194,3 +231,3 @@ this.name = this._options.name; | ||
if (key instanceof EnumItem || (typeof(key) === 'object' && key.key !== undefined && key.value !== undefined)) { | ||
if (isEnumItem(key)) { | ||
var foundIndex = this.enums.indexOf(key); | ||
@@ -204,3 +241,10 @@ if (foundIndex >= 0) { | ||
return this.get(key.key); | ||
} else if (typeof(key) === 'string') { | ||
} else if (isString(key)) { | ||
var enums = this; | ||
if (this._options.ignoreCase) { | ||
enums = this.getLowerCaseEnums(); | ||
key = key.toLowerCase(); | ||
} | ||
if (key.indexOf(this._options.separator) > 0) { | ||
@@ -210,6 +254,6 @@ var parts = key.split(this._options.separator); | ||
var value = 0; | ||
for(var i = 0; i < parts.length; i++) { | ||
for (var i = 0; i < parts.length; i++) { | ||
var part = parts[i]; | ||
value |= this[part].value; | ||
value |= enums[part].value; | ||
} | ||
@@ -219,3 +263,3 @@ | ||
} else { | ||
return this[key]; | ||
return enums[key]; | ||
} | ||
@@ -324,6 +368,2 @@ } else { | ||
if (typeof (window) !== 'undefined') { | ||
window.Enum = Enum; | ||
} | ||
module.exports = Enum; |
{ | ||
"author": "adrai", | ||
"name": "enum", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
@@ -8,2 +8,4 @@ # Introduction | ||
...and [ref](https://github.com/TooTallNate/ref) compatible [Known Types](https://github.com/TooTallNate/ref/wiki/Known-%22types%22) | ||
# Dependencies | ||
@@ -17,4 +19,4 @@ No dependencies! | ||
|:------------|:----------------|:---------| | ||
| `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) | | ||
| `enum-1.1.0.js` | *uncompressed, with comments* | [Download](https://raw.github.com/adrai/enum/master/enum-1.1.0.js) | | ||
| `enum-1.1.0.min.js` | *compressed, without comments* | [Download](https://raw.github.com/adrai/enum/master/enum-1.1.0.min.js) | | ||
@@ -65,2 +67,5 @@ # Installation (node.js) | ||
// or | ||
var myEnum = new Enum(['A', 'B', 'C'], 'MyEnum'); | ||
// if you want your enum to have an explicit "endianness", define it in the options | ||
@@ -70,4 +75,8 @@ // (defaults to `os.endianness()`) | ||
// or | ||
var myEnum = new Enum(['A', 'B', 'C'], 'MyEnum'); | ||
// if you want your enum to be not case sensitive | ||
// (defaults to `false`) | ||
var myEnum = new Enum(['One', 'tWo', 'ThrEE'], { ignoreCase: true }); | ||
myEnum.get('one'); // => myEnum.One | ||
myEnum.get('TWO'); // => myEnum.tWo | ||
myEnum.ThrEE.is('three'); // => true | ||
@@ -74,0 +83,0 @@ //define enum type without flag |
@@ -532,4 +532,26 @@ var expect = expect || require('expect.js'), | ||
describe('beeing not case sensitive', function() { | ||
var myEnum = new e(['One', 'tWo', 'ThrEE'], { ignoreCase: true }); | ||
it('it should work correctly even if not requesting exactly the same key value', function() { | ||
expect(myEnum.get('one').value).to.eql(myEnum.One.value); | ||
expect(myEnum.get('two').value).to.eql(myEnum.tWo.value); | ||
expect(myEnum.get('THREE').value).to.eql(myEnum.ThrEE.value); | ||
expect(myEnum.One.is('onE')).to.eql(true); | ||
expect(myEnum.tWo.is('Two')).to.eql(true); | ||
expect(myEnum.ThrEE.is('three')).to.eql(true); | ||
expect(myEnum.One.has('onE')).to.eql(true); | ||
expect(myEnum.tWo.has('Two')).to.eql(true); | ||
expect(myEnum.ThrEE.has('three')).to.eql(true); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
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
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
235122
3855
204
0