Created
January 31, 2016 17:17
-
-
Save asleepysamurai/7e592ebd7d671c9a1d6d to your computer and use it in GitHub Desktop.
Bows - custom inspect method for printing time diff
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
(function() { | |
function checkColorSupport() { | |
if (typeof window === 'undefined' || typeof navigator === 'undefined') { | |
return false; | |
} | |
var chrome = !!window.chrome, | |
firefox = /firefox/i.test(navigator.userAgent), | |
firefoxVersion; | |
if (firefox) { | |
var match = navigator.userAgent.match(/Firefox\/(\d+\.\d+)/); | |
if (match && match[1] && Number(match[1])) { | |
firefoxVersion = Number(match[1]); | |
} | |
} | |
return chrome || firefoxVersion >= 31.0; | |
} | |
var yieldColor = function() { | |
var goldenRatio = 0.618033988749895; | |
hue += goldenRatio; | |
hue = hue % 1; | |
return hue * 360; | |
}; | |
var inNode = typeof window === 'undefined', | |
ls = !inNode && window.localStorage, | |
debugKey = ls.andlogKey || 'debug', | |
debug = ls[debugKey], | |
logger = require('andlog'), | |
bind = Function.prototype.bind, | |
hue = 0, | |
padLength = 15, | |
noop = function() {}, | |
colorsSupported = ls.debugColors || checkColorSupport(), | |
bows = null, | |
debugRegex = null, | |
invertRegex = false, | |
moduleColorsMap = {}; | |
if (debug && debug[0] === '!' && debug[1] === '/') { | |
invertRegex = true; | |
debug = debug.slice(1); | |
} | |
debugRegex = debug && debug[0] === '/' && new RegExp(debug.substring(1, debug.length - 1)); | |
var logLevels = ['log', 'debug', 'warn', 'error', 'info']; | |
//Noop should noop | |
for (var i = 0, ii = logLevels.length; i < ii; i++) { | |
noop[logLevels[i]] = noop; | |
} | |
var lastInvokedAt; | |
diff = function() { | |
var now = Date.now(); | |
var diff = now - (lastInvokedAt || now); | |
lastInvokedAt = now; | |
return '+' + diff + 'ms'; | |
}; | |
bows = function(str) { | |
var msg, colorString, logfn; | |
msg = (str.slice(0, padLength)); | |
msg += Array(padLength + 3 - msg.length).join(' '); | |
if (debugRegex) { | |
var matches = str.match(debugRegex); | |
if ( | |
(!invertRegex && !matches) || | |
(invertRegex && matches) | |
) return noop; | |
} | |
if (!bind) return noop; | |
var logArgs = [logger]; | |
if (colorsSupported) { | |
if (!moduleColorsMap[str]) { | |
moduleColorsMap[str] = yieldColor(); | |
} | |
var color = moduleColorsMap[str]; | |
msg = "%c" + msg; | |
colorString = "color: hsl(" + (color) + ",99%,40%); font-weight: bold"; | |
logArgs.push(msg, colorString); | |
} else { | |
logArgs.push(msg); | |
} | |
if (arguments.length > 1) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
logArgs = logArgs.concat(args); | |
} | |
function TimeDiff() {}; | |
TimeDiff.prototype.inspect = function() { | |
return diff(); | |
}; | |
logArgs.push(new TimeDiff()); | |
logArgs.push('|'); | |
logfn = bind.apply(logger.log, logArgs); | |
logLevels.forEach(function(f) { | |
logfn[f] = bind.apply(logger[f] || logfn, logArgs); | |
}); | |
return logfn; | |
}; | |
bows.config = function(config) { | |
if (config.padLength) { | |
padLength = config.padLength; | |
} | |
}; | |
if (typeof module !== 'undefined') { | |
module.exports = bows; | |
} else { | |
window.bows = bows; | |
} | |
}).call(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment