Permalink
Browse files

updated docs with streaming map pipe example

  • Loading branch information...
1 parent b3b7726 commit c79ae3aa9f40428da84ccf1c81187d529c2e6c9c @substack committed Jan 30, 2012
Showing with 50 additions and 14 deletions.
  1. +40 −3 README.md
  2. +10 −11 examples/stream_map.js
View
@@ -67,10 +67,47 @@ be chained.
**proc1.pipe(proc2)**
-Similar to node's Stream.pipe, this is modeled after unix command
+Similar to node's `Stream.pipe`, this is modeled after unix command
piping. The stdout of in_proc is directed to the stdin of out_proc.
-Stderr of proc1 is directed to stderr of proc2. This method chains by
-returning proc2.
+Stderr of `proc1` is directed to stderr of `proc2`. This method chains by
+returning `proc2`.
+
+`proc2` can also be a node `Stream` object and can be interleaved with piping to
+commands:
+
+ var $p = require('procstreams');
+
+ $p('cat tests/fixtures/10lines.txt')
+ .pipe('grep even')
+ .pipe('wc -l')
+ .pipe(process.stdout)
+
+If your `Stream` object has a `write()` function and emits `'data'` events then
+you can interleave shell commands with streaming map functions:
+
+ var $p = require('../')
+ var Stream = require('stream').Stream
+
+ var grepEven = new Stream
+ grepEven.writable = true
+ grepEven.readable = true
+
+ var data = ''
+ grepEven.write = function (buf) { data += buf }
+ grepEven.end = function () {
+ this.emit('data', data
+ .split('\n')
+ .map(function (line) { return line + '\n' })
+ .filter(function (line) { return line.match(/even/) })
+ .join('')
+ )
+ this.emit('end')
+ }
+
+ $p('cat ../tests/fixtures/10lines.txt')
+ .pipe(grepEven)
+ .pipe('wc -l')
+ .pipe(process.stdout)
**proc1.then(proc2)**
@@ -1,24 +1,23 @@
-var $p = require('../');
-var Stream = require('stream').Stream;
+var $p = require('../')
+var Stream = require('stream').Stream
-var grepEven = new Stream;
-grepEven.readable = true;
-grepEven.writable = true;
+var grepEven = new Stream
+grepEven.writable = true
+grepEven.readable = true
-var data = '';
-grepEven.write = function (buf) { data += buf };
+var data = ''
+grepEven.write = function (buf) { data += buf }
grepEven.end = function () {
this.emit('data', data
.split('\n')
.map(function (line) { return line + '\n' })
.filter(function (line) { return line.match(/even/) })
.join('')
- );
- this.emit('end');
-};
+ )
+ this.emit('end')
+}
$p('cat ../tests/fixtures/10lines.txt')
.pipe(grepEven)
.pipe('wc -l')
.pipe(process.stdout)
-;

0 comments on commit c79ae3a

Please sign in to comment.