Commit 09ac685c authored by Alan W Szlosek Jr's avatar Alan W Szlosek Jr

separator for LineBuffer can be changed

parent 6b5912cc
var LineBuffer = function(lineCallback) {
var LineBuffer = function(lineCallback, separator) {
this.callback = lineCallback;
this.separator = separator || /\r\n|\r|\n/;
/*
- lineBuffer contains an array of strings
- we split input data on newline and push into this array
......@@ -7,13 +8,20 @@ var LineBuffer = function(lineCallback) {
so we need to append the first line of new input onto it
*/
// Last element should always be an empty string to play nice with our algorithm below
this.clear();
};
LineBuffer.prototype.clear = function() {
// the last element is assumed to be a partial line (event if it's an empty string),
// so we need to append the first line of new input onto it
this.lineBuffer = [''];
};
LineBuffer.prototype.data = function(data) {
var last = this.lineBuffer.length - 1;
var lines = data.toString().split(/\r\n|\r|\n/);
var last = this.lineBuffer.length > 1 ? this.lineBuffer.length - 1: 0;
var lines = data.toString().split(this.separator);
// Append first line of new input to last element of lineBuffer
this.lineBuffer[last] += lines.shift();
......@@ -25,13 +33,8 @@ LineBuffer.prototype.data = function(data) {
// Leave the last partial line in the line buffer
while (this.lineBuffer.length > 1) {
var line = this.lineBuffer.shift();
var payload;
var message;
var len;
var id;
if (line.length > 0) {
this.callback(line);
}
// Whether or not to send empty lines should be an option
this.callback(line);
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment