Created
November 30, 2016 19:18
-
-
Save joeyred/8933cbb2a98ba71a707a03f53bd85339 to your computer and use it in GitHub Desktop.
A little debugging object.
This file contains hidden or 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
/** | |
* Controls whether debug functions have any output to console. | |
* @type {Boolean} | |
*/ | |
var DEBUG_IS_ENABLED = true; | |
/** | |
* Debug constructor | |
* @method Debug | |
* @param {string} objectName The name of the parent object of all logged data | |
*/ | |
function Debug(objectName) { | |
this.objectName = objectName; | |
} | |
Debug.prototype = { | |
constructor: Debug, | |
valueType: function(value) { | |
return typeof value; | |
}, | |
outputObjectParent: function() { | |
console.log('%c ' + this.objectName, 'color: green'); | |
}, | |
/** | |
* Check if function has been called and what it returned. | |
* @method function | |
* @param {string} functionName Name of the function called. | |
* @param {boolean/string} output The return of the function, or false. | |
*/ | |
functionReturn: function(functionName, output) { | |
// Check if debug mode is enabled. | |
if (DEBUG_IS_ENABLED) { | |
this.outputObjectParent(); | |
// If the function has no return, then just say the function was called, | |
if (output === 'undefined') { | |
console.log(functionName + ' has been called. Function has no return'); | |
// Else, log out the function being called along with its return. | |
} else { | |
console.log(functionName + ' has been called. Returned: ' + output); | |
} | |
} | |
}, | |
/** | |
* Output values within a function to console. | |
* @example | |
* // Will log to console: | |
* // Within foobar: | |
* // foo is: 5 (Number) | |
* // bar is: 22 (Number) | |
* function foobar() { | |
* var foo = 5; | |
* var bar = 22; | |
* Debug.value('foobar', | |
* {foo: foo, bar: bar} | |
* ); | |
* } | |
* @method value | |
* @param {string} functionName Name of function the value reside in. | |
* @param {Object} values Object with keys matching variable names to be logged. | |
*/ | |
values: function(functionName, values) { | |
// Check if debug mode is enabled. | |
if (DEBUG_IS_ENABLED) { | |
this.outputObjectParent(); | |
console.log('Within ' + functionName + ':'); | |
// Loop through values within the function. | |
for (var key in values) { | |
if ({}.hasOwnProperty.call(values, key)) { | |
// Log key and it's value in a readable fashion. | |
console.log(key + ' is: ', values[key], ' (' + this.valueType(values[key]) + ')'); | |
} | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment