module.exports = require('./s-pipe');
module.exports = require('./s-pipe');
This package provides a range of utility functions with simple/nice API for playing with streams. For those who dont know, streams are a nice node.js abstraction for handling any kind of IO. It also works through browserify.
Streams can be used to solve a great range of problems. For a introduction on streams, read @substack 's stream handbook.
The API exposed by this module was first proposed by @mlanza in an underscore PR and is based on lisp chaining syntax
var spipe = require('s-pipe');
var array = require('s-pipe/lib/array');
var filter = require('s-pipe/lib/filter');
var map = require('s-pipe/lib/map');
var reduce = require('s-pipe/lib/reduce');
spipe(array([1,2,3,4,5]))
(filter, function(n) { return n > 3; })
(map, function(n) { return n * n; })
(reduce, function(result, n) { return result + n; })
(); // returns [41]
Explanation:
Data is processed by each layer of the pipeline as soon as its available, no buffering ever happens. This opens the possibility for doing things like:
var range = require('s-pipe/lib/range');
spipe(range(1, 1000000000))
(map, function(n) { return {square: n * n, number: n}; })
(find, function(obj) { return obj.square >= 81; })
(map, function(n) { return n.number; })
(); // [9]
The 'range' function above returns a readable stream that yields numbers from 1 to 1000000000. The above chain returns very fast because the pipeline will be closed as soon as 81 reaches the 'find' stream.
As shown above, the functions are kept in separate files for optimzed builds with browserify.