lazy-eval.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var slice = [].slice,
  2. objectAssign = require('object-assign');
  3. function _resolveOutput(func, bindThis) {
  4. var wrapped = function() {
  5. var args;
  6. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  7. // lazy function eval to keep output memory pressure down, if not used
  8. if (typeof args[0] === 'function') {
  9. args[0] = args[0]();
  10. }
  11. return func.apply(bindThis, args);
  12. };
  13. objectAssign(wrapped, func);
  14. return wrapped;
  15. };
  16. function wrapEval(debug) {
  17. var debugOrig = debug,
  18. noop = function(){};
  19. debug = function (namespace) {
  20. var instance = debugOrig(namespace);
  21. // if we're not enabled then don't attempt to log anything
  22. // if a debug namespace wraps its debug in a closure then it never allocates anything but the function itself
  23. if (!instance.enabled){
  24. objectAssign(noop, instance);
  25. instance = noop;
  26. }
  27. else {
  28. instance = _resolveOutput(instance);
  29. }
  30. return instance;
  31. }
  32. objectAssign(debug, debugOrig);
  33. return debug;
  34. }
  35. module.exports = wrapEval;