diff --git a/examples/console2.js b/examples/console2.js index b5fcd34..8e67196 100644 --- a/examples/console2.js +++ b/examples/console2.js @@ -25,3 +25,5 @@ console.log( ); console.tag("Combo!").time().file().log("A combo"); + +console.log("A string %s and a number %d", "hello", "123"); //you can you printf-like format diff --git a/lib/console2.js b/lib/console2.js index ce3063b..978ea18 100644 --- a/lib/console2.js +++ b/lib/console2.js @@ -74,6 +74,26 @@ return '[' + infos.filename + ':' + infos.line + ']'; }; + /** + * areAllStringOrNumber + * + * Check in an array contains only string and number + * + * @param {Array} arr + * @retrun {Boolean} + */ + var areAllStringOrNumber = function (arr) { + + arr.forEach(function (elem) { + if (typeof elem !== 'string' && typeof elem !== 'number') { + return false; + } + }); + + return true; + + }; + /** * applyColors * @@ -194,7 +214,7 @@ this._tags = []; this._time = false; this._date = false; - this.location = false; + this._location = false; return this; }; @@ -234,7 +254,9 @@ * Console2.prototype.tag * * Add tags - * @param {*} tag + * @param {String|Object} tag + * @param {String} tag.msg The tag + * @paral {String|Array} tag.color colors.js colors */ Console2.prototype.tag = Console2.prototype.t = function () { var tags = Array.prototype.slice.call(arguments, 0); @@ -311,35 +333,44 @@ //It's time to log the args var args = Array.prototype.slice.call(log.args, 0), //transform args in an array - msg = "", //the log message - delimiter = '\n\n', - multiLines = false; //if the log need multiples lines (ie. object, array) + msg = ""; //the log message - //Process arg one by one - args.forEach(function (arg) { + //if all args are string or number, format args as usual + if (areAllStringOrNumber(args)) { + msg = util.format.apply(this, args); + + //if objects or array present + } else { - //if arg is an object / array - //use multiples lines - if (arg !== null && typeof arg === 'object') { + var delimiter = '\n\n', + multiLines = false; //if the log need multiples lines (ie. object, array) - msg += delimiter; - - msg += JSON.stringify(arg, null, 2); - - multiLines = true; - - //for "normal" args - } else { - if (multiLines) { + //Process arg one by one + args.forEach(function (arg) { + + //if arg is an object / array + //use multiples lines + if (arg !== null && typeof arg === 'object') { + msg += delimiter; + + msg += JSON.stringify(arg, null, 2); + + multiLines = true; + + //for "normal" args + } else { + if (multiLines) { + msg += delimiter; + } + msg += arg + ""; } - msg += arg + ""; + }); + + if (multiLines) { + msg += '\n'; + msg = msg.replace(/\n/gm, '\n' + new Array(this.opt.spaceSize).join(' ')); } - }); - - if (multiLines) { - msg += '\n'; - msg = msg.replace(/\n/gm, '\n' + new Array(this.opt.spaceSize).join(' ')); } result += msg;