CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
wrappy.js34 linesDownload Raw Back to wrappy
1// Returns a wrapper function that returns a wrapped callback2// The wrapper function should do some stuff, and return a3// presumably different callback function.4// This makes sure that own properties are retained, so that5// decorations and such are not lost along the way.6module.exports = wrappy7function wrappy (fn, cb) {8  if (fn && cb) return wrappy(fn)(cb)9 10  if (typeof fn !== 'function')11    throw new TypeError('need wrapper function')12 13  Object.keys(fn).forEach(function (k) {14    wrapper[k] = fn[k]15  })16 17  return wrapper18 19  function wrapper() {20    var args = new Array(arguments.length)21    for (var i = 0; i < args.length; i++) {22      args[i] = arguments[i]23    }24    var ret = fn.apply(this, args)25    var cb = args[args.length-1]26    if (typeof ret === 'function' && ret !== cb) {27      Object.keys(cb).forEach(function (k) {28        ret[k] = cb[k]29      })30    }31    return ret32  }33}34