Skip to content

Commit

Permalink
feat(parsers): Adding option to include delimiter in the DelimiterPar…
Browse files Browse the repository at this point in the history
…ser transform. (#1453)
  • Loading branch information
saboya authored and reconbot committed Jan 25, 2018
1 parent a819bca commit 6a3ab65
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/parsers/delimiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class DelimiterParser extends Transform {
throw new TypeError('"delimiter" has a 0 or undefined length');
}

this.includeDelimiter = options.includeDelimiter !== undefined ? options.includeDelimiter : false;
this.delimiter = Buffer.from(options.delimiter);
this.buffer = Buffer.alloc(0);
}
Expand All @@ -35,7 +36,7 @@ class DelimiterParser extends Transform {
let data = Buffer.concat([this.buffer, chunk]);
let position;
while ((position = data.indexOf(this.delimiter)) !== -1) {
this.push(data.slice(0, position));
this.push(data.slice(0, position + (this.includeDelimiter ? this.delimiter.length : 0)));
data = data.slice(position + this.delimiter.length);
}
this.buffer = data;
Expand Down
19 changes: 19 additions & 0 deletions test/parser-delimiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@ describe('DelimiterParser', () => {
parser.on('data', spy);
parser.write(Buffer.from('I love robots\nEach '));
parser.write(Buffer.from('and Every One\n'));
parser.write(Buffer.from('\n'));
parser.write(Buffer.from('even you!'));

assert.deepEqual(spy.getCall(0).args[0], Buffer.from('I love robots'));
assert.deepEqual(spy.getCall(1).args[0], Buffer.from('Each and Every One'));
assert(spy.calledTwice);
});

it('includes delimiter when includeDelimiter is true', () => {
const spy = sinon.spy();
const parser = new DelimiterParser({
delimiter: Buffer.from('\n'),
includeDelimiter: true
});
parser.on('data', spy);
parser.write(Buffer.from('I love robots\nEach '));
parser.write(Buffer.from('and Every One\n'));
parser.write(Buffer.from('\n'));
parser.write(Buffer.from('even you!'));

assert.deepEqual(spy.getCall(0).args[0], Buffer.from('I love robots\n'));
assert.deepEqual(spy.getCall(1).args[0], Buffer.from('Each and Every One\n'));
assert.deepEqual(spy.getCall(2).args[0], Buffer.from('\n'));
assert.equal(spy.callCount, 3);
});

it('flushes remaining data when the stream ends', () => {
const parser = new DelimiterParser({ delimiter: Buffer.from([0]) });
const spy = sinon.spy();
Expand Down

0 comments on commit 6a3ab65

Please sign in to comment.