Create stream from any value - function, array, string, buffer, promise, number, object and etc. Always returns a function that returns Transform Stream, using through2.
![dependency status](https://img.shields.io/david/hybridables/always-stream.svg)
Note: This is just a thin wrapper around callback2stream and value2stream.
Install
npm i always-stream --save
Usage
For more use-cases see the tests
const alwaysStream = require('always-stream')
Create a stream from any value.
Params
val
{Mixed}: Any type of value.[opts]
{Object|Function=}: Directly passed to value2stream, callback2stream and through2.[Promize]
{Function}: Promise constructor to be used when no support for native Promise. Can be 2nd argument.returns
{Function}: That when executed, returns TransformStream
, using through2
Example
var fs = require('fs')
var toStream = require('always-stream')
var numberStream = toStream(123)
numberStream().on('data', function (val) {
console.log(val)
})
var stringStream = toStream('str foo')
stringStream().on('data', function (val) {
console.log(val)
})
var readFile = toStream(fs.readFile)
readFile('package.json', 'utf8')
.on('data', function (val) {
var json = JSON.parse(val)
console.log(json.name)
})
.once('error', console.error)
.once('end', console.error)
var statFile = toStream(fs.statSync)
statFile('package.json')
.on('data', function (stats) {
console.log(stats)
})
.once('error', console.error)
.once('end', console.error)
var stringifyStream = toStream(JSON.stringify)
stringifyStream({ foo: 'bar' }, null, 2)
.on('data', function (val) {
console.log(val)
})
.once('error', console.error)
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.
![tunnckoCore github](https://img.shields.io/badge/github-@tunnckoCore-4183c4.svg)