a zip code crypto-currency system good for red ONLY

wrappy.js 905B

12345678910111213141516171819202122232425262728293031323334
  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. }