The Email class represents an email address, providing methods to validate, parse, and manipulate the email address components like username, domain, and TLD. It also supports email validation through an external API.

This class is immutable in its interface but allows controlled modifications to the username, hostname, and TLD through getters and setters.

Constructors

  • Creates and returns an Email object referencing the Email specified using an email string, or a base Email object

    Parameters

    Returns Email

Properties

verifier: Verifier = ...

Accessors

  • get alias(): undefined | string
  • Retrieves the alias part of the email, which is the portion after the + symbol in the username, if it exists.

    If the username contains a +, this method returns the part of the username after the +. If there is no + in the username, it returns undefined.

    Returns undefined | string

    The alias part of the email, or undefined if no alias is present.

    const email = Email.from("user+alias@example.com");
    console.log(email.alias); // "alias"

    const email2 = Email.from("user@example.com");
    console.log(email2.alias); // undefined
  • set alias(alias: undefined | string): void
  • Sets or updates the alias part of the email (the portion after the + symbol in the username). If undefined or an empty string is provided, the alias is removed.

    Parameters

    • alias: undefined | string

      The new alias to set. If undefined or empty, the alias will be removed.

    Returns void

    const email = Email.from("user@example.com");
    email.alias = "alias";
    console.log(email.toString()); // "user+alias@example.com"

    const email2 = Email.from("user+alias@example.com");
    email2.alias = undefined;
    console.log(email2.toString()); // "user@example.com"
  • get hash(): string
  • Returns the hash of the email address using the SHA-256 algorithm.

    Returns string

    A string containing the SHA-256 hash of the email address.

  • get hostname(): string
  • Gets the domain (part after the @ symbol) of the email.

    Returns string

    A string containing the domain.

    If the domain is missing.

  • set hostname(value: string): void
  • Sets the domain (hostname) of the email.

    Parameters

    • value: string

      The new domain to set.

    Returns void

  • get username(): string
  • Gets the username (part before the @ symbol) of the email.

    Returns string

    A string containing the username.

    If the username is missing.

  • set username(value: string): void
  • Sets the username of the email.

    Parameters

    • value: string

      The new username to set.

    Returns void

Methods

  • Checks if the email's username contains a + followed by additional text, commonly referred to as an alias or "plus addressing".

    Returns boolean

    true if the email contains an alias, otherwise false.

    const email = Email.from("user+alias@example.com");
    email.hasAlias(); // true

    const email2 = Email.from("user@example.com");
    email2.hasAlias(); // false
  • Serializes the email address to JSON format, returning the same value as toString().

    Returns string

    A string representation of the email for JSON serialization.

  • Returns the full email address as a string.

    Returns string

    A string representation of the email.

  • Determines if the provided value can be parsed as a valid Email.

    This method checks whether the input is either an instance of the Email class or a valid email string that can be successfully parsed by the Email.from() method.

    Type Parameters

    • M

    Parameters

    • this: new (email: string | Email) => M
    • email: string | Email

      The email value to check, which can either be a string or an instance of Email.

    Returns boolean

    true if the value can be parsed as an Email, otherwise false.

    const result1 = Email.canParse("user@example.com"); // true
    const result2 = Email.canParse("invalid-email"); // false
    const emailInstance = Email.from("user@example.com");
    const result3 = Email.canParse(emailInstance); // true
  • Static factory method to create an Email object.

    Type Parameters

    • T

    Parameters

    • this: new (email: string | Email) => T
    • email: string | Email

      A string representing an email address or another Email instance.

    Returns T

    A new Email object.