object-stream-tools
Advanced tools
Comparing version 2.0.0 to 2.1.0
13
index.js
@@ -73,2 +73,12 @@ 'use strict' | ||
function streamToPromise(stream) { | ||
return new Promise((resolve, reject) => { | ||
const arr = [] | ||
stream | ||
.on('data', data => arr.push(data)) | ||
.on('error', reject) | ||
.on('end', () => resolve(arr)) | ||
}) | ||
} | ||
function filter(func) { | ||
@@ -112,3 +122,4 @@ return new stream.Transform({ | ||
reduce, | ||
promiseToStream | ||
promiseToStream, | ||
streamToPromise | ||
} |
{ | ||
"name": "object-stream-tools", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Useful tools for manipulating object streams. Will be especially helpful to developers used to map - filter - reduce approach of nodejs arrays.", | ||
@@ -28,3 +28,7 @@ "main": "index.js", | ||
"promise to stream", | ||
"promise to streams" | ||
"promise to streams", | ||
"streams to promise", | ||
"stream to promises", | ||
"array to stream", | ||
"stream to array" | ||
], | ||
@@ -31,0 +35,0 @@ "author": "Krzysztof Burlinski and Lukasz Gintowt", |
@@ -41,6 +41,7 @@ # object-stream-tools | ||
const jsonStream = require('JSONStream') | ||
ost.streamToSet(fs.createReadStream('./test/data.json') | ||
fs.createReadStream('../test/data.json') | ||
.pipe(jsonStream.parse('*')) | ||
.pipe(ost.map(obj => obj.requiredProperty))) | ||
.then(uniqueSet => { | ||
.pipe(ost.map(obj => obj.requiredProperty)) | ||
.pipe(ost.streamToSet()) | ||
.on('data', uniqueSet => { | ||
// here one get array of unique elements | ||
@@ -57,7 +58,8 @@ const uniqueArray = Array.from(uniqueSet.values()).sort() | ||
```js | ||
ost.streamToArray(dataStream() | ||
.pipe(ost.filter(e => e.property > 6))) | ||
.then(filteredObjects => | ||
// here you will get filtered objects | ||
) | ||
fs.createReadStream('../test/data.json') | ||
.pipe(jsonStream.parse('*')) | ||
.pipe(ost.filter(e => e.value > 6)) | ||
// here you will get filtered objects | ||
.pipe(jsonStream.stringify()) | ||
.pipe(process.stdout) | ||
``` | ||
@@ -111,8 +113,24 @@ | ||
```js | ||
ost.promiseToStream(myDbQueryThatReturnPromise()) | ||
.on('data', data => { | ||
// here you will get a real stream that you can pipe | ||
ost.promiseToStream(myDbQueryThatReturnPromise()) | ||
.on('data', data => { | ||
// here you will get a real stream that you can pipe | ||
}) | ||
``` | ||
#### stream to promise | ||
Very handy when you want to consume streams but rest of your application logic uses promises. | ||
```js | ||
ost.streamToPromise(fs.createReadStream('../test/data.json') | ||
.pipe(jsonStream.parse('*')) | ||
.pipe(ost.filter(e => e.value > 6))) | ||
.then(data => { | ||
// here you will get filtered objects | ||
}) | ||
``` | ||
## Please look at the tests for more use cases. |
@@ -144,3 +144,3 @@ 'use strict' | ||
tap.test('Test promise to stream on successful resolution', t => { | ||
tap.test('Test promise to stream on successful resolution', t => | ||
ost.promiseToStream(new Promise((resolve, reject) => { | ||
@@ -152,5 +152,5 @@ setTimeout(() => resolve([3, 6, 3, 6, 2, 4]), 10) | ||
.on('end', t.end) | ||
}) | ||
) | ||
tap.test('Test promise to stream on rejection', t => { | ||
tap.test('Test promise to stream on rejection', t => | ||
ost.promiseToStream(new Promise((resolve, reject) => { | ||
@@ -164,4 +164,10 @@ setTimeout(() => reject([3, 6, 3, 6, 2, 4]), 10) | ||
}) | ||
}) | ||
) | ||
tap.test('Test stream to promise', t => | ||
ost.streamToPromise(dataStream()) | ||
.then(objs => t.same(objs, data)) | ||
.catch(t.fail) | ||
) | ||
function dataStream() { | ||
@@ -168,0 +174,0 @@ return fs.createReadStream('./test/data.json') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14501
284
134
0