Skip to content

Commit e551961

Browse files
committed
Added stream transform filter and reduce
1 parent 676592a commit e551961

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 13-transform-filter-reduce
2+
3+
This examples shows how to use Transform streams to implement streaming data filter and reduce blocks.
4+
5+
6+
## Dependencies
7+
8+
Install all necessary dependencies with:
9+
10+
```bash
11+
npm install
12+
```
13+
14+
15+
## Run
16+
17+
To run the example:
18+
19+
```bash
20+
node index.js
21+
```
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Transform } from 'stream'
2+
3+
export class FilterByCountry extends Transform {
4+
constructor (country, options = {}) {
5+
options.objectMode = true
6+
super(options)
7+
this.country = country
8+
}
9+
10+
_transform (record, enc, cb) {
11+
if (record.country === this.country) {
12+
this.push(record)
13+
}
14+
cb()
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createReadStream } from 'fs'
2+
import { createGunzip } from 'zlib'
3+
import parse from 'csv-parse'
4+
import { FilterByCountry } from './filter-by-country.js'
5+
import { SumProfit } from './sum-profit.js'
6+
7+
const csvParser = parse({ columns: true })
8+
9+
// A small difference from the code presented in the book is that
10+
// here we have gzipped the data to keep the download size of the repository
11+
// as small as possible. For this reason we added an extra step that decompresses
12+
// the data on the fly. The final result doesn't change
13+
createReadStream('data.csv.gz')
14+
.pipe(createGunzip())
15+
.pipe(csvParser)
16+
.pipe(new FilterByCountry('Italy'))
17+
.pipe(new SumProfit())
18+
.pipe(process.stdout)

06-coding-with-streams/13-transform-filter-reduce/package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "13-transform-filter-reduce",
3+
"version": "1.0.0",
4+
"description": "",
5+
"type": "module",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "Luciano",
12+
"license": "ISC",
13+
"dependencies": {
14+
"csv-parse": "^4.10.1"
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Transform } from 'stream'
2+
3+
export class SumProfit extends Transform {
4+
constructor (options = {}) {
5+
options.objectMode = true
6+
super(options)
7+
this.total = 0
8+
}
9+
10+
_transform (record, enc, cb) {
11+
this.total += Number.parseFloat(record.profit)
12+
cb()
13+
}
14+
15+
_flush (cb) {
16+
this.push(this.total.toString())
17+
cb()
18+
}
19+
}

0 commit comments

Comments
 (0)