Advertisement
g-stoyanov

Extended Console Log

May 1st, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     'use strict';
  3.  
  4.     var specialConsole = (function () {
  5.         var writeLine = function () {
  6.             var args = Array.prototype.slice.call(arguments);
  7.             if (args.length > 0) {
  8.                 var outputString = createString(args);
  9.                 console.log(outputString);
  10.             }          
  11.         };
  12.  
  13.         var writeError = function () {
  14.             var args = Array.prototype.slice.call(arguments);
  15.             if (args.length > 0) {
  16.                 var outputString = createString(args);
  17.                 console.error(outputString);
  18.             }
  19.         };
  20.  
  21.         function createString(dataArr) {
  22.             var regExp = new RegExp('(\\{(0)\\})', ['gi']);
  23.             var workString = dataArr[0].toString();
  24.             var ocurrArr = workString.match(regExp);
  25.             var count = 1;
  26.             while (ocurrArr) {
  27.                 workString = workString.replace(regExp, (dataArr[count] || '').toString());
  28.                 regExp = new RegExp('(\\{(' + count + ')\\})', ['gi']);
  29.                 ocurrArr = workString.match(regExp);
  30.                 count++;
  31.             }
  32.  
  33.             return workString;
  34.         }
  35.  
  36.         return {
  37.             writeLine: writeLine,
  38.             writeError: writeError
  39.         };
  40.     })();
  41.  
  42.     specialConsole.writeLine();
  43.     specialConsole.writeLine('Test message!');
  44.     specialConsole.writeLine('Test message with {1} {0}{2}', 'placeholders', 'some', '!');
  45.     specialConsole.writeLine('Test message with {1} {0}{2} and missing parameter!', 'placeholders', 'some');
  46.  
  47.     specialConsole.writeError();
  48.     specialConsole.writeError('Error message!');
  49.     specialConsole.writeError('Error message with {1} {0}{2}', 'placeholders', 'some', '!');
  50.     specialConsole.writeError('Error message with {1} {0}{2} and missing parameter!', 'placeholders', 'some');
  51. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement