From 238059bca2ddf55d0df14bd807e9df4036892dbe Mon Sep 17 00:00:00 2001 From: Larry Battle Date: Wed, 10 Apr 2013 13:53:11 -0500 Subject: [PATCH] Updated script based on feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added project header info - Added `use strict` - Updated script based on feedback from pull-request-1 81a1805 --- normalizeconsole.js | 154 ++++++++++++++++++-------------------------- 1 file changed, 64 insertions(+), 90 deletions(-) diff --git a/normalizeconsole.js b/normalizeconsole.js index 25934ff..4082c8b 100644 --- a/normalizeconsole.js +++ b/normalizeconsole.js @@ -1,91 +1,65 @@ - -(function(console) { - /********************************************************************************************* - * Make sure console exists because IE blows up if it's not open and you attempt to access it - * Create some dummy functions if we need to, so we don't have to if/else everything - *********************************************************************************************/ - console||(console = window.console = { - // all this "a, b, c, d, e" garbage is to make the IDEs happy, since they can't do variable argument lists - /** - * @param a - * @param [b] - * @param [c] - * @param [d] - * @param [e] - */ - log: function(a, b, c, d, e) {}, - /** - * @param a - * @param [b] - * @param [c] - * @param [d] - * @param [e] - */ - info: function(a, b, c, d, e) {}, - /** - * @param a - * @param [b] - * @param [c] - * @param [d] - * @param [e] - */ - warn: function(a, b, c, d, e) {}, - /** - * @param a - * @param [b] - * @param [c] - * @param [d] - * @param [e] - */ - error: function(a, b, c, d, e) {} - }); - - // le sigh, IE, oh IE, how we fight... fix Function.prototype.bind as needed - if (!Function.prototype.bind) { - //credits: taken from bind_even_never in this discussion: https://prototype.lighthouseapp.com/projects/8886/tickets/215-optimize-bind-bindaseventlistener#ticket-215-9 - Function.prototype.bind = function(context) { - var fn = this, args = Array.prototype.slice.call(arguments, 1); - return function(){ +/** +* Console Normalizer, https://github.com/Zenovations/console-normalizer +* @version 2013-04-10 +* @license MIT +*/ +(function (root) { + "use strict"; + if(typeof root !== "object"){ + return; + } + /********************************************************************************************* + * Make sure console exists because IE blows up if it's not open and you attempt to access it + * Create some dummy functions if we need to, so we don't have to if/else everything + *********************************************************************************************/ + var console = root.console || { + error : function () {}, + info : function () {}, + log : function () {}, + warn : function () {} + }; + + //credits: taken from bind_even_never in this discussion: https://prototype.lighthouseapp.com/projects/8886/tickets/215-optimize-bind-bindaseventlistener#ticket-215-9 + Function.prototype.bind = Function.prototype.bind || function (context) { + var fn = this, + args = Array.prototype.slice.call(arguments, 1); + return function () { return fn.apply(context, Array.prototype.concat.apply(args, arguments)); - }; - }; - } - - // IE 9 won't allow us to call console.log.apply (WTF IE!) It also reports typeof(console.log) as 'object' (UNH!) - // but together, those two errors can be useful in allowing us to fix stuff so it works right - if( typeof(console.log) === 'object' ) { - // Array.forEach doesn't work in IE 8 so don't try that :( - console.log = Function.prototype.call.bind(console.log, console); - console.info = Function.prototype.call.bind(console.info, console); - console.warn = Function.prototype.call.bind(console.warn, console); - console.error = Function.prototype.call.bind(console.error, console); - } - - /** - * Support group and groupEnd functions - */ - ('group' in console) || - (console.group = function(msg) { - console.info("\n------------\n"+msg+"\n------------"); - }); - ('groupEnd' in console) || - (console.groupEnd = function() { - //console.log("\n\n"); - }); - - /** - * Support time and timeEnd functions - */ - ('time' in console) || - (function() { - var trackedTimes = {}; - console.time = function(msg) { - trackedTimes[msg] = new Date().getTime(); - }; - console.timeEnd = function(msg) { - var end = new Date().getTime(), time = (msg in trackedTimes)? end - trackedTimes[msg] : 0; - console.info(msg+': '+time+'ms') - } - }()); - -})(window.console); \ No newline at end of file + }; + }; + + // IE 9 won't allow us to call console.log.apply (WTF IE!) It also reports typeof(console.log) as 'object' (UNH!) + // but together, those two errors can be useful in allowing us to fix stuff so it works right + if (typeof(console.log) === 'object') { + console.error = Function.prototype.call.bind(console.error, console); + console.info = Function.prototype.call.bind(console.info, console); + console.log = Function.prototype.call.bind(console.log, console); + console.warn = Function.prototype.call.bind(console.warn, console); + } + + /** + * Support group and groupEnd functions + */ + console.group = console.group || function (msg) { + console.info("\n------------\n" + msg + "\n------------"); + }; + + console.groupEnd = console.groupEnd || function () {}; + /** + * Support time and timeEnd functions + */ + + console.trackedTimes = {}; + + console.time = console.time || function (msg) { + console.trackedTimes[msg] = new Date().getTime(); + }; + + console.timeEnd = console.timeEnd || function (msg) { + var end = new Date().getTime(), + time = end - (console.trackedTimes[msg] || end); + console.info(msg + ': ' + time + 'ms'); + }; + + root.console = console; +}(window));