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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sparser/email-address-parser

v2.0.0

Published

An RFC 5322, and RFC 6532 compliant email address parser.

Downloads

182

Readme

email-address-parser

This is a WASM wrapper over the rust crate email-address-parser which provides an RFC 5322, and RFC 6532 compliant implementation of email address parser. The code for this npm package is generated with with wasm-bindgen.

Install

npm i @sparser/email-address-parser

Usage

Node.js

import("@sp/email-address-parser")
  .then((parser) => {
    const { EmailAddress, ParsingOptions } = parser.default;

    // pares valid address
    const email = EmailAddress.parse("[email protected]");
    // get local part and domain
    console.log(`local part: ${email.localPart}, domain: ${email.domain}`); // local part: foo, domain: bar.com

    // invalid address
    console.log(EmailAddress.parse("[email protected]", new ParsingOptions(true))); // undefined
  })
  .catch((reason) => {
    console.error(reason);
  });

In webapp with bundler (like webpack)

// @ts-check
import("@sp/email-address-parser")
  .then(({ EmailAddress, ParsingOptions }) => {
    // pares valid address
    const email = EmailAddress.parse("[email protected]");
    // get local part and domain
    console.log(`local part: ${email.localPart}, domain: ${email.domain}`);  // local part: foo, domain: bar.com

    // invalid address
    console.log(EmailAddress.parse("[email protected]", new ParsingOptions(true))); // undefined
});

API

The EmailAddress class encapsulates the validation and parsing part. Optionally an instance of ParsingOptions can be used to affect the strictness of the parsing.

ParsingOptions

An instance of this class can be used affect the strictness of the parsing.

Strict parsing

// strict parsing options
const options = new ParsingOptions(false); // ParsingOptions { is_lax: false }

With strict parsing, the obsolete production rules as outlined in RFC 5322, are disallowed. Strict parsing is the default setting; i.e. while parsing or validating and email address, no parsing options needs to be explicitly supplied.

Lax parsing

// lax parsing options
const options = new ParsingOptions(true); // ParsingOptions { is_lax: true }

If the input contains obsolete local part or domain, then an instance of the options object needs to be used explicitly.

EmailAddress

This is responsible for validating and parsing email addresses.

parse

Parses a given string as an email address, and returns an instance EmailAddress if the input is valid, else undefined.

const email = EmailAddress.parse(`[email protected]`);
assert(email.getLocalPart() === "foo");
assert(email.getDomain() === "bar.com");

// for invalid addresses `undefined` is returned.
assert(EmailAddress.parse(`[email protected]`, new ParsingOptions(true)) === undefined);

isValid

Validates if the given input string is an email address or not. Unlike the parse method, it does not instantiate an EmailAddress.

assert(EmailAddress.isValid(`[email protected]`));
assert(!EmailAddress.isValid(`[email protected]`, new ParsingOptions(true)));

Instance methods

An instance of EmailAddress can also be created using the constructor.

const email = new EmailAddress("foo", "bar.com");
assert(email.localPart === "foo");
assert(email.domain === "bar.com");

If either the local part or domain is invalid and cannot be parsed, the constructor throws error. For example, the following attempts fails.

new EmailAddress('-foo', 'bar.com');
new EmailAddress('foo', '-bar.com');

Unicode support

In compliance to RFC 6532, it supports parsing, validating, and instantiating email addresses with Unicode characters.

assert(`${new EmailAddress('foö', 'bücher.de')}` === 'foö@bücher.de');
assert(`${EmailAddress.parse('foö@bücher.de')}` === 'foö@bücher.de');
assert(EmailAddress.isValid('foö@bücher.de'));