Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wrappy.js 905B

123456789101112131415161718192021222324252627282930313233
  1. // Returns a wrapper function that returns a wrapped callback
  2. // The wrapper function should do some stuff, and return a
  3. // presumably different callback function.
  4. // This makes sure that own properties are retained, so that
  5. // decorations and such are not lost along the way.
  6. module.exports = wrappy
  7. function wrappy (fn, cb) {
  8. if (fn && cb) return wrappy(fn)(cb)
  9. if (typeof fn !== 'function')
  10. throw new TypeError('need wrapper function')
  11. Object.keys(fn).forEach(function (k) {
  12. wrapper[k] = fn[k]
  13. })
  14. return wrapper
  15. function wrapper() {
  16. var args = new Array(arguments.length)
  17. for (var i = 0; i < args.length; i++) {
  18. args[i] = arguments[i]
  19. }
  20. var ret = fn.apply(this, args)
  21. var cb = args[args.length-1]
  22. if (typeof ret === 'function' && ret !== cb) {
  23. Object.keys(cb).forEach(function (k) {
  24. ret[k] = cb[k]
  25. })
  26. }
  27. return ret
  28. }
  29. }