Skip to content

dolphin278/pipemaster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pipemaster

Simple tool to wrap your functions to work as streams. Functions can be both sync and async.

Installation

npm install pipemaster 

Usage

var FuncWrapper = require('pipemaster').FuncWrapper,
    streamFunc;

function asyncExample(x, callback) {
   callback(null, x * x); // first argument is an optional error.
}

streamFunc = new FuncWrapper(asyncExample);

streamFunc.write(2);  // streamFunc emits '4'
streamFunc.write(6);  // streamFunc emits '36'

Sync functions also supported:

var FuncWrapper = require('pipemaster').FuncWrapper,
    streamFunc;

function syncExample(x) {
   return 2 * x;
}

streamFunc = new FuncWrapper(syncExample, false); // 'false' is a flag for sync function

streamFunc.write(2);  // streamFunc emits '4'
streamFunc.write(6);  // streamFunc emits '12'

Filtering stream

Based on FuncWrapper, it takes sync or async function that returns true or false, and emits data event only when function returns true. Filtering functions also can be both sync and async ones.

var StreamFilter = require('pipemaster').StreamFilter,
    streamFunc;
function filterAsync(item, callback) {
    callback(null, item === 4);
}

streamFunc = new StreamFilter(filterAsync, true);

streamFunc.write(1);
streamFunc.write(4);
streamFunc.write(2);

// streamFunc emits only '4'

If you suggest pull requests, please include vows test suites.

About

Tool to wrap your functions so they works as streams.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published