Awesome
value2stream
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')
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) // => 123
})
toStream('str foo').on('data', function (val) {
console.log(val) // => 'str foo'
})
// not throws if `opts.objectMode: true` (default)
toStream({ foo: 'bar' }).on('data', function (val) {
console.log(val) // => { foo: 'bar' }
})
// throws when non-object mode
toStream({ foo: 'bar' }, { objectMode: false })
.once('error', function (err) {
console.log(err instanceof Error) // => true
console.log(err) // => [Error: Invalid non-string/buffer chunk]
})
// same applies if non-object and promise resolves object
var fails = Promise.resolve({ a: 'b' })
toStream(fails, { objectMode: false })
.once('error', function (err) {
console.log(err instanceof Error) // => true
console.log(err) // => [Error: Invalid non-string/buffer chunk]
})
var promise = Promise.resolve('foo bar')
toStream(promise).on('data', function (val) {
console.log(val) // => 'foo bar'
})
var rejected = Promise.reject(new Error('err msg'))
toStream(rejected).once('error', function (err) {
console.log(err instanceof Error) // => true
console.log(err.message) // => 'err msg'
})
Related
- callback2stream: Transform sync, async or generator function to Stream. Correctly handle… more | homepage
- native-promise: Get native
Promise
or falsey value if not available. | homepage - to-stream: Turn an array to a Node.js stream | homepage
- promise2stream: Transform ES2015 Promise to Stream - specifically, Transform Stream using… more | homepage
- promise2thunk: Convert (transform) promise to thunk, just like was in co@3 | homepage
- relike: Simple promisify a callback-style function with sane defaults. Support promisify-ing… more | homepage
- thunk2promise: Transform or convert thunk to Bluebird Promise. | homepage
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.