-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support rfc6854 and parsing for From, Sender and Reply-To #31
Support rfc6854 and parsing for From, Sender and Reply-To #31
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are my thoughts while running through this. If you see what I'm getting at, feel free to clean this up (oh also console.logs throughout). Otherwise I'll get to this within a couple weeks and clean it up myself.
lib/email-addresses.js
Outdated
|
||
|
||
function addrListGiveResult(ast) { | ||
return _giveResult(ast, ['group', 'mailbox']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it intentional that addrListGiveResult
, fromGiveResult
, and senderGiveResult
have the same body of code? Is there a reason they may need to have different functionality? Either one should be different or there should be one function and no giveResMap
. I do see that you wanted to hide the interface of _giveResult
, but no need to have different function names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I can clean that up now. It happened because they actually have different definitions, but it turns out what you want from the results for each is achieved the same way. I started out with different calls to _giveResults()
. They ended up identical when I carefully examined the grammar and realized that everything boils down to address
, which is mailbox / group
anyway.
lib/email-addresses.js
Outdated
ret.addresses = []; | ||
for (i = 0; i < addresses.length; i += 1) { | ||
addr = addresses[i]; | ||
if (addr.seen) continue; | ||
addr.seen = true; | ||
if (addr.name === 'group') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It happens to work, because findAllNodes
is first called on group
and then on mailbox
, but considering this loop by itself one might wonder if a node in a group
could be processed before the group
it is part of. If you called _giveResult(ast, ['mailbox', 'group'])
somewhere else, you would get the wrong result, which is confusing.
I think it would be simpler to:
- make this two functions (no recursion)
- process all the 'group' nodes first (loop over
findAllNodes('group', ast)
, then process non group nodes underneath that group, marking seen) - process all the non-group nodes second (loop over
findAllNodes('mailbox', ast)
, skipping if "seen")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both achieve things the same way, the "correct" way would be to know just how deep it's OK to go in the AST, IMHO. That way you could guarantee that looking for mailboxes under a group wouldn't happen.
lib/email-addresses.js
Outdated
function parseOneAddressSimple(opts) { | ||
var result; | ||
|
||
function _opts (opts, startAt) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge this with handleOpts
since it's not otherwise clear why there are two functions _opts
and handleOpts
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because _opts gives the defaults for the named functions. When called as a library (require('email-addresses')('foo@bar')
) those defaults don't apply.
lib/email-addresses.js
Outdated
@@ -528,7 +528,21 @@ function parse5322(opts) { | |||
obsGroupList | |||
)()); | |||
} | |||
|
|||
|
|||
function fromSpec() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// RFC 6854 2.1. Replacement of RFC 5322, Section 3.6.2. Originator Fields
// from = "From:" (mailbox-list / address-list) CRLF
lib/email-addresses.js
Outdated
)()); | ||
} | ||
|
||
function senderSpec() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// RFC 6854 2.1. Replacement of RFC 5322, Section 3.6.2. Originator Fields
// sender = "Sender:" (mailbox / address) CRLF
lib/email-addresses.js
Outdated
address | ||
)()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have a function for reply-to while we're here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured we don't need it because reply-to is just address-list, so it's superfluous.
test/email-addresses.js
Outdated
|
||
result = fxn("Managing Partners:ben@example.com,carol@example.com;"); | ||
t.ok(result, "Parse group for Sender:"); | ||
t.equal(result.length, undefined, "Result is not an array"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty loose test :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mea culpa, I know. I got tired :) Nobody likes writing tests. I'll improve.
Addressed the changes that were simple. Only remaining one might be giveResult() - let me know if you think it's important now it's contained to not be changeable. |
whitespace add comments for originator fields, move to section 3.6.2 refactor giveResult refactor _opts process groups and mailboxes in one pass dfs instead of bfs so results are obviously in parse order quotes
I had a bunch of extra time so I refactored a few things that were bugging me, beyond the original scope of the pr. I modified one of the test cases you wrote very slightly, but otherwise it is as you wrote the tests. The options read a bit more sanely now, so it should be easy to get the module function to do what you want or add more named functions. I also added some misc. "startAt" options that seemed like they might be interesting. I'll merge this now -- ping me if I changed something you didn't like. |
Looks great, thanks a lot! When will you publish? |
@baudehlo Just published 3.0.0 on npm |
Fixes #28
Updated docs and tests.