Last active
June 6, 2018 19:45
-
-
Save SidIcarus/59f3735da5cc27b678557c6b2006cad7 to your computer and use it in GitHub Desktop.
Pretty print your console logs.
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
((root, factory, has, name) => { | |
if (!has.call(root, name)) root[name] = factory(); | |
})(this || window, () => { | |
const consoleColors = { | |
default: "rgb(189, 198, 207)", | |
alert: "rgb(210, 70, 70)", | |
background: "rgb(64, 64, 64)", | |
info: "rgb(70, 140, 210)", | |
success: "rgb(35, 140, 35)", | |
warn: "rgb(225, 180, 110)" | |
}, | |
vPad = "padding: 2.6px 2.6px 2.6px 0", | |
pPad = "padding: 2.6px 0 2.6px 2.6px"; | |
const consoul = { | |
/** | |
* @todo Add a description. | |
* @param {Object} args - | |
* @param {string} title - | |
* @param {boolean} collapsed - | |
* @param {number} maxDepth | |
*/ | |
color(args = {}, title = "Object", collapsed = true, maxDepth = 3) { | |
collapsed = collapsed == null ? true : collapsed; | |
let colors = consoleColors, | |
bg = `background-color: ${colors.background}`, | |
DepthTraversal = args.DepthTraversal || 1; | |
let text = `%c${title}`, | |
color = `color: ${colors.warn}; ${bg}; padding: 2.6px`, | |
gc = collapsed ? "groupCollapsed" : "group"; | |
console[gc](text, color); | |
// eslint-disable-next-line guard-for-in, no-restricted-syntax | |
for (var prop in args) { | |
if (prop === "DepthTraversal") continue; | |
let isObject = false, | |
msg = "", | |
value = args[prop], | |
pColor = colors.default, | |
vColor; | |
switch(typeof value) { | |
case "boolean": | |
vColor = value ? colors.success : colors.alert; | |
break; | |
case "string": | |
if (value === "") value = "\"\""; | |
case "number": | |
vColor = colors.info; | |
break; | |
case "object": | |
isObject = true; | |
// if(Array.isArray(args)) { | |
// msg += "Array: "; | |
// value = [].concat.apply([], value).toString(); | |
// vColor = colors.warn; | |
// } | |
if (DepthTraversal < maxDepth) { | |
value.DepthTraversal = DepthTraversal + 1; | |
this.color(value, prop, collapsed, maxDepth); | |
} else { | |
// This may need to be wrapped in its own groupCollapsed | |
// console.groupCollapsed( | |
// `%c${prop}`, | |
// `color: ${colors.warn}; ${bg}; padding: 6px` | |
// ); | |
console.dir(value); | |
// console.groupEnd(); | |
} | |
break; | |
default: | |
break; | |
} | |
if (!isObject) { | |
vColor = `color: ${vColor};${bg};${vPad}`; | |
pColor = `color: ${pColor};${bg};${pPad}`; | |
msg += `%c${prop}: %c${value}`; | |
console.log(msg, pColor, vColor); | |
} | |
} | |
console.groupEnd(); | |
} | |
}; | |
console.color = consoul.color; | |
return consoul; | |
}, Object.prototype.hasOwnProperty, "consoul"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment