
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
object-stream-tools
Advanced tools
Useful tools for manipulating object streams. Will be especially helpful to developers used to map - filter - reduce approach of nodejs arrays.
This package brings goodies of functional programming (map, filter, reduce) to node streams.
npm install --save object-stream-tools
Converts existing array to stream of objects. Useful if you want to inject/merge those object to the existing stream.
const ost = require('object-stream-tools')
ost.arrayToStream([{foo: 'bar'}, {web: 'scale'}])
.on('data', data => {
console.log(data)
})
.pipe(somewhereWritable)
Prints
[{foo: 'bar'}, {web: 'scale'}]
Its very useful if you want to get unique elements / set of values
const jsonStream = require('JSONStream')
fs.createReadStream('../test/data.json')
.pipe(jsonStream.parse('*'))
.pipe(ost.map(obj => obj.requiredProperty))
.pipe(ost.streamToSet())
.on('data', uniqueSet => {
// here one get array of unique elements
const uniqueArray = Array.from(uniqueSet.values()).sort()
})
If you just want to remove some objects from stream, you probably want to use filter function.
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)
Map is useful when you want to modify existing objects in the stream.
Reduce is useful if you want to get single object/value based on whole stream, but you dont want to load whole stream to memory.
Example: sum / average value of huge stream
const jsonStream = require('JSONStream')
fs.createReadStream('./test/data.json')
.pipe(jsonStream.parse('*'))
// pick required property you want to reduce over
.pipe(ost.map(obj => obj.requiredProperty))
.pipe(ost.reduce((acc, curr, i) => {
return acc + curr + i
}, 0))
.on('data', reducedValue => {
// here you will get reduced value
})
Here is example with buffered/string input output:
const jsonStream = require('JSONStream')
fs.createReadStream('./test/data.json')
.pipe(jsonStream.parse('*'))
.pipe(ost.map(obj => obj.requiredProperty))
.pipe(ost.reduce((acc, curr, i) => {
return acc + curr + i
}, 0)))
.on('data', reducedValue =>
// here you will get reduced value
})
.pipe(jsonStream.stringify())
.pipe(process.stdout)
Please note that if you do not pass initial value reduce function will start in (prev, curr, i) mode. Objects/Array/Reduce
It is a useful helper if you dealing with a lot of smaller data that are wrapped in Promise API, ex:
ost.promiseToStream(myDbQueryThatReturnPromise())
.on('data', data => {
// here you will get a real stream that you can pipe
})
Very handy when you want to consume streams but rest of your application logic uses promises.
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
})
Find is super handy if we want to quickly check if vale/objects exists in the stream. Think about it as a grep on the steroids.
ost.streamToPromise(fs.createReadStream('../test/data.json')
.pipe(jsonStream.parse('*'))
.pipe(ost.find(e => e.value > 6)))
.then(foundObj => {
// here you will get found first object that matches condition
// or undefined when there were none that matches
})
FAQs
Useful tools for manipulating object streams. Will be especially helpful to developers used to map - filter - reduce approach of nodejs arrays.
The npm package object-stream-tools receives a total of 10 weekly downloads. As such, object-stream-tools popularity was classified as not popular.
We found that object-stream-tools demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.