Skip to content

Commit 7ab4546

Browse files
committed
Node streams
1 parent 0d65537 commit 7ab4546

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

node-streams/actualbears.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
grizzly
2+
brown
3+
polar
4+
panda

node-streams/bears.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
grizzly
2+
brown
3+
polar
4+
koala
5+
panda

node-streams/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var fs = require('fs')
2+
var Transform = require('stream').Transform
3+
var inherits = require('util').inherits
4+
5+
function ActualBears () {
6+
Transform.call(this)
7+
}
8+
inherits(ActualBears, Transform)
9+
10+
ActualBears.prototype._transform = function (chunk, enc, done) {
11+
chunk = chunk.toString().split('\n').filter(function (bear) {
12+
return (bear !== 'koala')
13+
}).join('\n')
14+
this.push(chunk)
15+
done()
16+
}
17+
18+
var read = fs.createReadStream('bears.txt')
19+
var write = fs.createWriteStream('actualbears.txt')
20+
21+
read.pipe(new ActualBears()).pipe(write)

node-streams/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "streams",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"author": "Kyle Robinson Young <kyle@dontkry.com> (http://dontkry.com)",
7+
"license": "MIT",
8+
"devDependencies": {
9+
}
10+
}

0 commit comments

Comments
 (0)