Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Unify processAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
Noviny authored and miaowing committed Mar 30, 2020
1 parent b398ced commit ee4dc06
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lib/transports/nodemailer/getSendOptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var assign = require('object-assign');

var processAddress = require('./processAddress');
var processAddress = require('../../util/processAddress');
var getRecipients = require('./getRecipients');

var defaultOptions = {
Expand Down
48 changes: 0 additions & 48 deletions lib/transports/nodemailer/processAddress.js

This file was deleted.

20 changes: 12 additions & 8 deletions lib/util/processAddress.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var truthy = require('./truthy');

var ADDRESS_RX = /.*<(.*)>/;
var ADDRESS_RX = /(.*)? <(.*)>/;

function processAddress (data) {
var rtn = {
email: '',
address: '',
};
if (typeof data === 'object') {
Expand All @@ -15,19 +14,24 @@ function processAddress (data) {
data.name = [data.name.first, data.name.last].filter(truthy).join(' ');
}
// process { name: 'Jed Watson', email: 'user@keystonejs.com' } into 'name <email>' format
if (data.name && data.email) {
if (data.address) {
rtn.address = data.address;
} else if (data.name && data.email) {
rtn.address = data.name + ' <' + data.email + '>';
}
rtn.name = data.name;
rtn.email = data.email;

rtn.name = data.name ? data.name : '';
rtn.email = data.email ? data.email : '';
} else if (typeof data === 'string') {
rtn.address = data;
// split email out from 'name <email>' format
// split email out from 'name <email>' format, and add name
var parsed = ADDRESS_RX.exec(data);
if (parsed && parsed.length === 2) {
rtn.email = parsed[1];
if (parsed && parsed.length === 3) {
rtn.name = parsed[1] ? parsed[1] : parsed[2];
rtn.email = parsed[2];
} else {
rtn.email = data;
rtn.name = data;
}
}
return rtn;
Expand Down
2 changes: 1 addition & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('nodemailer transport', function () {
});

describe('processAddress', function () {
var processAddress = require('../lib/transports/nodemailer/processAddress');
var processAddress = require('../lib/util/processAddress');

it('should set a string provided email', function () {
var res = processAddress(testEmail);
Expand Down

0 comments on commit ee4dc06

Please sign in to comment.