Skip to content

Commit 2b4abb4

Browse files
author
James Halliday
committed
whale example
1 parent ca4cfc2 commit 2b4abb4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

streams/whale.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// first do:
2+
// curl -s http://www.gutenberg.org/cache/epub/2701/pg2701.txt > mobydick.txt
3+
4+
var fs = require('fs');
5+
var path = require('path');
6+
var through = require('through2');
7+
var combine = require('stream-combiner2')
8+
var split = require('split');
9+
10+
read('mobydick.txt')
11+
.pipe(replace(/\s+/g, '\n'))
12+
.pipe(filter(/whale/i))
13+
.pipe(linecount(function (count) {
14+
console.log(count)
15+
}))
16+
;
17+
18+
function read (file) {
19+
return fs.createReadStream(path.join(__dirname, file));
20+
}
21+
22+
function replace (pattern, replacement) {
23+
return combine(split(), through(function (buf, enc, next) {
24+
var line = buf.toString('utf8');
25+
this.push(line.replace(pattern, replacement) + '\n');
26+
next();
27+
}));
28+
}
29+
30+
function filter (pattern) {
31+
return combine(split(), through(function (buf, enc, next) {
32+
var line = buf.toString('utf8');
33+
if (pattern.test(line)) this.push(line + '\n');
34+
next();
35+
}));
36+
}
37+
38+
function linecount (cb) {
39+
var count = 0;
40+
return combine(split(), through(write, end));
41+
function write (buf, enc, next) { count ++; next() }
42+
function end () { cb(count) }
43+
}

0 commit comments

Comments
 (0)