diff --git a/lib/middleware-chain.js b/lib/middleware-chain.js index d0f88f9..79ff48b 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,6 +25,10 @@ module.exports = function(context, chain) { chain = context; context = {}; } + + //flatten chain + if(Array.isArray(chain)) + chain=flatten(chain); // Context should be an object. if (typeof context !== 'object' || util.isArray(context)) { @@ -52,4 +57,4 @@ module.exports = function(context, chain) { // Start the chain. return next(); -}; \ No newline at end of file +}; 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