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

Allow comma in display name #52

Merged
merged 7 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

## 2.1.0 - 2021-02-26

- make parse accept an options object as second argument
- allow , character in display name, off by default #52

## 2.0.6 - 2020-11-17

- replace travis/appveyor CI tests with Github Actions
Expand Down
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

const ea_lib = require('email-addresses');

exports.parse = function parse (line, startAt) {
exports.parse = function parse (line, opts = null) {
if (!line) throw new Error('Nothing to parse');

line = line.trim();

const defaultOpts = {
startAt: null,
allowAtInDisplayName: true,
allowCommaInDisplayName: false,
}

const { startAt, allowAtInDisplayName, allowCommaInDisplayName } = typeof opts === 'object'
? Object.assign({}, defaultOpts, opts)
: Object.assign({}, defaultOpts, { startAt: opts })

const addr = ea_lib({
input: line,
rfc6532: true, // unicode
Expand All @@ -15,7 +25,8 @@ exports.parse = function parse (line, startAt) {
strict: false, // turn off obs- features in the rfc
rejectTLD: false, // domains require a "."
startAt: startAt || null,
atInDisplayName: true // allow at in display name
atInDisplayName: allowAtInDisplayName,
commaInDisplayName: allowCommaInDisplayName,
});

if (!addr) throw new Error('No results');
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "address-rfc2822",
"version": "2.0.6",
"version": "2.1.0",
"description": "RFC 2822 & 5322 (Header) email address parser",
"main": "index.js",
"homepage": "https://github.com/haraka/node-address-rfc2822",
Expand Down Expand Up @@ -31,6 +31,6 @@
},
"license": "MIT",
"dependencies": {
"email-addresses": "^3.1.0"
"email-addresses": "^4.0.0"
}
}
24 changes: 24 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,27 @@ describe('parseReplyTo', function () {
done();
})
})

describe('parse with options', function () {
it('should not allow parsing display name with comma by default', function (done) {
try {
address.parse('Foo, Bar <foo@example.com>');
}
catch (e) {
assert.equal(e.message, 'No results');
}
done();
})

it('should allow parsing display name with comma', function (done) {
try {
const [r] = address.parse('Foo, Bar <foo@example.com>', { allowCommaInDisplayName: true });
assert.equal('foo@example.com', r.address);
assert.equal('Foo, Bar', r.phrase);
}
catch (e) {
console.error(e);
}
done();
})
})