Skip to content

Commit

Permalink
feat(conditional): Allow additional arguments to be used (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotttf committed Jan 10, 2018
1 parent a1a031f commit fa02fc6
Show file tree
Hide file tree
Showing 4 changed files with 3,552 additions and 12 deletions.
12 changes: 5 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ notifications:
email: false
node_js:
- '6'
before_install:
- npm i -g npm@^2.0.0
before_script:
- npm prune
script: npm run lint && npm t
script:
- yarn lint
- yarn test
after_success:
- npm run coveralls
- npm run semantic-release
- yarn coveralls
- yarn semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
7 changes: 3 additions & 4 deletions lib/conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ const once = require('once');
* }
* ));
*/
module.exports = (condition, success, fail) => (req, res, next) => {
module.exports = (condition, success, fail) => (req, res, next, ...rest) => {
const nextOnce = once(next);
if (condition === true || (typeof condition === 'function' && condition(req, res, nextOnce))) {
return success(req, res, nextOnce);
return success(req, res, nextOnce, ...rest);
}
if (fail) {
return fail(req, res, nextOnce);
return fail(req, res, nextOnce, ...rest);
}

return nextOnce();
};

13 changes: 12 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ module.exports = {
});
},

boolAdditional(test) {
test.expect(2);
const middleware = conditional(true, (req, res, next, extra) => {
test.ok(true, 'true conditional worked.');
test.equal(extra, 'extra received');
next();
});
middleware({}, {}, () => {
test.done();
}, 'extra received');
},

func(test) {
test.expect(3);

Expand Down Expand Up @@ -82,4 +94,3 @@ module.exports = {
});
},
};

0 comments on commit fa02fc6

Please sign in to comment.