Skip to content
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

Merged
merged 12 commits into from
Jun 20, 2017

Conversation

baudehlo
Copy link
Contributor

Fixes #28

Updated docs and tests.

Copy link
Owner

@jackbearheart jackbearheart left a 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.



function addrListGiveResult(ast) {
return _giveResult(ast, ['group', 'mailbox']);
Copy link
Owner

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.

Copy link
Contributor Author

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.

ret.addresses = [];
for (i = 0; i < addresses.length; i += 1) {
addr = addresses[i];
if (addr.seen) continue;
addr.seen = true;
if (addr.name === 'group') {
Copy link
Owner

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:

  1. make this two functions (no recursion)
  2. process all the 'group' nodes first (loop over findAllNodes('group', ast), then process non group nodes underneath that group, marking seen)
  3. process all the non-group nodes second (loop over findAllNodes('mailbox', ast), skipping if "seen")

Copy link
Contributor Author

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.

function parseOneAddressSimple(opts) {
var result;

function _opts (opts, startAt) {
Copy link
Owner

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.

Copy link
Contributor Author

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.

@@ -528,7 +528,21 @@ function parse5322(opts) {
obsGroupList
)());
}


function fromSpec() {
Copy link
Owner

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

)());
}

function senderSpec() {
Copy link
Owner

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

address
)());
}

Copy link
Owner

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?

Copy link
Contributor Author

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.


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");
Copy link
Owner

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 :)

Copy link
Contributor Author

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.

@baudehlo
Copy link
Contributor Author

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
@jackbearheart
Copy link
Owner

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.

@jackbearheart jackbearheart merged commit 2d6ca1e into jackbearheart:master Jun 20, 2017
@baudehlo
Copy link
Contributor Author

Looks great, thanks a lot!

When will you publish?

@jackbearheart
Copy link
Owner

@baudehlo Just published 3.0.0 on npm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants