1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
export function parseSendGridEmail(input) {
function parseString(input) {
if (typeof input === "string") {
// parse email out of format like 'name <email@email.com>'
const regex = /<([^>]+)>/;
let email = input.match(regex)?.at(1);
return email ?? input;
}
else
throw new Error(
"Not parseable as a sendgrid email: " + JSON.stringify(input),
);
}
if (typeof input === "string") {
return { email: parseString(input) };
}
else if (typeof input === "object" && "email" in input) {
return {
email: parseString(input.email),
name: input.name,
};
}
else
throw new Error(
"Not parseable as sendgrid emails: " + JSON.stringify(input),
);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023