Last active
July 25, 2017 01:41
-
-
Save m1m1s1ku/a511781f86d9606a31e004e6ca2ec281 to your computer and use it in GitHub Desktop.
Debug beautifully (log function pure js)
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
/** | |
* log(anything); | |
* Shorter | |
*/ | |
log = function(){ | |
return Function.prototype.bind.call(console.log, console, "🛠️"); | |
}(); | |
/** | |
* Debug beautifully | |
* @param {any} params | |
* @param {string} type : 'log', 'info', 'warn', 'error' | |
* @param {string} emoji : Emoji to use (or another string) | |
* @param {boolean} breakHere : Add breakpoint for debugger | |
* @param {boolean} callStack : if call stack should be logged | |
*/ | |
var log = (obj, type = 'log', emoji = '🛠️', breakHere = false, callStack = false) => { | |
var debug = null; | |
if (typeof obj.reduce === 'function') | |
debug = obj.reduce(function (result, item) { | |
var key = Object.keys(item)[0]; | |
result[key] = item[key]; | |
return result; | |
}); | |
else | |
debug = obj; | |
console[type](emoji, debug); | |
if (breakHere) | |
debugger; | |
if (callStack) | |
if (typeof console.trace === 'function') | |
console.trace(); | |
else | |
console.log(new Error('trace').stack); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment