lib/ConnectSequence.js
function appendIf (filter /*, middlewares */) {
  var errorMsg
  var middlewares = []

  if (arguments.length < 2) {
    errorMsg = 'ConnectSequence#appendIf() takes 2 arguments. '
    errorMsg += arguments.length + ' given.'
    throw new MissingArgumentError(errorMsg)
  }
  if (typeof filter !== 'function') {
    errorMsg = 'The first argument must be a filter function. '
    errorMsg += typeof filter + ' given.'
    throw new TypeError(errorMsg)
  }

  var middleware, i
  for (i = 1; i < arguments.length; i++) {
    middleware = arguments[i]
    if (typeof middleware !== 'function') {
      errorMsg = 'The second argument must be a middleware function. '
      errorMsg += typeof middleware + ' given.'
      throw new TypeError(errorMsg)
    }
    middlewares.push(middleware)
  }

  var firstMiddleware = middlewares[0]
  if (isErrorHandler(firstMiddleware)) {
    this.append(function (err, req, res, next) {
      req.__connectSequenceFilterValue = filter(req)
      if (req.__connectSequenceFilterValue) {
        firstMiddleware(err, req, res, next)
      } else {
        next()
      }
    })
  } else {
    this.append(function (req, res, next) {
      req.__connectSequenceFilterValue = filter(req)
      if (req.__connectSequenceFilterValue) {
        firstMiddleware(req, res, next)
      } else {
        next()
      }
    })
  }

  for (i = 1; i < middlewares.length; i++) {
    middleware = middlewares[i]
    appendOnFilterValue.call(this, middleware)
  }

  return this
}
function appendIf (filter /*, middlewares */) {
  var errorMsg
  var middlewares = []

  if (arguments.length < 2) {
    errorMsg = 'ConnectSequence#appendIf() takes 2 arguments. '
    errorMsg += arguments.length + ' given.'
    throw new MissingArgumentError(errorMsg)
  }
  if (typeof filter !== 'function') {
    errorMsg = 'The first argument must be a filter function. '
    errorMsg += typeof filter + ' given.'
    throw new TypeError(errorMsg)
  }

  var middleware, i
  for (i = 1; i < arguments.length; i++) {
    middleware = arguments[i]
    if (typeof middleware !== 'function') {
      errorMsg = 'The second argument must be a middleware function. '
      errorMsg += typeof middleware + ' given.'
      throw new TypeError(errorMsg)
    }
    middlewares.push(middleware)
  }

  var firstMiddleware = middlewares[0]
  if (isErrorHandler(firstMiddleware)) {
    this.append(function (err, req, res, next) {
      req.__connectSequenceFilterValue = filter(req)
      if (req.__connectSequenceFilterValue) {
        firstMiddleware(err, req, res, next)
      } else {
        next()
      }
    })
  } else {
    this.append(function (req, res, next) {
      req.__connectSequenceFilterValue = filter(req)
      if (req.__connectSequenceFilterValue) {
        firstMiddleware(req, res, next)
      } else {
        next()
      }
    })
  }

  for (i = 1; i < middlewares.length; i++) {
    middleware = middlewares[i]
    appendOnFilterValue.call(this, middleware)
  }

  return this
}
function appendIf ( filter /*, middlewares */ ) { var errorMsg var middlewares = [ ] if ( arguments . length < 2 ) { errorMsg = 'ConnectSequence#appendIf() takes 2 arguments. ' errorMsg += arguments . length + ' given.' throw new MissingArgumentError ( errorMsg ) } if ( typeof filter !== 'function' ) { errorMsg = 'The first argument must be a filter function. ' errorMsg += typeof filter + ' given.' throw new TypeError ( errorMsg ) } var middleware , i for ( i = 1 ; i < arguments . length ; i ++ ) { middleware = arguments [ i ] if ( typeof middleware !== 'function' ) { errorMsg = 'The second argument must be a middleware function. ' errorMsg += typeof middleware + ' given.' throw new TypeError ( errorMsg ) } middlewares . push ( middleware ) } var firstMiddleware = middlewares [ 0 ] if ( isErrorHandler ( firstMiddleware ) ) { this . append ( function ( err , req , res , next ) { req . __connectSequenceFilterValue = filter ( req ) if ( req . __connectSequenceFilterValue ) { firstMiddleware ( err , req , res , next ) } else { next ( ) } } ) } else { this . append ( function ( req , res , next ) { req . __connectSequenceFilterValue = filter ( req ) if ( req . __connectSequenceFilterValue ) { firstMiddleware ( req , res , next ) } else { next ( ) } } ) } for ( i = 1 ; i < middlewares . length ; i ++ ) { middleware = middlewares [ i ] appendOnFilterValue . call ( this , middleware ) } return this }
Append an arbitrary number of middlewares as an argument list if the filter pass at runtime of the first middleware in the given list
@method
@memberof ConnectSequence.prototype
@param {Function} filter A filter function (returning a Boolean)
@param {...Function} middlewares A list of middleware functions (or errorHandler middlewares)
@returns {ConnectSequence} a reference to the instance to be chainable
Append an arbitrary number of middlewares as an argument list if the filter pass at runtime of the first middleware in the given list