UNPKG

4.11 kBMarkdownView Raw
1email-addresses.js
2==================
3
4An RFC 5322 email address parser.
5
6v 2.0.0
7
8What?
9-----
10Want to see if something could be an email address? Want to grab the display name or just the address out of a string? Put your regexes down and use this parser!
11
12This library does not validate email addresses - we can't really do that without sending an email. However, it attempts to parse addresses using the (fairly liberal) grammar specified in RFC 5322. You can use this to check if user input looks like an email address.
13
14Why use this?
15-------------
16Use this library because you can be sure it really respects the RFC:
17 - The functions in the recursive decent parser match up with the productions in the RFC
18 - The productions from the RFC are written above each function for easy verification
19 - Tests include all of the test cases from the [is_email](https://github.com/dominicsayers/isemail) project, which are extensive
20
21Installation
22------------
23npm install email-addresses
24
25Example
26-------
27
28```
29$ node
30> addrs = require("email-addresses")
31{ [Function: parse5322]
32 parseOneAddress: [Function: parseOneAddressSimple],
33 parseAddressList: [Function: parseAddressListSimple] }
34> addrs.parseOneAddress('"Jack Bowman" <jack@fogcreek.com>')
35{ parts:
36 { name: [Object],
37 address: [Object],
38 local: [Object],
39 domain: [Object] },
40 name: 'Jack Bowman',
41 address: 'jack@fogcreek.com',
42 local: 'jack',
43 domain: 'fogcreek.com' }
44> addrs.parseAddressList('jack@fogcreek.com, Bob <bob@example.com>')
45[ { parts:
46 { name: null,
47 address: [Object],
48 local: [Object],
49 domain: [Object] },
50 name: null,
51 address: 'jack@fogcreek.com',
52 local: 'jack',
53 domain: 'fogcreek.com' },
54 { parts:
55 { name: [Object],
56 address: [Object],
57 local: [Object],
58 domain: [Object] },
59 name: 'Bob',
60 address: 'bob@example.com',
61 local: 'bob',
62 domain: 'example.com' } ]
63> addrs("jack@fogcreek.com")
64{ ast:
65 { name: 'address-list',
66 tokens: 'jack@fogcreek.com',
67 semantic: 'jack@fogcreek.com',
68 children: [ [Object] ] },
69 addresses:
70 [ { node: [Object],
71 parts: [Object],
72 name: null,
73 address: 'jack@fogcreek.com',
74 local: 'jack',
75 domain: 'fogcreek.com' } ] }
76> addrs("bogus")
77null
78```
79
80Usage
81-----
82If you want to simply check whether an address or address list parses, you'll want to call the following functions and check whether the results are null or not: ```parseOneAddress``` for a single address and ```parseAddressList``` for multiple addresses.
83
84If you want to examine the parsed address, for example to extract a name or address, you have some options. The object returned by ```parseOneAddress``` has four helper values on it: ```name```, ```address```, ```local```, and ```domain```. See the example above to understand is actually returned. (These are equivalent to ```parts.name.semantic```, ```parts.address.semantic```, etc.) These values try to be smart about collapsing whitespace, quotations, and excluding RFC 5322 comments. If you desire, you can also obtain the raw parsed tokens or semantic tokens for those fields. The ```parts``` value is an object referencing nodes in the AST generated. Nodes in the AST have two values of interest here, ```tokens``` and ```semantic```.
85
86```
87> a = addrs.parseOneAddress('Jack Bowman <jack@fogcreek.com >')
88> a.parts.name.tokens
89'Jack Bowman '
90> a.name
91'Jack Bowman'
92> a.parts.name.semantic
93'Jack Bowman '
94> a.parts.address.tokens
95'jack@fogcreek.com '
96> a.address
97'jack@fogcreek.com'
98> a.parts.address.semantic
99'jack@fogcreek.com'
100```
101
102If you need to, you can inspect the AST directly. The entire AST is returned when calling the module's function.
103
104References
105----------
106 - http://tools.ietf.org/html/rfc5322
107 - http://code.google.com/p/isemail/
108
109Props
110-----
111Many thanks to [Dominic Sayers](https://github.com/dominicsayers) and his documentation and tests
112for the [is_email](https://github.com/dominicsayers/isemail) function which helped greatly in writing this parser.
113
114License
115-------
116Licensed under the MIT License. See the LICENSE file.
117
\No newline at end of file