Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helpers): add compose feature (#1660)
- Loading branch information
1 parent
a078fa0
commit eb60ef4
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
/* eslint-disable func-names */ | ||
|
||
var Chain = require('../chain'); | ||
var _ = require('lodash'); | ||
|
||
module.exports = composeHandlerChain; | ||
|
||
/** | ||
* Builds a function with the signature of a handler | ||
* function(req,resp,callback). | ||
* which internally executes the passed in array of handler function as a chain. | ||
* | ||
* @param {Array} [handlers] - handlers Array of | ||
* function(req,resp,callback) handlers. | ||
* @param {Object} [options] - options Optional option object that is | ||
* passed to Chain. | ||
* @returns {Function} Handler function that executes the handler chain when run | ||
*/ | ||
function composeHandlerChain(handlers, options) { | ||
var chain = new Chain(options); | ||
if (_.isArray(handlers)) { | ||
handlers = _.flattenDeep(handlers); | ||
handlers.forEach(function(handler) { | ||
chain.add(handler); | ||
}); | ||
} else { | ||
chain.add(handlers); | ||
} | ||
|
||
return function handlerChain(req, resp, callback) { | ||
chain.run(req, resp, callback); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
/* eslint-disable func-names */ | ||
|
||
if (require.cache[__dirname + '/lib/helper.js']) { | ||
delete require.cache[__dirname + '/lib/helper.js']; | ||
} | ||
var helper = require('./lib/helper.js'); | ||
|
||
///--- Globals | ||
|
||
var test = helper.test; | ||
var composer = require('../lib/helpers/chainComposer'); | ||
|
||
test('chainComposer creates a valid chain for a handler array ', function(t) { | ||
var counter = 0; | ||
var handlers = []; | ||
handlers.push(function(req, res, next) { | ||
counter++; | ||
next(); | ||
}); | ||
|
||
handlers.push(function(req, res, next) { | ||
counter++; | ||
next(); | ||
}); | ||
|
||
var chain = composer(handlers); | ||
chain( | ||
{ | ||
startHandlerTimer: function() {}, | ||
endHandlerTimer: function() {}, | ||
closed: function() { | ||
return false; | ||
} | ||
}, | ||
{}, | ||
function() { | ||
t.equal(counter, 2); | ||
t.done(); | ||
} | ||
); | ||
}); | ||
|
||
test('chainComposer creates a valid chain for a single handler', function(t) { | ||
var counter = 0; | ||
var handlers = function(req, res, next) { | ||
counter++; | ||
next(); | ||
}; | ||
|
||
var chain = composer(handlers); | ||
chain( | ||
{ | ||
startHandlerTimer: function() {}, | ||
endHandlerTimer: function() {}, | ||
closed: function() { | ||
return false; | ||
} | ||
}, | ||
{}, | ||
function() { | ||
t.equal(counter, 1); | ||
t.done(); | ||
} | ||
); | ||
}); |