npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

typescript-mailbox-parser

v0.0.3

Published

Reliably get the display name, local, and domain parts of an email address

Readme

typescript-mailbox-parser

Parse an email address (i.e. mailbox) into it main parts: display name, local portion, domain.

Also:

  • it has 0 dependencies
  • is 100% based off the spec
  • is compatible in browser or node (maybe Deno too?)
  • Preserves case sensitivity

⚠️ THIS LIBRARY IS NOT ABANDONED ⚠️

While there may be few commits or little activity, the code from this project is almost entirely derived from the email rfc spec, and it therefore benefits from all the very mature work that has gone into those efforts.

Examples:

import { mailbox } from 'typescript-mailbox-parser'

// {
//   "ok": true,
//   "local": "bob",
//   "domain": "example.com",
//   "addr": "[email protected]"
// }
console.log(mailbox('<[email protected]>'))

// {
//   "ok": true,
//   "name": "Bob Hope",
//   "local": "bob",
//   "domain": "example.com",
//   "addr": "[email protected]"
// }
console.log(mailbox('Bob Hope <[email protected]>'))

// {
//   "ok": true,
//   "name": "Bob Hope",
//   "local": "bob",
//   "domain": "example.com",
//   "addr": "[email protected]"
// }
console.log(mailbox('"Bob Hope" <[email protected]>'))

// {
//   "ok": true,
//   "name": "Bruce \"The Boss\" Springsteen",
//   "local": "bruce",
//   "domain": "example.com",
//   "addr": "[email protected]"
// }
console.log(mailbox('Bruce "The Boss" Springsteen <[email protected]>'))

// {
//   "ok": true,
//   "local": "bob",
//   "domain": "example",
//   "addr": "bob@example"
// }
console.log(mailbox('bob@example'))

// {
//   "ok": true,
//   "local": "BOB",
//   "domain": "example",
//   "addr": "BOB@example"
// }
console.log(mailbox('BOB@example'))

// {
//   "ok": true,
//   "local": "bob",
//   "domain": "EXAMPLE",
//   "addr": "bob@EXAMPLE"
// }
console.log(mailbox('bob@EXAMPLE'))

// {
//   "ok": true,
//   "local": "a.b.c",
//   "domain": "d.e.f.g",
//   "addr": "[email protected]"
// }
console.log(mailbox('[email protected]'))

// {
//   "ok": true,
//   "local": "\"site.local.test:1111\"",
//   "domain": "example.com",
//   "addr": "\"site.local.test:1111\"@example.com"
// }
console.log(mailbox('"site.local.test:1111"@example.com'))

// {
//   "ok": true,
//   "local": "\"hello, world\"",
//   "domain": "example.com",
//   "addr": "\"hello, world\"@example.com"
// }
console.log(mailbox('"hello, world"@example.com'))

// {
//   "ok": false,
//   "errors": [
//     "{\"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}]}"
//   ]
// }
console.log(mailbox('foo bar baz'))

Installation and Usage

To install npm install typescript-mailbox-parser

Then to use it you just need to import it and call the mailbox function on a string.

import { mailbox } from 'typescript-mailbox-parser'

const email = mailbox('[email protected]')

It returns the following type

type MailboxParseResult =
  | { ok: false; errors: string[] }
  | { ok: true; addr: string; name?: string; local: string; domain: string }

In other words, if successful it will return an object representing the parts of the email address (mailbox):

import { mailbox } from 'typescript-mailbox-parser'

// {
//   ok: true,
//   name: 'Bruce "The Boss" Springsteen',
//   local: 'bruce',
//   domain: 'example.com',
//   addr: '[email protected]'
// }
console.log(mailbox('Bruce "The Boss" Springsteen <[email protected]>'))

If unsuccessful it will return an object with an errors field containing an array of error messages as strings:

import { mailbox } from 'typescript-mailbox-parser'

// {
//   ok: false,
//   errors: [
//     '{"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}]}'
//   ]
// }
const email = mailbox('foo bar baz')

For development

# run tests
npm test

# build the parser from the grammar
npm run build:parser

# compile the parser and output to dist
npm run build:compile

# combine build:parser and build:compile
npm run build:all

# generate the examples used in the README
npm run docs:examples

# run formatter
npm run fmt

Notes

This should in theory work with 100% accuracy, as the logic is based off the rfc5322 specification. Please file an issue if there are any bugs.