Skip to content

Commit

Permalink
add variadic arg support (#65)
Browse files Browse the repository at this point in the history
* add variadic arg support

ref #63

* remove array check

* fix varargs test
  • Loading branch information
stephenmathieson authored and jonathanong committed Oct 25, 2016
1 parent 95edbe7 commit f881413
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const Promise = require('any-promise')
const flatten = require('lodash/flatten')

/**
* Expose compositor.
Expand All @@ -13,13 +14,13 @@ module.exports = compose
* a fully valid middleware comprised
* of all those which are passed.
*
* @param {Array} middleware
* @param {Array|Function...} middleware
* @return {Function}
* @api public
*/

function compose (middleware) {
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!')
middleware = flatten(arguments)
for (const fn of middleware) {
if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"index.js"
],
"dependencies": {
"any-promise": "^1.1.0"
"any-promise": "^1.1.0",
"lodash": "^4.16.4"
},
"devDependencies": {
"co": "^4.6.0",
Expand Down
18 changes: 10 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ describe('Koa Compose', function () {
})
})

it('should only accept an array', function () {
var err
try {
(compose()).should.throw()
} catch (e) {
err = e
}
return (err).should.be.instanceof(TypeError)
it('should support variadic arguments', function () {
const arr = []
const gen = str => (ctx, next) => new Promise((resolve, reject) => {
arr.push(str)
next()
resolve()
})

const promise = compose(gen('a'), gen('b'), gen('c'))({})
return promise.then(() => arr.should.eql([ 'a', 'b', 'c' ]))
})

it('should work with 0 middleware', function () {
Expand Down

0 comments on commit f881413

Please sign in to comment.