/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: false, bitwise: true, newcap: true, maxerr: 50, indent: 4 */
var jsl = typeof jsl === 'undefined' ? {} : jsl;

/**
 * jsl.format - Provide json reformatting in a character-by-character approach, so that even invalid JSON may be reformatted (to the best of its ability).
 *
**/
jsl.format = (function () {

    function repeat(s, count) {
        return new Array(count + 1).join(s);
    }
    function getSizeOfArray(jsonstring,startingposition){
        var result;
        var currentposition = startingposition + 1;
        var inString = false;
        var num_opened = 1;
        try{
            while (num_opened > 0 && currentposition < jsonstring.length) {
                var currentChar = jsonstring.charAt(currentposition)
                switch (currentChar) {
                    case '[':
                        if(!inString){
                            num_opened++;
                        }
                        break;
                    case ']':
                        if(!inString){
                            num_opened--;
                        }
                        break;
                    case '"':
                        inString = !inString;
                        break;
                }
                currentposition++;
            }
            return JSON.parse(jsonstring.substring(startingposition,currentposition)).length;
        }
        catch(err){
            return null;
        }
    }
    function formatJson(json, options) {
        options = options || {};
        var tabSize = options.tabSize || 2;
        var indentCStyle = options.indentCStyle || false;
        var showArraySize = (typeof options.showArraySize !== "undefined" ? Boolean(options.showArraySize) : true);
        var tab = "";
        for (var ts = 0; ts < tabSize; ts++) {
          tab += " ";
        }

        var i           = 0,
            il          = 0,
            newJson     = "",
            indentLevel = 0,
            inString    = false,
            currentChar = null;
        for (i = 0, il = json.length; i < il; i += 1) {
            currentChar = json.charAt(i);

            switch (currentChar) {
            case '{':
            case '[':
                if (!inString) {
                    if (indentCStyle) newJson += "\n" + repeat(tab, indentLevel);
                    if(currentChar === "["){
                        if(showArraySize){
                            var arraySize = getSizeOfArray(json,i);
                            if(arraySize !== null){
                                newJson += "Array[" + arraySize + "]";
                            }
                        }
                    }
                    newJson += currentChar;
                    
                    newJson +=  "\n" + repeat(tab, indentLevel + 1);
                    indentLevel += 1;
                } else {
                    newJson += currentChar;
                }
                break;
            case '}':
            case ']':
                if (!inString) {
                    indentLevel -= 1;
                    newJson += "\n" + repeat(tab, indentLevel) + currentChar;
                } else {
                    newJson += currentChar;
                }
                break;
            case ',':
                if (!inString) {
                    newJson += ",\n" + repeat(tab, indentLevel);
                } else {
                    newJson += currentChar;
                }
                break;
            case ':':
                if (!inString) {
                    newJson += ": ";
                } else {
                    newJson += currentChar;
                }
                break;
            case ' ':
            case "\n":
            case "\t":
                if (inString) {
                    newJson += currentChar;
                }
                break;
            case '"':
                if (i > 0 && (json.charAt(i - 1) !== '\\' || (json.charAt(i - 1) == '\\' && json.charAt(i - 2) == '\\'))) {
                    inString = !inString;
                }
                newJson += currentChar;
                break;
            default:
                newJson += currentChar;
                break;
            }
        }

        return newJson;
    }

    return { "formatJson": formatJson };

}());

module.exports = jsl.format.formatJson;

@@ -524,25 +524,25 @@
 OfArray(json
-s
+S
 tring,starti
@@ -539,25 +539,25 @@
 ing,starting
-p
+P
 osition)%7B%0A  
@@ -558,28 +558,8 @@
 n)%7B%0A
-        var result;%0A
     
@@ -565,33 +565,33 @@
      var current
-p
+P
 osition = starti
@@ -588,25 +588,25 @@
 n = starting
-p
+P
 osition + 1;
@@ -647,26 +647,25 @@
      var num
-_o
+O
 pened = 1;%0A 
@@ -698,18 +698,17 @@
 ile (num
-_o
+O
 pened %3E 
@@ -715,25 +715,25 @@
 0 && current
-p
+P
 osition %3C js
@@ -730,25 +730,25 @@
 ition %3C json
-s
+S
 tring.length
@@ -785,25 +785,25 @@
 tChar = json
-s
+S
 tring.charAt
@@ -806,25 +806,25 @@
 arAt(current
-p
+P
 osition)%0A   
@@ -955,26 +955,25 @@
          num
-_o
+O
 pened++;%0A   
@@ -1130,10 +1130,9 @@
  num
-_o
+O
 pene
@@ -1337,25 +1337,25 @@
      current
-p
+P
 osition++;%0A 
@@ -1401,17 +1401,17 @@
 rse(json
-s
+S
 tring.su
@@ -1426,17 +1426,17 @@
 starting
-p
+P
 osition,
@@ -1442,17 +1442,17 @@
 ,current
-p
+P
 osition)

Fix variable naming conventions