value2stream

1.0.0 • Public • Published

value2stream npmjs.com The MIT License npm downloads

Transform any value to stream. Create a stream from any value - string, array, buffer, number, promise or even Error object.

code climate standard code style travis build status coverage status dependency status

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

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.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github

Package Sidebar

Install

npm i value2stream

Weekly Downloads

3

Version

1.0.0

License

MIT

Last publish

Collaborators

  • vanchoy
  • tunnckocore