From ff4c10f3be47b5720b5164e7bb84fe2d5e3fa66f Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Fri, 17 Jun 2016 21:20:34 -0700 Subject: [PATCH] Resolve object mode as an array of objects Closes #7 --- index.js | 3 +++ test.js | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/index.js b/index.js index 6c5e054..64ca5bb 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,9 @@ function fromReadable (stream) { return promise .then(function concat (parts) { + if (stream._readableState && stream._readableState.objectMode) { + return parts + } return Buffer.concat(parts.map(bufferize)) }) } diff --git a/test.js b/test.js index 5d8a174..adda1d8 100644 --- a/test.js +++ b/test.js @@ -69,6 +69,18 @@ describe('stream-to-promise', function () { }) }) + it('returns an array for object streams', function () { + var objectStream = new Stream.Readable({objectMode: true}) + objectStream._read = function noop () {} + var promise = streamToPromise(objectStream) + objectStream.emit('data', {foo: 'bar'}) + objectStream.emit('data', {baz: 'qux'}) + objectStream.emit('end') + return promise.then(function (results) { + expect(results).to.deep.equal([{foo: 'bar'}, {baz: 'qux'}]) + }) + }) + it('rejects on stream errors', function () { var err = new Error() readable.emit('error', err)