index.js 825 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*!
  2. *
  3. * before
  4. *
  5. * before decorator factory
  6. *
  7. * MIT
  8. *
  9. */
  10. /**
  11. * Module dependencies.
  12. */
  13. var slice = [].slice;
  14. /**
  15. * Expose `before`.
  16. */
  17. module.exports = before;
  18. /**
  19. * Captures `context` to be used by decorator.
  20. *
  21. * @param {Object} [context]
  22. * @return {Function}
  23. * @api public
  24. */
  25. function before(context){
  26. return decorator;
  27. /**
  28. * Decorates `method` using `outer`.
  29. *
  30. * @param {Function} outer
  31. * @return {Object} this
  32. * @api public
  33. */
  34. function decorator(method, outer){
  35. var inner = context[method];
  36. context[method] = function(){
  37. var args = slice.call(arguments);
  38. var fn = args[args.length - 1];
  39. return outer.call(context, args, function(err){
  40. if (err) return fn(err);
  41. inner.apply(context, args);
  42. });
  43. };
  44. return this;
  45. }
  46. }