Skip to content

Commit 0c59063

Browse files
committed
Function added to parse CSV file
1 parent 8a1f636 commit 0c59063

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

email_template.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
<head><meta charset='utf-8'></head>
3+
<body>
4+
<p>Hey FIRST_NAME,</p>
5+
<br>
6+
<p>
7+
How are you doing? I'm well. It's been like NUM_MONTHS_SINCE_CONTACT months
8+
since we last spoke. I'm learning to code full-time at Fullstack Academy. It's REALLY difficult but I love it.
9+
</p>
10+
<p>
11+
I've been blogging about my experiences at Fullstack on my blog.
12+
Check out my new posts here:
13+
<a href="http://codingmeow.tumblr.com/">My Tumblr</a>
14+
</p>
15+
<p>
16+
Laters,<br>
17+
Jenny
18+
</p>
19+
</body>
20+
</html>

friend_list.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
firstName,lastName,numMonthsSinceContact,emailAddress
2+
Scott,D'Alessandro,0,[email protected]

tumblr_mailer.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var fs = require('fs');
2+
3+
var csvFile = fs.readFileSync("friend_list.csv","utf8");
4+
var emailTemplate = fs.readFileSync('email_template.html', 'utf8');
5+
6+
function csvParse(csvFile){
7+
var arrayOfObjects = [];
8+
var arr = csvFile.split("\n");
9+
var newObj;
10+
11+
keys = arr.shift().split(",");
12+
13+
arr.forEach(function(contact){
14+
contact = contact.split(",");
15+
newObj = {};
16+
17+
for(var i =0; i < contact.length; i++){
18+
newObj[keys[i]] = contact[i];
19+
}
20+
21+
arrayOfObjects.push(newObj);
22+
23+
});
24+
25+
return arrayOfObjects;
26+
}
27+
28+
friendList = csvParse(csvFile);
29+
30+
friendList.forEach(function(row){
31+
32+
var firstName = row["firstName"];
33+
var numMonthsSinceContact = row["numMonthsSinceContact"];
34+
35+
// we make a copy of the emailTemplate variable to a new variable to ensure
36+
// we don't edit the original template text since we'll need to us it for
37+
// multiple emails
38+
39+
var templateCopy = emailTemplate;
40+
41+
// use .replace to replace FIRST_NAME and NUM_MONTHS_SINCE_CONTACT with firstName and monthsSinceLastContact
42+
templateCopy = templateCopy.replace(/FIRST_NAME/gi,
43+
firstName).replace(/NUM_MONTHS_SINCE_CONTACT/gi, numMonthsSinceContact);
44+
45+
console.log(templateCopy);
46+
47+
48+
});

0 commit comments

Comments
 (0)