Transform any value to stream. Create a stream from any value - string, array, buffer, number, promise or even Error object.
Note: Be aware of that if you pass Error
object you will still recieve it as value. It won't fire the error
event on created stream. That will only happen if you pass rejected promise. And all this is intentional - you just pass a value and recieve a stream with that value.
Install
npm i value2stream --save
Usage
For more use-cases see the tests
const value2stream = require('value2stream')
Create a stream from any value.
Params
val
{Mixed}: Any type of value except function. Use callback2stream for functions.[opts]
{Object|Function=}: Directly passed to promise2stream and through2, otherwise Promise contstructor.[Promize]
{Function}: Promise constructor to be used when no support for native Promise.returns
{Stream}
Example
var toStream = require('value2promise')
toStream(123).on('data', function (val) {
console.log(val)
})
toStream('str foo').on('data', function (val) {
console.log(val)
})
toStream({ foo: 'bar' }).on('data', function (val) {
console.log(val)
})
toStream({ foo: 'bar' }, { objectMode: false })
.once('error', function (err) {
console.log(err instanceof Error)
console.log(err)
})
var fails = Promise.resolve({ a: 'b' })
toStream(fails, { objectMode: false })
.once('error', function (err) {
console.log(err instanceof Error)
console.log(err)
})
var promise = Promise.resolve('foo bar')
toStream(promise).on('data', function (val) {
console.log(val)
})
var rejected = Promise.reject(new Error('err msg'))
toStream(rejected).once('error', function (err) {
console.log(err instanceof Error)
console.log(err.message)
})
Related
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.