Skip to content

Instantly share code, notes, and snippets.

@trevorhreed
Last active July 21, 2017 17:17
Show Gist options
  • Save trevorhreed/6be414291a4bef2b37318cf1429c8355 to your computer and use it in GitHub Desktop.
Save trevorhreed/6be414291a4bef2b37318cf1429c8355 to your computer and use it in GitHub Desktop.
Medium - How A Printer From The 80’s Can Improve Your Programming - 1
// COMMON CODE
const DEFAULT_SENDER = '[email protected]';
const Email = require('some-email-service');
// EXAMPLE 1
let emailOne = (person, subject, body) => {
let email = new Email();
email.from = DEFAULT_SENDER;
email.to = person.email;
email.subject = subject;
email.body = body;
email.send();
}
let emailMany = (people, subject, body) => {
people.forEach((person) => {
let email = new Email();
email.from = DEFAULT_SENDER;
email.to = person.email;
email.subject = subject;
email.body = body;
email.send();
});
}
let sendEmails = (peopleOrPerson, subject, body) => {
if(Array.isArray(peopleOrPerson)){
emailMany(peopleOrPerson, subject, body);
}else{
emailOne(peopleOrPerson, subject, body);
}
}
// EXAMPLE 2
let sendEmails = (tos, subject, body) => {
if(!Array.isArray(tos)) tos = [tos];
tos.forEach((to) => {
email = new Email();
email.from = DEFAULT_SENDER;
email.to = to;
email.subject = subject;
email.body = body;
email.send();
});
}
// USAGE
sendEmails(people.map(x => x.email), "You're Invited", "Event: 2pm on Saturday");
sendEmails(people.map(x => x.email), "Don't Forget", "Event: 2pm on Saturday");
sendEmails(people.pop(), "You're The Guest of Honor", "Event: 2pm on Saturday");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment