From 8d0275cfa4f6a907531fdc18e8b39e2bd3d112ec Mon Sep 17 00:00:00 2001 From: Utkarsh85 Date: Mon, 19 Sep 2016 03:19:39 +0530 Subject: [PATCH 1/2] Add chain flattening code --- lib/middleware-chain.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/middleware-chain.js b/lib/middleware-chain.js index d0f88f9..bbd1c1f 100644 --- a/lib/middleware-chain.js +++ b/lib/middleware-chain.js @@ -9,6 +9,7 @@ // Module dependencies. var util = require('util'); +var flatten = require('array-flatten'); /** * Middleware Chain module. @@ -24,7 +25,10 @@ module.exports = function(context, chain) { chain = context; context = {}; } - + + //flatten chain + chain=flatten(chain); + // Context should be an object. if (typeof context !== 'object' || util.isArray(context)) { throw new Error('Context must be an object.') @@ -52,4 +56,4 @@ module.exports = function(context, chain) { // Start the chain. return next(); -}; \ No newline at end of file +}; From 437bb28ecb7d26286ccd810969316613e70f0049 Mon Sep 17 00:00:00 2001 From: Utkarsh85 Date: Mon, 19 Sep 2016 03:28:17 +0530 Subject: [PATCH 2/2] Added nested chain test --- lib/middleware-chain.js | 5 +++-- package.json | 3 +++ test/index.js | 11 +++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/middleware-chain.js b/lib/middleware-chain.js index bbd1c1f..79ff48b 100644 --- a/lib/middleware-chain.js +++ b/lib/middleware-chain.js @@ -27,8 +27,9 @@ module.exports = function(context, chain) { } //flatten chain - chain=flatten(chain); - + if(Array.isArray(chain)) + chain=flatten(chain); + // Context should be an object. if (typeof context !== 'object' || util.isArray(context)) { throw new Error('Context must be an object.') diff --git a/package.json b/package.json index 025124a..9146da5 100644 --- a/package.json +++ b/package.json @@ -20,5 +20,8 @@ "devDependencies": { "consign": "^0.1.1", "mocha": "^2.2.5" + }, + "dependencies": { + "array-flatten": "^2.1.0" } } diff --git a/test/index.js b/test/index.js index 51af86e..a099321 100644 --- a/test/index.js +++ b/test/index.js @@ -158,3 +158,14 @@ describe('Middleware Chain', function() { }); +// Flatten Test +describe('Flatten Test',function () { + it("should work with nested arrays",function (done) { + chain([one, [two], [two,three], function(context, next) { + assert.deepEqual(context, { + one: 'Hello', two: 'Hello', three: 'Hello' + }); + done(); + }]); + }) +}) \ No newline at end of file