GithubHelp home page GithubHelp logo

nvdnkpr / from2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hughsk/from2

0.0 3.0 0.0 115 KB

Convenience wrapper for ReadableStream, with an API lifted from "from" and "through2"

License: MIT License

from2's Introduction

from2 Flattr this!experimental

Convience wrapper for readable-stream's ReadableStream base class, with an API lifted from from and through2.

Usage

from2

stream = from2([opts], readFunction)

Where opts are the options to pass on to the ReadableStream constructor, and readFunction(size) is called when data is requested from the stream. Consul the documentation of stream.Readable for the exact rules of the readFunction (i.e. this._read).

For example, here's a readable stream that emits the contents of a given string:

var from = require('from2')

module.exports = fromString

function fromString(string) {
  return from(function(size, next) {
    // if there's no more content
    // left in the string, close the stream.
    if (string.length <= 0) return this.push(null)

    // Pull in a new chunk of text,
    // removing it from the string.
    var chunk = string.slice(0, size)
    string = string.slice(size)

    // Emit "chunk" from the stream.
    this.push(chunk)
  })
}

// pipe "hello world" out
// to stdout.
fromString('hello world').pipe(process.stdout)

stream = from2.obj([opts], read)

Shorthand for from2({ objectMode: true }, readFunction).

createStream = from2.ctor([opts], readFunction)

If you're creating similar streams in quick succession you can improve performance by generating a stream constructor that you can reuse instead of creating one-off streams on each call.

Takes the same options as from2, instead returning a constructor which you can use to create new streams.

License

MIT. See LICENSE.md for details.

from2's People

Contributors

hughsk avatar mariocasciaro avatar

Watchers

Navid Nikpour 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.