|
| 1 | +# typescript-mailbox-parser |
| 2 | + |
| 3 | +Parse an email address (i.e. [mailbox](https://www.rfc-editor.org/rfc/rfc5322#section-3.4)) into it main parts: display name, local portion, domain. |
| 4 | + |
| 5 | +Also: |
| 6 | + |
| 7 | +* it has 0 dependencies |
| 8 | +* is 100% based off [the spec](https://www.rfc-editor.org/rfc/rfc5322#section-3.4) |
| 9 | +* is compatible in browser or node (maybe Deno too?) |
| 10 | +* Preserves case sensitivity |
| 11 | + |
| 12 | +⚠️ THIS LIBRARY IS NOT ABANDONED ⚠️ |
| 13 | + |
| 14 | +While there are few commits and not much activity, sometimes things are just done and work. |
| 15 | + |
| 16 | +Examples: |
| 17 | + |
| 18 | +```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>') |
| 51 | +``` |
| 52 | + |
| 53 | +## Installation and Usage |
| 54 | + |
| 55 | +To install `npm install typescript-mailbox-parser` |
| 56 | + |
| 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). |
| 58 | + |
| 59 | +```ts |
| 60 | +import { mailbox } from 'typescript-mailbox-parser' |
| 61 | + |
| 62 | +const email = mailbox('hello@example.com') |
| 63 | +``` |
| 64 | + |
| 65 | +If the string supplied is not a valid email address (mailbox), then it will return an array of error strings. |
| 66 | + |
| 67 | +```ts |
| 68 | +import { mailbox } from 'typescript-mailbox-parser' |
| 69 | + |
| 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 | + */ |
| 75 | +const email = mailbox('foo bar baz') |
| 76 | +``` |
| 77 | + |
| 78 | +For development |
| 79 | + |
| 80 | +```sh |
| 81 | +# run tests |
| 82 | +npm run test |
| 83 | + |
| 84 | +# build the parser from the grammar |
| 85 | +npm run build:parser |
| 86 | + |
| 87 | +# compile the parser and output to dist |
| 88 | +npm run build:compile |
| 89 | + |
| 90 | +# combine build:parser and build:compile |
| 91 | +npm run build:all |
| 92 | + |
| 93 | +# run formatter |
| 94 | +npm run fmt |
| 95 | +``` |
| 96 | + |
| 97 | +## Notes |
| 98 | + |
| 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. |
0 commit comments