GithubHelp home page GithubHelp logo

fpipe's Introduction

Function Pipe

A function pipe eases the construction of extensible processing pipelines.

Each step in the pipeline takes the result of the prior step as input, and produces its own result, which in turn, is used by the next step in the pipeline, until processing is complete and a final result is produced. Each step in the pipeline can be thought of as middleware, but unlike the connect framework, the inputs and outputs are not related to nodejs' original callback pattern.

2018 Breaking Changes

The 2018 version 1.0.0 has very little in common with the version published in 2012. The 2018 version of the Pipe is exclusively ES6, and is intended for use in nodejs.

Getting Started

Create a pipeline by constructing an instance of the Pipe class, optionally specifying initial processing steps, then as needed, add additional processing steps using the .pipe() method, and eventually, execute the pipeline using the .process() method.

const res = await new Pipe(fn1)
  .pipe(fn2)
  .pipe(fn3)
  .process('an argument for fn1');

console.log(`Result of pipeline processing: ${res}`);

The result of each step in the process cascades into the next step, just like Promise's .then() method.

arg ---v
   fn1(arg1) ---v
            fn2(arg2) ---v
                     fn3(arg3) ---v
                                  result

The constructor can take multiple steps...

const res = await new Pipe(fn1, fn2, fn3).process('an argument for fn1');

console.log(`Result of pipeline processing: ${res}`);

.pipe() can take multiple steps...

const res = await new Pipe(fn2)
  .pipe(
    fn2,
    fn3
  )
  .process(42);

Arguments passed to .process() are projected into the first step...

const res = await new Pipe((x, y) => x + y)
  .pipe(x => x + 1)
  .pipe(x => x * 2)
  .process(10, 10);

Each step's result can be projected down stream in the pipe...

const res = await new Pipe((x, y) => [x * 2, y * 5], true)
  .pipe(
    (x, y) => [x + y, 1],
    true
  )
  .pipe((x, y) => (x + y) * 2)
  .process(5, 2);

Errors propagate to the caller...

const stdout = console.log.bind(console);
try {
  const res = await new Pipe()
    .pipe(() => stdout('click'))
    .pipe(() => stdout('click'))
    .pipe(() => {
      throw new Error('Boom!');
    })
    .pipe(() => stdout('click'))
    .pipe(() => stdout('click'))
    .process();
} catch (err) {
  assert.equal('Boom!', err.message);
}

fpipe's People

Stargazers

momo avatar Airyland avatar Todd Bashor avatar Yornaath avatar steve flitcroft avatar

Watchers

James Cloos avatar  avatar

Forkers

sgml

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.