Skip to content

Instantly share code, notes, and snippets.

@roelvan
Created February 1, 2017 03:25
Show Gist options
  • Save roelvan/e4a2d6d983d7aa54ebfc5d11295633bf to your computer and use it in GitHub Desktop.
Save roelvan/e4a2d6d983d7aa54ebfc5d11295633bf to your computer and use it in GitHub Desktop.
Extract first name from an array of email addresses.
const _ = require('lodash')
const fs = require('fs')
const json2csv = require('json2csv')
const emails = [
"firstname.lastname@test.be"
]
const mappedMails = _.compact(_.map(emails, email => {
const firstName = _.capitalize(email.split('.')[0])
if (firstName.indexOf('@') < 0) {
return { "firstName": firstName, "email": email }
}
}))
try {
const csv = json2csv({ data: mappedMails, fields: [ 'firstName', 'email' ] })
fs.writeFile('./emails.csv', csv, (err) => {
if (err) throw err
console.log('file saved')
})
} catch (err) {
console.error(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment