chaining-tool
Advanced tools
+1
-1
| { | ||
| "name": "chaining-tool", | ||
| "main": "index.js", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "homepage": "https://github.com/maxazan/chaining-tool", | ||
@@ -6,0 +6,0 @@ "authors": [ |
+8
-2
@@ -14,7 +14,13 @@ /** | ||
| /** | ||
| * Add handler to the end of chain | ||
| * Add handler or handlers to the end of chain | ||
| * @param {function} handler | ||
| */ | ||
| Chain.prototype.add = function(handler) { | ||
| this.handlers.push(handler); | ||
| if(typeof(handler)=="function"){ | ||
| this.handlers.push(handler); | ||
| }else{ | ||
| for (var i = 0; i < handler.length; i++) { | ||
| this.handlers.push(handler[i]); | ||
| } | ||
| } | ||
| }; | ||
@@ -21,0 +27,0 @@ |
+1
-1
| { | ||
| "name": "chaining-tool", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "description": "Chain of responsibility", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+62
-0
@@ -89,2 +89,64 @@ var assert = require("assert"); | ||
| }); | ||
| it('add method test', function(done) { | ||
| var handlers = []; | ||
| handlers[0] = function(context, next) { | ||
| context.request.test = 1; | ||
| next(); | ||
| }; | ||
| handlers[1] = function(context, next) { | ||
| next(); | ||
| }; | ||
| another_handler = function(context, next) { | ||
| context.request.test = 2; | ||
| next(); | ||
| }; | ||
| var context = { | ||
| request: {}, | ||
| response: {} | ||
| }; | ||
| var oncomplete = function(context) { | ||
| assert.equal(context.request.test, 2); | ||
| done(); | ||
| }; | ||
| var oninterrupt = function(context) { | ||
| assert.ok(false); | ||
| }; | ||
| var chain = new Chain(handlers); | ||
| chain.add(another_handler); | ||
| chain.start(context, oncomplete, oninterrupt); | ||
| }); | ||
| it('add array method test', function(done) { | ||
| var handlers = []; | ||
| handlers[0] = function(context, next) { | ||
| context.request.test = 1; | ||
| next(); | ||
| }; | ||
| var another_handler = []; | ||
| another_handler[0] = function(context, next) { | ||
| next(); | ||
| }; | ||
| another_handler[1] = function(context, next) { | ||
| context.request.test = 2; | ||
| next(); | ||
| }; | ||
| var context = { | ||
| request: {}, | ||
| response: {} | ||
| }; | ||
| var oncomplete = function(context) { | ||
| assert.equal(context.request.test, 2); | ||
| done(); | ||
| }; | ||
| var oninterrupt = function(context) { | ||
| assert.ok(false); | ||
| }; | ||
| var chain = new Chain(handlers); | ||
| chain.add(another_handler); | ||
| chain.start(context, oncomplete, oninterrupt); | ||
| }); | ||
| it('memory usage test', function(done) { | ||
@@ -91,0 +153,0 @@ var handlers = []; |
7333
36.23%242
35.2%