Skip to content

Commit eb60ef4

Browse files
paulbakkerhekike
authored andcommitted
feat(helpers): add compose feature (#1660)
1 parent a078fa0 commit eb60ef4

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

lib/helpers/chainComposer.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
/* eslint-disable func-names */
3+
4+
var Chain = require('../chain');
5+
var _ = require('lodash');
6+
7+
module.exports = composeHandlerChain;
8+
9+
/**
10+
* Builds a function with the signature of a handler
11+
* function(req,resp,callback).
12+
* which internally executes the passed in array of handler function as a chain.
13+
*
14+
* @param {Array} [handlers] - handlers Array of
15+
* function(req,resp,callback) handlers.
16+
* @param {Object} [options] - options Optional option object that is
17+
* passed to Chain.
18+
* @returns {Function} Handler function that executes the handler chain when run
19+
*/
20+
function composeHandlerChain(handlers, options) {
21+
var chain = new Chain(options);
22+
if (_.isArray(handlers)) {
23+
handlers = _.flattenDeep(handlers);
24+
handlers.forEach(function(handler) {
25+
chain.add(handler);
26+
});
27+
} else {
28+
chain.add(handlers);
29+
}
30+
31+
return function handlerChain(req, resp, callback) {
32+
chain.run(req, resp, callback);
33+
};
34+
}

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,4 @@ module.exports.createServer = createServer;
108108
module.exports.formatters = require('./formatters');
109109
module.exports.plugins = require('./plugins');
110110
module.exports.pre = require('./plugins').pre;
111+
module.exports.helpers = { compose: require('./helpers/chainComposer') };

test/chainComposer.test.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
/* eslint-disable func-names */
3+
4+
if (require.cache[__dirname + '/lib/helper.js']) {
5+
delete require.cache[__dirname + '/lib/helper.js'];
6+
}
7+
var helper = require('./lib/helper.js');
8+
9+
///--- Globals
10+
11+
var test = helper.test;
12+
var composer = require('../lib/helpers/chainComposer');
13+
14+
test('chainComposer creates a valid chain for a handler array ', function(t) {
15+
var counter = 0;
16+
var handlers = [];
17+
handlers.push(function(req, res, next) {
18+
counter++;
19+
next();
20+
});
21+
22+
handlers.push(function(req, res, next) {
23+
counter++;
24+
next();
25+
});
26+
27+
var chain = composer(handlers);
28+
chain(
29+
{
30+
startHandlerTimer: function() {},
31+
endHandlerTimer: function() {},
32+
closed: function() {
33+
return false;
34+
}
35+
},
36+
{},
37+
function() {
38+
t.equal(counter, 2);
39+
t.done();
40+
}
41+
);
42+
});
43+
44+
test('chainComposer creates a valid chain for a single handler', function(t) {
45+
var counter = 0;
46+
var handlers = function(req, res, next) {
47+
counter++;
48+
next();
49+
};
50+
51+
var chain = composer(handlers);
52+
chain(
53+
{
54+
startHandlerTimer: function() {},
55+
endHandlerTimer: function() {},
56+
closed: function() {
57+
return false;
58+
}
59+
},
60+
{},
61+
function() {
62+
t.equal(counter, 1);
63+
t.done();
64+
}
65+
);
66+
});

0 commit comments

Comments
 (0)