Skip to content

Instantly share code, notes, and snippets.

@siygle
Created January 23, 2018 13:01
Show Gist options
  • Save siygle/759ec80f98da6707db08a8ab565028eb to your computer and use it in GitHub Desktop.
Save siygle/759ec80f98da6707db08a8ab565028eb to your computer and use it in GitHub Desktop.
Parse CSV, filter and then output to another file (e.g, Email)
const parse = require('csv-parse')
const fs = require('fs')
const transform = require('stream-transform')
const isEmail = require('is-email')
const input = fs.createReadStream('./input.csv')
const output = fs.createWriteStream('./output.csv')
const parser = parse({ delimiter: ',' })
const transformer = transform((record, callback) => {
if (Array.isArray(record) && isEmail(record[4])) {
callback(null, `${record[4]}\n`)
} else {
callback(null)
}
}, { parallel: 5 })
input.pipe(parser).pipe(transformer).pipe(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment