Skip to content

Commit

Permalink
feat: noCollect option
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 15, 2019
1 parent ee47bf0 commit 8adbdf8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
20 changes: 11 additions & 9 deletions README.md
Expand Up @@ -29,15 +29,11 @@ const streamPromise = require("stream-promise");

streamPromise(someReadableStream);

someReadableStream.then(result => {
console.log("Concatenated stream output", result);
});
someReadableStream.then(result => { console.log("Concatenated stream output", result); });

streamPromise(someWritabletream);

someReadableStream.then(result => {
console.log("Cumulated stream output", result);
});
someReadableStream.then(result => { console.log("Cumulated stream output", result); });
```

Already emitted data is accessible at `emittedData` property
Expand All @@ -51,11 +47,17 @@ const streamToPromise = require("stream-promise/to-promise");

const someReadableStreamPromise = streamPromiseTo(someReadableStream);

someReadableStreamPromise.then(result => {
console.log("Concatenated stream output", result);
});
someReadableStreamPromise.then(result => { console.log("Concatenated stream output", result); });
```

## Supported options

### noCollect `boolean` (default: `false`)

Applicable to _readable_ streams. Set to true, if it's not intended to gather stream result and resolve with it.

Then the only purpose of promise would be to indicate a moment when data stream is finalized

### Tests

```bash
Expand Down
12 changes: 8 additions & 4 deletions to-promise.js
@@ -1,9 +1,11 @@
"use strict";

const toShortString = require("es5-ext/to-short-string-representation")
const isObject = require("es5-ext/object/is-object")
, toShortString = require("es5-ext/to-short-string-representation")
, isStream = require("is-stream");

module.exports = stream => {
module.exports = (stream, options = {}) => {
if (!isObject(options)) options = {};
if (!isStream(stream)) throw new TypeError(`${ toShortString(stream) } is not a stream`);
if (!isStream.readable(stream) && !isStream.writable(stream)) {
throw new TypeError(
Expand All @@ -15,7 +17,9 @@ module.exports = stream => {
stream.on("error", reject);

if (isStream.readable(stream)) {
if (stream._readableState.objectMode) {
if (options.noCollect) {
result = undefined;
} else if (stream._readableState.objectMode) {
result = [];
stream.on("data", data => result.push(data));
} else {
Expand All @@ -42,6 +46,6 @@ module.exports = stream => {
stream.on("finish", () => resolve());
}
});
if (isStream.readable) promise.emittedData = result;
if (isStream.readable && !options.noCollect) promise.emittedData = result;
return promise;
};

0 comments on commit 8adbdf8

Please sign in to comment.