varemenos
11/9/2014 - 5:54 PM

logger

logger

var spacing = (function (size) {
  var result = '';

  for (var i = 0; i < size; i++) {
    result += '&nbsp;';
  }

  return result;
})(4);

var spacing4Depth = function (size) {
  var result = '';

  for (var i = 0; i < size; i++) {
    result += spacing;
  }

  return result;
};

var parseObj = function (items, depth) {
  if (checkDepth()) {
    return items;
  }

  var str;
  var count = 0;

  str = "{<br>";

  for (var item in items) {
    if (items.hasOwnProperty(item)) {
      count++;
      str += spacing4Depth(depth + 1) + "<b>" + item + "</b>: " + parseItem(items[item], depth + 1) + ",<br>";
    }
  }

  if (count > 0) {
    str = str.substring(0, str.length - 5) + "<br>" + spacing4Depth(depth) + "}";
  } else {
    str = "{}";
  }

  return str;
};

var parseArr = function (items, depth) {
  if (checkDepth()) {
    return items;
  }

  var str;
  var count = 0;

  str = "[<br>";

  for (var item in items) {
    if (items.hasOwnProperty(item)) {
      count++;
      str += spacing4Depth(depth + 1) + parseItem(items[item], depth + 1) + ",<br>";
    }
  }

  if (count > 0) {
    str = str.substring(0, str.length - 5) + "<br>" + spacing4Depth(depth) + "]";
  } else {
    str = "[]";
  }

  return str;
};

var checkDepth = function (depth) {
  return depth >= 20;
};

var parseItem = function (items, depth) {
  if (typeof items == "object") {
    if (items instanceof Array) {
      return parseArr(items, depth);
    } else {
      return parseObj(items, depth);
    }
  } else {
    return items;
  }
};

var logger = function (items) {
  return parseItem(items, 0);
};