"use strict";

var leftCurlyCode = "{".charCodeAt(0);
var rightCurlyCode = "}".charCodeAt(0);
var quoteCode = '"'.charCodeAt(0);
var leftSquareBracketCode = "[".charCodeAt(0);
var rightSquareBracketCode = "]".charCodeAt(0);

function Parser(){
    this.curlyCounter = 0;
    this.packet = [];
    this.inQuoteMode = false;
    this.isArray = false;
}

Parser.prototype.consume = function(buffer){
    var parser = this;
    var byte;

    var i = 0;
    var bufferLength = buffer.length;
    for(i; i < bufferLength; i++){
        byte = buffer[i];
        if(parser.curlyCounter === 0 && parser.isArray && byte === rightSquareBracketCode){
            parser.packet.push(byte);
            parser.isArray = false;
            extract(parser.packet);
        }
        else if(parser.curlyCounter === 0){
            // Processing new packet
            switch(byte){
            case leftSquareBracketCode:
                parser.isArray = true;
                break;
            case leftCurlyCode:
                parser.curlyCounter++;
                break;
            }
            parser.packet.push(byte);
        } else{
            switch(byte){
            case leftCurlyCode:
                !parser.inQuoteMode && parser.curlyCounter++;
                break;
            case rightCurlyCode:
                !parser.inQuoteMode && parser.curlyCounter--;
                break;
            case quoteCode:
                parser.inQuoteMode = !parser.inQuoteMode;
                break;
            }
            parser.packet.push(byte);

            if(parser.curlyCounter === 0 && !parser.isArray){
                extract(parser.packet);
            }
        }
    }

    function extract(packet){
        parser.onPacket(new Buffer(packet));
        parser.packet = [];
    }
};

Parser.prototype.on = function(event, callback){
    var parser = this;

    switch (event) {
    case "json":
        parser.onPacket = callback;
        break;
    default:
        console.warn("Not supported event:", event);
        break;
    }
}

module.exports = Parser;

"use strict";

var leftCurlyCode = "{".charCodeAt(0);
var rightCurlyCode = "}".charCodeAt(0);
var quoteCode = '"'.charCodeAt(0);
var leftSquareBracketCode = "[".charCodeAt(0);
var rightSquareBracketCode = "]".charCodeAt(0);

function Parser(){
    this.curlyCounter = 0;
    this.packet = [];
    this.inQuoteMode = false;
    this.isArray = false;
}

Parser.prototype.consume = function(buffer){
    var parser = this;
    var byte;

    var bufferLength = buffer.length;
    for(var i = 0; i < bufferLength; i++){
        byte = buffer[i];
        if(parser.curlyCounter === 0 && parser.isArray && byte === rightSquareBracketCode){
            parser.packet.push(byte);
            parser.isArray = false;
            extract(parser.packet);
        }
        else if(parser.curlyCounter === 0){
            // Processing new packet
            switch(byte){
            case leftSquareBracketCode:
                parser.isArray = true;
                break;
            case leftCurlyCode:
                parser.curlyCounter++;
                break;
            }
            parser.packet.push(byte);
        } else{
            switch(byte){
            case leftCurlyCode:
                !parser.inQuoteMode && parser.curlyCounter++;
                break;
            case rightCurlyCode:
                !parser.inQuoteMode && parser.curlyCounter--;
                break;
            case quoteCode:
                parser.inQuoteMode = !parser.inQuoteMode;
                break;
            }
            parser.packet.push(byte);

            if(parser.curlyCounter === 0 && !parser.isArray){
                extract(parser.packet);
            }
        }
    }

    function extract(packet){
        parser.onPacket(new Buffer(packet));
        parser.packet = [];
    }
};

Parser.prototype.on = function(event, callback){
    var parser = this;

    switch (event) {
    case "json":
        parser.onPacket = callback;
        break;
    default:
        console.warn("Not supported event:", event);
        break;
    }
}

module.exports = Parser;

Put index variable inside for statement