Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created April 21, 2014 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/11153491 to your computer and use it in GitHub Desktop.
Save joyrexus/11153491 to your computer and use it in GitHub Desktop.
through2-map examples

A quick demo of through2-map usage.

Extract second column of each row in a CSV file

fs = require 'fs'
csv = require 'csv2'
map = require 'through2-map'

extract = map(objectMode: true, (row) -> row[1] + '\n')

fs.createReadStream('data.csv')
  .pipe(csv())
  .pipe(extract)
  .pipe(process.stdout)   # B, b1, b2, b3

Uppercase strings

{Readable} = require 'stream'

# a simple transform stream
tx = map((d) -> d.toString().toUpperCase())

# a simple source stream
rs = new Readable
rs.push 'the quick brown fox jumps over the lazy dog!\n'
rs.push null 

rs.pipe(tx)
  .pipe(process.stdout)   # THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!
A B C
a1 b1 c1
a2 b2 c2
a3 b3 c3

A quick demo of through2-map usage.

Extract second column of each row in a CSV file

fs = require 'fs'
csv = require 'csv2'
map = require 'through2-map'

extract = map(objectMode: true, (row) -> row[1] + '\n')

fs.createReadStream('data.csv')
  .pipe(csv())
  .pipe(extract)
  .pipe(process.stdout)   # B, b1, b2, b3

Uppercase strings

{Readable} = require 'stream'

# a simple transform stream
tx = map((d) -> d.toString().toUpperCase())

# a simple source stream
rs = new Readable
rs.push 'the quick brown fox jumps over the lazy dog!\n'
rs.push null 

rs.pipe(tx)
  .pipe(process.stdout)   # THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!
{
"name": "through2-map-demo",
"version": "0.1.1",
"description": "Quick demo of through2-map usage.",
"dependencies": {
"through2-map": "~1.2.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment