Last active
August 29, 2015 14:13
-
-
Save aaronmccall/ee951b71fe1714226e58 to your computer and use it in GitHub Desktop.
some useful &.js debugging utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var each = require('amp-each'); | |
var isFunction = require('amp-is-function'); | |
function debugMethods(prototype, handler) { | |
each(prototype, function (prop, name) { | |
if (isFunction(handler)) return handler(prop, name); | |
if (isFunction(prop)) { | |
prototype[name] = addLogger(prop, name); | |
} | |
}); | |
} | |
exports.debugMethods = debugMethods; | |
function addLogger(fn, name) { | |
return function () { | |
console.log('[Function] %s: %o', name, arguments); | |
return fn.apply(this, arguments); | |
}; | |
} | |
exports.addLogger = addLogger; | |
function debugDerived(prototype) { | |
debugMethods(prototype._derived, function (prop, name) { | |
if (isFunction(prop.fn)) { | |
prop.fn = addLogger(prop.fn, name + ':derived'); | |
} | |
}); | |
} | |
exports.debugDerived = debugDerived; | |
function mixin(constructor) { | |
constructor.debug = function (debugDerived) { | |
if (this._debug) return this; | |
this._debug = true; | |
debugMethods(this.prototype); | |
if (debugDerived) debugDerived(this.prototype); | |
return this; | |
}; | |
} | |
exports.mixin = mixin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var State = require('ampersand-state'); | |
var debugMixin = require('./debug').mixin; | |
var MyState = module.exports = State.extend({/* my defs here */}); | |
debugMixin(MyState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment