eventproxy
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -9,14 +9,13 @@ /*global exports */ | ||
;(function (name, definition) { | ||
var theModule = definition(), | ||
// this is considered "safe": | ||
hasDefine = typeof define === 'function' && define.amd, | ||
// this is considered "safe": | ||
var hasDefine = typeof define === 'function', | ||
// hasDefine = typeof define === 'function', | ||
hasExports = typeof module !== 'undefined' && module.exports; | ||
if (hasDefine) { // AMD Module | ||
define(theModule); | ||
if (hasDefine) { // AMD Module or CMD Module | ||
define(definition); | ||
} else if (hasExports) { // Node.js Module | ||
module.exports = theModule; | ||
module.exports = definition(); | ||
} else { // Assign to common namespaces or simply the global object (window) | ||
this[name] = theModule; | ||
this[name] = definition(); | ||
} | ||
@@ -23,0 +22,0 @@ })('EventProxy', function () { |
@@ -13,6 +13,7 @@ { | ||
"devDependencies": { | ||
"expresso": ">= 0.9.2" | ||
"mocha": "*", | ||
"chai": "*" | ||
}, | ||
"scripts": { | ||
"test": "expresso test/eventproxy.test.js" | ||
"test": "mocha test/test.js" | ||
}, | ||
@@ -23,4 +24,3 @@ "repository" : { | ||
}, | ||
"main" : "eventproxy.js", | ||
"version" : "0.1.3" | ||
"version" : "0.1.4" | ||
} |
292
test/test.js
@@ -1,183 +0,223 @@ | ||
test("EventProxy - bind/trigger", function() { | ||
var obj = new EventProxy(); | ||
var assert = require('chai').assert; | ||
var EventProxy = require('../lib/eventproxy'); | ||
describe("EventProxy", function () { | ||
it('create on line ways', function () { | ||
var counter = 0; | ||
obj.bind("event", function(data) { | ||
var ep = EventProxy.create('event', function (data) { | ||
counter += 1; | ||
assert.deepEqual(data, 'event data'); | ||
}); | ||
ep.emit("event", 'event data'); | ||
assert.equal(counter, 1, 'Counter should be incremented.'); | ||
}); | ||
it('bind/trigger', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
ep.bind("event", function (data) { | ||
counter += 1; | ||
}); | ||
obj.trigger("event"); | ||
equals(counter, 1, 'Counter should be incremented.'); | ||
obj.trigger("event"); | ||
equals(counter, 2, 'Counter should be incremented.'); | ||
obj.trigger("event"); | ||
equals(counter, 3, 'Counter should be incremented.'); | ||
obj.trigger("event"); | ||
equals(counter, 4, 'Counter should be incremented.'); | ||
}); | ||
ep.trigger("event"); | ||
assert.equal(counter, 1, 'Counter should be incremented.'); | ||
ep.trigger("event"); | ||
assert.equal(counter, 2, 'Counter should be incremented.'); | ||
ep.trigger("event"); | ||
assert.equal(counter, 3, 'Counter should be incremented.'); | ||
ep.trigger("event"); | ||
assert.equal(counter, 4, 'Counter should be incremented.'); | ||
}); | ||
test("EventProxy - bind, then unbind all functions", function () { | ||
var obj = new EventProxy(); | ||
it('bind, then unbind all functions', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
obj.bind('event', function() { | ||
ep.bind('event', function () { | ||
counter += 1; | ||
}); | ||
obj.trigger('event'); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
obj.unbind('event'); | ||
obj.trigger('event'); | ||
equals(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
ep.trigger('event'); | ||
assert.equal(counter, 1, 'counter should be incremented.'); | ||
ep.unbind('event'); | ||
ep.trigger('event'); | ||
assert.equal(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
test("EventProxy - once/trigger", function () { | ||
var obj = new EventProxy(); | ||
it('once/trigger', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
obj.once('event', function() { | ||
ep.once('event', function () { | ||
counter += 1; | ||
}); | ||
obj.trigger('event'); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
obj.trigger('event'); | ||
equals(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
ep.trigger('event'); | ||
assert.equal(counter, 1, 'counter should be incremented.'); | ||
ep.trigger('event'); | ||
assert.equal(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
test("EventProxy - immediate", function () { | ||
var obj = new EventProxy(); | ||
it('immediate', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
obj.immediate('event', function (){ | ||
ep.immediate('event', function (){ | ||
counter +=1; | ||
}); | ||
equals(counter, 1, "counter should be incremented."); | ||
obj.trigger('event'); | ||
equals(counter, 2, "counter should be incremented."); | ||
}); | ||
assert.equal(counter, 1, "counter should be incremented."); | ||
ep.trigger('event'); | ||
assert.equal(counter, 2, "counter should be incremented."); | ||
}); | ||
test("EventProxy - immediate/parameter", function () { | ||
var obj = new EventProxy(); | ||
var param = 0; | ||
obj.immediate('event', function (data){ | ||
equals(data, param, "data should same as param."); | ||
it('immediate/parameter', function () { | ||
var ep = EventProxy.create(); | ||
var param = new Date(), counter = 0; | ||
ep.immediate('event', function (data) { | ||
assert.equal(data, param, "data should same as param."); | ||
counter += 1; | ||
}, param); | ||
}); | ||
assert.equal(counter, 1, "counter should be incremented."); | ||
ep.trigger('event', param); | ||
assert.equal(counter, 2, "counter should be incremented."); | ||
}); | ||
test("EventProxy - assign one event", function () { | ||
var obj = new EventProxy(); | ||
it('assign one event', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
obj.assign('event', function() { | ||
ep.assign('event', function () { | ||
counter += 1; | ||
}); | ||
obj.trigger('event'); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
obj.trigger('event'); | ||
equals(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
ep.trigger('event'); | ||
assert.equal(counter, 1, 'counter should be incremented.'); | ||
ep.trigger('event'); | ||
assert.equal(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
test("EventProxy - assign two events", function () { | ||
var obj = new EventProxy(); | ||
it('assign two events', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
obj.assign('event1', 'event2', function(event1, event2) { | ||
equals(event1, 'event1', 'counter should not be incremented.'); | ||
equals(event2, 'event2', 'counter should not be incremented.'); | ||
ep.assign('event1', 'event2', function (event1, event2) { | ||
assert.equal(event1, 'event1', 'counter should not be incremented.'); | ||
assert.equal(event2, 'event2', 'counter should not be incremented.'); | ||
counter += 1; | ||
}); | ||
obj.trigger('event1', 'event1'); | ||
equals(counter, 0, 'counter should not be incremented.'); | ||
obj.trigger('event2', 'event2'); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
obj.trigger('event2'); | ||
equals(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
ep.trigger('event1', 'event1'); | ||
assert.equal(counter, 0, 'counter should not be incremented.'); | ||
ep.trigger('event2', 'event2'); | ||
assert.equal(counter, 1, 'counter should be incremented.'); | ||
ep.trigger('event2'); | ||
assert.equal(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
it('assign two events with array events', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
var events = ['event1', 'event2']; | ||
ep.assign(events, function (event1, event2) { | ||
assert.equal(event1, 'event1', 'counter should not be incremented.'); | ||
assert.equal(event2, 'event2', 'counter should not be incremented.'); | ||
counter += 1; | ||
}); | ||
ep.trigger('event1', 'event1'); | ||
assert.equal(counter, 0, 'counter should not be incremented.'); | ||
ep.trigger('event2', 'event2'); | ||
assert.equal(counter, 1, 'counter should be incremented.'); | ||
ep.trigger('event2'); | ||
assert.equal(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
test("EventProxy - assignAlways", function () { | ||
var obj = new EventProxy(); | ||
it('assignAlways', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
var event2 = null; | ||
obj.assignAlways('event1', 'event2', function(data1, data2) { | ||
ep.assignAlways('event1', 'event2', function (data1, data2) { | ||
counter += 1; | ||
equals(data2, event2, 'Second data should same as event2.'); | ||
assert.equal(data1, 'event1'); | ||
assert.equal(data2, event2, 'Second data should same as event2.'); | ||
}); | ||
obj.trigger('event1', 'event1'); | ||
equals(counter, 0, 'counter should not be incremented.'); | ||
ep.trigger('event1', 'event1'); | ||
assert.equal(counter, 0, 'counter should not be incremented.'); | ||
event2 = "event2_1"; | ||
obj.trigger('event2', event2); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
ep.trigger('event2', event2); | ||
assert.equal(counter, 1, 'counter should be incremented.'); | ||
event2 = "event2_2"; | ||
obj.trigger('event2', event2); | ||
equals(counter, 2, 'counter should be incremented.'); | ||
obj.trigger('event3', 'This event not in list'); | ||
equals(counter, 2, 'counter should not be incremented.'); | ||
}); | ||
ep.trigger('event2', event2); | ||
assert.equal(counter, 2, 'counter should be incremented.'); | ||
ep.trigger('event3', "The event not in list"); | ||
assert.equal(counter, 2, 'counter should not be incremented.'); | ||
}); | ||
test("EventProxy - after, 5 times", function () { | ||
var obj = new EventProxy(); | ||
it('after, n times', function () { | ||
var ep = EventProxy.create(); | ||
var n = Math.round(Math.random() * 100) + 1; | ||
var counter = 0; | ||
obj.after('event', 5, function(data) { | ||
equals(data.join(","), "1,2,3,4,5", 'Return array should be 1,2,3,4,5'); | ||
ep.after('event', n, function (data) { | ||
assert.deepEqual(data.length, n); | ||
for(var i = 0, l = data.length; i < l; i++) { | ||
assert.deepEqual(data[i], i); | ||
} | ||
counter += 1; | ||
}); | ||
obj.trigger('event', 1); | ||
equals(counter, 0, 'counter should not be incremented.'); | ||
obj.trigger('event', 2); | ||
equals(counter, 0, 'counter should not be incremented.'); | ||
obj.trigger('event', 3); | ||
equals(counter, 0, 'counter should not be incremented.'); | ||
obj.trigger('event', 4); | ||
equals(counter, 0, 'counter should not be incremented.'); | ||
obj.trigger('event', 5); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
obj.trigger('event', 6); | ||
equals(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
for(var i = 0, last = n - 1; i < n; i++) { | ||
ep.trigger('event', i); | ||
if (i !== last) { | ||
assert.deepEqual(counter, 0, 'counter should not be incremented.'); | ||
} else { | ||
assert.deepEqual(counter, 1, 'counter should be incremented.'); | ||
} | ||
} | ||
ep.trigger('event', n); | ||
assert.deepEqual(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
test("EventProxy - after, 1 time", function() { | ||
var obj = EventProxy.create(); | ||
it('after, 1 time', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
obj.after('event', 1, function(data) { | ||
equals(data.length, 1, "The data length should be 1."); | ||
equals(data[0], "1 time", "The item should be 1 time."); | ||
ep.after('event', 1, function (data) { | ||
assert.deepEqual(data.length, 1); | ||
assert.deepEqual(data[0], "1 time"); | ||
counter += 1; | ||
}); | ||
obj.trigger('event', "1 time"); | ||
equals(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
ep.trigger('event', "1 time"); | ||
assert.deepEqual(counter, 1, 'counter should have only been incremented once.'); | ||
}); | ||
test("EventProxy - after, 0 time", function () { | ||
it('after, 0 time', function () { | ||
var obj = new EventProxy(); | ||
var counter = 0; | ||
obj.after('event', 0, function(data) { | ||
equals(data.join(","), "", 'Return array should be []'); | ||
obj.after('event', 0, function (data) { | ||
assert.deepEqual(data.join(","), "", 'Return array should be []'); | ||
counter += 1; | ||
}); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
}); | ||
assert.deepEqual(counter, 1, 'counter should be incremented.'); | ||
}); | ||
test("EventProxy - any", function () { | ||
var obj = new EventProxy(); | ||
it('any', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
var eventData1 = "eventData1"; | ||
var eventData2 = "eventData2"; | ||
obj.any('event1', 'event2', function(map) { | ||
equals(map.data, eventData1, 'Return data should be evnetData1.'); | ||
equals(map.eventName, "event1", 'Event name should be event1.'); | ||
counter += 1; | ||
ep.any('event1', 'event2', function (map) { | ||
assert.deepEqual(map.data, eventData1, 'Return data should be evnetData1.'); | ||
assert.deepEqual(map.eventName, "event1", 'Event name should be event1.'); | ||
counter += 1; | ||
}); | ||
obj.trigger('event1', eventData1); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
obj.trigger('event2', 2); | ||
equals(counter, 1, 'counter should not be incremented.'); | ||
}); | ||
ep.trigger('event1', eventData1); | ||
assert.deepEqual(counter, 1, 'counter should be incremented.'); | ||
ep.trigger('event2', eventData2); | ||
assert.deepEqual(counter, 1, 'counter should not be incremented.'); | ||
ep.trigger('event1', eventData1); | ||
assert.deepEqual(counter, 1, 'counter should not be incremented.'); | ||
}); | ||
test("EventProxy - not", function () { | ||
var obj = new EventProxy(); | ||
it('not', function () { | ||
var ep = EventProxy.create(); | ||
var counter = 0; | ||
obj.not('event1', function(data) { | ||
ep.not('event1', function (data) { | ||
counter += 1; | ||
}); | ||
obj.trigger('event1', 1); | ||
equals(counter, 0, 'counter should not be incremented.'); | ||
obj.trigger('event2', 2); | ||
equals(counter, 1, 'counter should be incremented.'); | ||
obj.trigger('event2', 2); | ||
equals(counter, 2, 'counter should be incremented.'); | ||
}); | ||
ep.trigger('event1', 1); | ||
assert.deepEqual(counter, 0, 'counter should not be incremented.'); | ||
ep.trigger('event2', 2); | ||
assert.deepEqual(counter, 1, 'counter should be incremented.'); | ||
ep.trigger('event2', 2); | ||
assert.deepEqual(counter, 2, 'counter should be incremented.'); | ||
}); | ||
}); |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
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
472263
21
9121
2
11
3