mailparser
mailparser copied to clipboard
Request: Make `ParsedMail` properties always arrays
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;
...
}