Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-stream-tools

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-stream-tools - npm Package Compare versions

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
}

8

package.json
{
"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')

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc