Files
upstream-atomic-crm/supabase/functions/postmark/extractMailContactData.ts
upstream-mirror 93ecc1a6f8
Some checks failed
✅ Check / 🔬 ESLint (push) Failing after 21s
✅ Check / 🏷️ Typecheck (push) Failing after 11s
✅ Check / 🔎 Test (push) Failing after 11s
✅ Check / e2e-test (push) Failing after 11s
✅ Check / 🔨 Build (push) Failing after 11s
🚀 Deploy / 🚀 Deploy (doc) (push) Failing after 12s
🚀 Deploy / 🚀 Deploy (demo) (push) Failing after 12s
🚀 Deploy / 🚀 Deploy (supabase) (push) Failing after 18s
snapshot of https://github.com/marmelab/atomic-crm.git (main, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:48:50 +05:00

76 lines
1.8 KiB
TypeScript

import { parse } from "tldts";
const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
export function extractCompanyName(domain: string) {
if (!domain) return "";
let name = domain.split(".").at(-2) || domain;
const parseResult = parse(domain);
if (parseResult.domainWithoutSuffix) {
name = parseResult.domainWithoutSuffix;
}
const humanizedName = name
.replace(/[-_]+/g, " ")
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
return humanizedName;
}
/**
* Extracts the first name, last name, email, and domain from a mail contact.
*
* Example:
* "ToFull": [
* {
* "Email": "firstname.lastname@marmelab.com",
* "Name": "Firstname Lastname"
* }
* ]
*
* Return Value:
* {
* firstName: "Firstname",
* lastName: "Lastname",
* email: "firstname.lastname@marmelab.com",
* domain: "marmelab.com"
* }
*
*/
export const extractMailContactData = (
ToFull: {
Email: string;
Name: string;
}[],
) => {
return ToFull.map((contact) => ({
...contact,
Email: (contact.Email || "").toLowerCase(),
})).map((contact) => {
const domain = (contact.Email.split("@").at(-1) || "").toLowerCase();
const fullName =
contact.Name ||
contact.Email.split("@").slice(0, -1).join(" ").split(".").join(" ");
let firstName = "";
let lastName = fullName;
if (fullName && fullName.includes(" ")) {
const parts = fullName.split(" ");
firstName = parts[0];
lastName = parts.slice(1).join(" ");
}
const companyName = extractCompanyName(domain);
const website = `https://${domain}`;
return {
firstName: capitalize(firstName),
lastName: capitalize(lastName),
email: contact.Email,
domain,
companyName,
website,
};
});
};