index.js
function use () {
  // init hooks
  if (!this.hasOwnProperty('_hooks')) {
    this._hooks = {}
  }

  var args = Array.prototype.slice.call(arguments)
  var i, j, l, m
  var name = null

  if (is.array(args[0])) {
    // use(['a','b','c'], ...)
    var arr = args.shift()
    for (i = 0, l = arr.length; i < l; i++) {
      use.apply(this, [arr[i]].concat(args))
    }
    return this
  } else if (is.object(args[0]) && args[0]._hooks) {
    // use(ginga)
    var key
    // concat hooks
    for (key in args[0]._hooks) {
      use.call(this, key, args[0]._hooks[key])
    }
    return this
  }

  // method name
  if (is.string(args[0])) name = args.shift()
  if (!name) throw new Error('Method name is not defined.')
  if (!this._hooks[name]) this._hooks[name] = []

  for (i = 0, l = args.length; i < l; i++) {
    if (is.function(args[i])) {
      this._hooks[name].push(wrapFn(args[i]))
    } else if (is.array(args[i])) {
      // use('a', [fn1, fn2, fn3])
      for (j = 0, m = args[i].length; j < m; j++) {
        use.call(this, name, args[i][j])
      }
      return this
    } else {
      throw new Error('Middleware must be a function')
    }
  }
  return this
}
function use () {
  // init hooks
  if (!this.hasOwnProperty('_hooks')) {
    this._hooks = {}
  }

  var args = Array.prototype.slice.call(arguments)
  var i, j, l, m
  var name = null

  if (is.array(args[0])) {
    // use(['a','b','c'], ...)
    var arr = args.shift()
    for (i = 0, l = arr.length; i < l; i++) {
      use.apply(this, [arr[i]].concat(args))
    }
    return this
  } else if (is.object(args[0]) && args[0]._hooks) {
    // use(ginga)
    var key
    // concat hooks
    for (key in args[0]._hooks) {
      use.call(this, key, args[0]._hooks[key])
    }
    return this
  }

  // method name
  if (is.string(args[0])) name = args.shift()
  if (!name) throw new Error('Method name is not defined.')
  if (!this._hooks[name]) this._hooks[name] = []

  for (i = 0, l = args.length; i < l; i++) {
    if (is.function(args[i])) {
      this._hooks[name].push(wrapFn(args[i]))
    } else if (is.array(args[i])) {
      // use('a', [fn1, fn2, fn3])
      for (j = 0, m = args[i].length; j < m; j++) {
        use.call(this, name, args[i][j])
      }
      return this
    } else {
      throw new Error('Middleware must be a function')
    }
  }
  return this
}
function use ( ) { // init hooks if ( ! this . hasOwnProperty ( '_hooks' ) ) { this . _hooks = { } } var args = Array . prototype . slice . call ( arguments ) var i , j , l , m var name = null if ( is . array ( args [ 0 ] ) ) { // use(['a','b','c'], ...) var arr = args . shift ( ) for ( i = 0 , l = arr . length ; i < l ; i ++ ) { use . apply ( this , [ arr [ i ] ] . concat ( args ) ) } return this } else if ( is . object ( args [ 0 ] ) && args [ 0 ] . _hooks ) { // use(ginga) var key // concat hooks for ( key in args [ 0 ] . _hooks ) { use . call ( this , key , args [ 0 ] . _hooks [ key ] ) } return this } // method name if ( is . string ( args [ 0 ] ) ) name = args . shift ( ) if ( ! name ) throw new Error ( 'Method name is not defined.' ) if ( ! this . _hooks [ name ] ) this . _hooks [ name ] = [ ] for ( i = 0 , l = args . length ; i < l ; i ++ ) { if ( is . function ( args [ i ] ) ) { this . _hooks [ name ] . push ( wrapFn ( args [ i ] ) ) } else if ( is . array ( args [ i ] ) ) { // use('a', [fn1, fn2, fn3]) for ( j = 0 , m = args [ i ] . length ; j < m ; j ++ ) { use . call ( this , name , args [ i ] [ j ] ) } return this } else { throw new Error ( 'Middleware must be a function' ) } } return this }
ginga use method
ginga use method