Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Make ParsedMail properties always arrays #373

Open
Remscar opened this issue Jul 3, 2024 · 0 comments
Open

Request: Make ParsedMail properties always arrays #373

Remscar opened this issue Jul 3, 2024 · 0 comments

Comments

@Remscar
Copy link

Remscar commented Jul 3, 2024

There are many members of the ParsedMail interface that can either be a type, an array of that type, or undefined.

Writing downstream code which has to specifically handle array vs literal cases is cumbersome. It's more intuitive for consumers to handle only arrays (or undefined) instead of handle both cases.

Some examples:

// Assume undefined checks on `parsedMessage.to`

// Before:
// Only works if there is one recipient
const firstRecipient = parsedMessage.to.text;
// Only works if there is more than one recipient
const firstRecipient = parsedMessage.to[0].text;
// Requires code to handle both cases
let firstRecipient;
if (Array.isArray(parsedMessage.to)) 
  firstRecipient = parsedMessage.to[0].text;
else
  firstRecipient = parsedMessage.to[0].text;

// After (always use arrays)
// Gets the first address 100% of the time
const firstRecipient = parsedMessage.to[0].text;

Programmers are lazy, we don't want to write extra code.

I know this is a breaking change, so perhaps a multiple purpose object could be used for backwards compatibility. ie;

export interface AddressObject {
    /**
     * An array with address details.
     */
    value: EmailAddress[];
    /**
     * A formatted address string for HTML context.
     */
    html: string;
    /**
     * A formatted address string for plaintext context.
     */
    text: string;
}

export interface ParsedMessageAddressObject extends AddressObject {
    [index: number]: AddressObject  | undefined;
}

export interface ParsedMail {
    ...
    to?: ParsedMessageAddressObject  | undefined;
    ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant