Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

enum

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enum - npm Package Compare versions

Comparing version 0.2.6 to 0.2.7

bower.json

54

lib/enum.js

@@ -17,2 +17,5 @@ (function (root, module, global, define) {

/*constructor reference so that, this.constructor===EnumItem//=>true */
constructor: EnumItem,
/**

@@ -126,2 +129,3 @@ * Checks if the flagged EnumItem has the passing object.

this.isFlaggable = isFlaggable();
this.freezeEnums(); //this will make instances of Enum non-extensible
}

@@ -131,2 +135,5 @@

/*constructor reference so that, this.constructor===Enum//=>true */
constructor: Enum,
/**

@@ -220,4 +227,47 @@ * Returns the appropriate EnumItem key.

}
}
},
/**
* 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;
}
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);
}
}
deepFreezeEnums(this);
return this;
},
};

@@ -255,2 +305,2 @@

typeof(define) !== 'undefined' ? define : undefined
));
));

6

package.json
{
"author": "adrai",
"name": "enum",
"version": "0.2.6",
"version": "0.2.7",
"private": false,

@@ -38,5 +38,5 @@ "main": "index.js",

],
"scripts" : {
"scripts" : {
"test" : "mocha"
}
}
}
# Introduction
[![Build Status](https://secure.travis-ci.org/adrai/enum.png)](http://travis-ci.org/adrai/enum)
[![travis](https://img.shields.io/travis/adrai/enum.svg)](https://travis-ci.org/adrai/enum) [![npm](https://img.shields.io/npm/v/enum.svg)](https://npmjs.org/package/enum)
Enum is a javascript module that introduces the Enum Type. It works for node.js and in the browser.

@@ -15,4 +16,4 @@

|:------------|:----------------|:---------|
| `enum-0.2.6.js` | *uncompressed, with comments* | [Download](https://raw.github.com/adrai/enum/master/enum-0.2.6.js) |
| `enum-0.2.6.min.js` | *compressed, without comments* | [Download](https://raw.github.com/adrai/enum/master/enum-0.2.6.min.js) |
| `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) |

@@ -36,8 +37,23 @@ # Installation (node.js)

// 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
for(var i=1; i<8; i++){ console.log(myEnum.get(i).value + '=> '+ myEnum.get(i).key)}
1=> Black
2=> Red
3=> Black | Red
4=> Green
5=> Black | Green
6=> Red | Green
7=> Black | Red | Green
// define an enum with own values
var myEnum = new Enum({'A': 1, 'B': 2, 'C': 4});
// if defining an flaggable enum, you can define your own separator (default is ' | ')
// if defining a flaggable enum, you can define your own separator (default is ' | ')
var myEnum = new Enum(['A', 'B', 'C'], { separator: ' | ' });

@@ -51,3 +67,15 @@

//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
1=> Black
2=> Red
3=> Red2
4=> Green
5=> Blue
// get your item

@@ -114,9 +142,35 @@ myEnum.A

myItem.valueOf() // 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
//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
//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
Copyright (c) 2014 Adriano Raiano
Copyright (c) 2015 Adriano Raiano

@@ -139,2 +193,2 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.

@@ -328,2 +328,73 @@ var expect = expect || require('expect.js'),

describe('on an enum object', function(){
var myEnum;
before(function(){
myEnum = new e({'A':1, 'B':2, 'C':4});
});
it('can not extend after creation', function() {
var extendMyEnum = Object.isExtensible(myEnum);
expect(extendMyEnum).to.be(false);
});
it('does not accept changes to existing property values, throws', function() {
expect(myEnum).to.have.property('C');
expect(function() {
myEnum['C'] = 3;
}).to.throwError("The value can not be set; Enum Type is not extensible.");
expect(function() {
Object.defineProperty(myEnum, 'C', {value: 3, writable:true, configurable: true});
}).to.throwError();
expect(myEnum.get('C')).to.have.property('value', 4);
expect(myEnum).to.equal(myEnum);
});
it('can not define new properties, throws', function() {
expect(function() {
Object.defineProperty(myEnum, 'D', {writable: true, enumerable:true});
}).to.throwError();
expect(myEnum.D).to.be(undefined);
expect(myEnum).not.to.have.property('D');
expect(myEnum).to.equal(myEnum);
});
it('is persistent to deletes', function() {
var deleteEnumItem = delete myEnum['A'];
expect(deleteEnumItem).to.be(false);
expect(myEnum).to.have.property('A');
expect(myEnum.get('A')).to.have.property('value', 1);
expect(myEnum).to.equal(myEnum);
});
it('creates unique identity for each property', function() {
var myEnum1 = new e({'A':1, 'B':2, 'C':4});
var myEnum2 = new e({'A':1, 'B':2, 'C':4});
expect(myEnum1.A).not.to.equal(myEnum2.A);
expect(myEnum1.B).not.to.equal(myEnum2.B);
expect(myEnum1.C).not.to.equal(myEnum2.C);
expect(myEnum1).not.to.equal(myEnum2);
});
it('respects the order of properties for equality', function() {
var m1 = Object.keys(myEnum);
var m2 = Object.keys(myEnum).reverse();
expect(m1).not.to.equal(m2);
});
});
describe('beeing flagged', function() {

@@ -411,2 +482,2 @@

});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc