Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

value2stream

hybridables29MIT1.0.0

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

any, anything, buffer, convert, create, create-stream, errors, everything, handle, make, number, promise, str, stream, string, to, to-stream, transform, val, value, value-stream, value-to-stream

readme

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'
})

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

changelog

1.0.0 - 2016-05-06

  • Release v1.0.0 / npm@v1.0.0
  • add keywords
  • add related libs
  • update readme
  • fix failing tests
  • test: fire error when promise resolves object in non-object mode
  • update/fix docs example
  • add test for objectMode true/false
  • generate docs, readme
  • implement

0.0.0 - 2016-05-06

  • Initial commit