Coder Social home page Coder Social logo

node-stream-spigot's Introduction

Stream Spigot

NPM

david-dm david-dm

A generator for (streams2) Readable streams, useful for testing or converting simple lazy functions into Readable streams, or just creating Readable streams without all the boilerplate.

var spigot = require("stream-spigot")

spigot.array(["ABCDEFG"]).pipe(process.stdout)
// ABCDEFG

spigot.array(["ABC", "DEF", "G"]).pipe(process.stdout)
// same as: (short form)
spigot(["ABC", "DEF", "G"]).pipe(process.stdout)
// ABCDEFG


// Create a stream out of a synchronous generator:
var count = 0
function gen() {
  if (count++ < 5) {
    return {val: count}
  }
}

spigot.sync({objectMode: true}, gen).pipe(...)
/*
{val: 1}
{val: 2}
{val: 3}
{val: 4}
{val: 5}
*/


// Create a more traditional Readable stream:
var source = spigot({objectMode: true}, function () {
  var self = this
  iterator.next(function (err, value) {
    if (err) return self.emit("error", err)
    self.push(value)
  })
})

source.pipe(...)

Usage

spigot([options,] _read)

Create a Readable stream instance with the specified _read method. Your _read method should follow the normal stream.Readable _read syntax. (I.e. it should call this.push(chunk))

spigot([options, ], array)

Create a Readable stream instance that will emit each member of the specified array until it is consumed. Creates a copy of the given array and consumes that -- if this will cause memory issues, consider implementing your own _read function to consume your array.

var Spigot = spigot.ctor([options,], _read)

Same as the above except provides a constructor for your Readable class. You can then create instances by using either var source = new Spigot() or var source = Spigot().

var Spigot = spigot.ctor([options,], array)

Same as the above except provides a constructor for your Readable class. You can then create instances by using either var source = new Spigot() or var source = Spigot().

spigot.array([options, ], array)

A manual version of the above to specify an array.

spigot.sync([options,] fn)

Create a readable instance providing a synchronous generator function. It will internally wrap your synchronous function as an async function.

Options

Accepts standard readable-stream options.

LICENSE

MIT

node-stream-spigot's People

Contributors

brycebaril avatar

Watchers

Matteo Collina avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.