@@ -11,75 +11,159 @@ Also:
1111
1212⚠️ THIS LIBRARY IS NOT ABANDONED ⚠️
1313
14- While there are few commits and not much activity, sometimes things are just done and work.
14+ At some point in the future it may seem like this project is abandoned, but it's not. While there _ may _ be few commits or not much activity, the code from this project is almost entirely derived from the email [ rfc ] ( https://www.rfc-editor.org/rfc/rfc5322#section-3.4 ) spec, and it therefore benefits from all the very mature work that has gone into those efforts .
1515
1616Examples:
1717
1818``` ts
19- import { mailbox } from ' ./src'
20-
21- /**
22- * { local: 'bob', domain: 'example.com' }
23- */
24- mailbox (' bob@example.com' )
25-
26- /**
27- * { name: undefined, local: 'bob', domain: 'example.com', addr: 'bob@example .com' }
28- */
29- mailbox (' <bob@example.com>' )
30-
31- /**
32- {
33- name: 'Bruce Springsteen',
34- local: 'bruce',
35- domain: 'springsteen.com',
36- addr: 'bruce@springsteen.com'
37- }
38- */
39- mailbox (' Bruce Springsteen <bruce@springsteen.com>' )
40-
41-
42- /**
43- {
44- name: 'Bruce "The Boss" Springsteen',
45- local: 'bruce',
46- domain: 'springsteen.com',
47- addr: 'bruce@springsteen.com'
48- }
49- */
50- mailbox (' Bruce "The Boss" Springsteen <bruce@springsteen.com>' )
19+ import mailbox from ' typescript-mailbox-parser'
20+
21+ // {
22+ // "ok": true,
23+ // "local": "bob",
24+ // "domain": "example.com",
25+ // "addr": "bob@example.com"
26+ // }
27+ console .log (mailbox (' <bob@example.com>' ))
28+
29+ // {
30+ // "ok": true,
31+ // "name": "Bob Hope",
32+ // "local": "bob",
33+ // "domain": "example.com",
34+ // "addr": "bob@example.com"
35+ // }
36+ console .log (mailbox (' Bob Hope <bob@example.com>' ))
37+
38+ // {
39+ // "ok": true,
40+ // "name": "Bob Hope",
41+ // "local": "bob",
42+ // "domain": "example.com",
43+ // "addr": "bob@example.com"
44+ // }
45+ console .log (mailbox (' "Bob Hope" <bob@example.com>' ))
46+
47+ // {
48+ // "ok": true,
49+ // "name": "Bruce \"The Boss\" Springsteen",
50+ // "local": "bruce",
51+ // "domain": "example.com",
52+ // "addr": "bruce@example.com"
53+ // }
54+ console .log (mailbox (' Bruce "The Boss" Springsteen <bruce@example.com>' ))
55+
56+ // {
57+ // "ok": true,
58+ // "local": "bob",
59+ // "domain": "example",
60+ // "addr": "bob@example"
61+ // }
62+ console .log (mailbox (' bob@example' ))
63+
64+ // {
65+ // "ok": true,
66+ // "local": "BOB",
67+ // "domain": "example",
68+ // "addr": "BOB@example"
69+ // }
70+ console .log (mailbox (' BOB@example' ))
71+
72+ // {
73+ // "ok": true,
74+ // "local": "bob",
75+ // "domain": "EXAMPLE",
76+ // "addr": "bob@EXAMPLE"
77+ // }
78+ console .log (mailbox (' bob@EXAMPLE' ))
79+
80+ // {
81+ // "ok": true,
82+ // "local": "a.b.c",
83+ // "domain": "d.e.f.g",
84+ // "addr": "a.b.c@d.e.f.g"
85+ // }
86+ console .log (mailbox (' a.b.c@d.e.f.g' ))
87+
88+ // {
89+ // "ok": true,
90+ // "local": "\"site.local.test:1111\"",
91+ // "domain": "example.com",
92+ // "addr": "\"site.local.test:1111\"@example.com"
93+ // }
94+ console .log (mailbox (' "site.local.test:1111"@example.com' ))
95+
96+ // {
97+ // "ok": true,
98+ // "local": "\"hello, world\"",
99+ // "domain": "example.com",
100+ // "addr": "\"hello, world\"@example.com"
101+ // }
102+ console .log (mailbox (' "hello, world"@example.com' ))
103+
104+ // {
105+ // "ok": false,
106+ // "errors": [
107+ // "{\"pos\":{\"overallPos\":11,\"line\":1,\"offset\":11},\"expmatches\":[{\"kind\":\"RegexMatch\",\"literal\":\"[A-Za-z0-9!#$%&\\\\x27\\\\*\\\\+\\\\-\\\\/=?^_\\\\`{|}~]\",\"negated\":false},{\"kind\":\"RegexMatch\",\"literal\":\"\\\\x20\",\"negated\":false},{\"kind\":\"RegexMatch\",\"literal\":\"\\\\x09\",\"negated\":false},{\"kind\":\"RegexMatch\",\"literal\":\"\\\\r\\\\n\",\"negated\":false},{\"kind\":\"RegexMatch\",\"literal\":\"\\\\(\",\"negated\":false},{\"kind\":\"RegexMatch\",\"literal\":\"\\\\x22\",\"negated\":false},{\"kind\":\"RegexMatch\",\"literal\":\"<\",\"negated\":false}]}"
108+ // ]
109+ // }
110+ console .log (mailbox (' foo bar baz' ))
51111```
52112
53113## Installation and Usage
54114
55115To install ` npm install typescript-mailbox-parser `
56116
57- Then to use it you just need to import it and call the ` mailbox ` function on a string. If successful it will return an object representing the parts of the email address (mailbox).
117+ Then to use it you just need to import it and call the ` mailbox ` function on a string.
58118
59119``` ts
60- import { mailbox } from ' typescript-mailbox-parser'
120+ import mailbox from ' typescript-mailbox-parser'
61121
62122const email = mailbox (' hello@example.com' )
63123```
64124
65- If the string supplied is not a valid email address (mailbox), then it will return an array of error strings.
125+ It returns the following type
66126
67127``` ts
68- import { mailbox } from ' typescript-mailbox-parser'
128+ type MailboxParseResult =
129+ | { ok: false ; errors: string [] }
130+ | { ok: true ; addr: string ; name? : string ; local: string ; domain: string }
131+ ` ` `
132+
133+ In other words, if successful it will return an object representing the parts of the email address (mailbox):
134+
135+ ` ` ` ts
136+ import mailbox from ' typescript-mailbox-parser'
137+
138+ // {
139+ // ok: true,
140+ // name: 'Bruce "The Boss" Springsteen',
141+ // local: 'bruce',
142+ // domain: 'example.com',
143+ // addr: 'bruce@example.com'
144+ // }
145+ console .log (mailbox (' Bruce "The Boss" Springsteen <bruce@example.com>' ))
146+ ```
147+
148+ If unsuccessful it will return an object with an ` errors ` field containing an array of error messages as strings:
69149
70- /**
71- [
72- '{"pos":{"overallPos":11,"line":1,"offset":11},"expmatches":[{"kind":"RegexMatch","literal":"[A-Za-z0-9!#$%&\\\\x27\\\\*\\\\+\\\\-\\\\/=?^_\\\\`{|}~]","negated":false},{"kind":"RegexMatch","literal":"\\\\x20","negated":false},{"kind":"RegexMatch","literal":"\\\\x09","negated":false},{"kind":"RegexMatch","literal":"\\\\r\\\\n","negated":false},{"kind":"RegexMatch","literal":"\\\\(","negated":false},{"kind":"RegexMatch","literal":"\\\\x22","negated":false},{"kind":"RegexMatch","literal":"<","negated":false}]}'
73- ]
74- */
150+ ``` ts
151+ import mailbox from ' typescript-mailbox-parser'
152+
153+ // {
154+ // ok: false,
155+ // errors: [
156+ // '{"pos":{"overallPos":11,"line":1,"offset":11},"expmatches":[{"kind":"RegexMatch","literal":"[A-Za-z0-9!#$%&\\\\x27\\\\*\\\\+\\\\-\\\\/=?^_\\\\`{|}~]","negated":false},{"kind":"RegexMatch","literal":"\\\\x20","negated":false},{"kind":"RegexMatch","literal":"\\\\x09","negated":false},{"kind":"RegexMatch","literal":"\\\\r\\\\n","negated":false},{"kind":"RegexMatch","literal":"\\\\(","negated":false},{"kind":"RegexMatch","literal":"\\\\x22","negated":false},{"kind":"RegexMatch","literal":"<","negated":false}]}'
157+ // ]
158+ // }
75159const email = mailbox (' foo bar baz' )
76160```
77161
78162For development
79163
80164``` sh
81165# run tests
82- npm run test
166+ npm test
83167
84168# build the parser from the grammar
85169npm run build:parser
@@ -90,10 +174,13 @@ npm run build:compile
90174# combine build:parser and build:compile
91175npm run build:all
92176
177+ # generate the examples used in the README
178+ npm run docs:examples
179+
93180# run formatter
94181npm run fmt
95182```
96183
97184## Notes
98185
99- This _ should_ in theory work with 100% accuracy, as the logic is based off the [ rfc5322] ( https://www.rfc-editor.org/rfc/rfc5322 ) specification... however mistakes can always be made. Please file an issue if there are any bugs.
186+ This _ should_ in theory work with 100% accuracy, as the logic is based off the [ rfc5322] ( https://www.rfc-editor.org/rfc/rfc5322 ) specification. Please file an issue if there are any bugs.
0 commit comments