From 6a3ab65f09a76284396a21dd78a8b16d53b0b2c1 Mon Sep 17 00:00:00 2001 From: Rodrigo Saboya Date: Thu, 25 Jan 2018 18:59:28 -0200 Subject: [PATCH] feat(parsers): Adding option to include delimiter in the DelimiterParser transform. (#1453) --- lib/parsers/delimiter.js | 3 ++- test/parser-delimiter.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/parsers/delimiter.js b/lib/parsers/delimiter.js index de91c7995..f9b74757a 100644 --- a/lib/parsers/delimiter.js +++ b/lib/parsers/delimiter.js @@ -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); } @@ -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; diff --git a/test/parser-delimiter.js b/test/parser-delimiter.js index 8968395c4..efecd542d 100644 --- a/test/parser-delimiter.js +++ b/test/parser-delimiter.js @@ -15,6 +15,7 @@ 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')); @@ -22,6 +23,24 @@ describe('DelimiterParser', () => { 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();