-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
78 lines (70 loc) · 2.08 KB
/
util.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
exports.abbreviateDegree = (degree) => {
const words = degree.split(" ");
return words
.filter((w) => w[0] === w[0].toUpperCase())
.map((w) => {
if (w === "/") return w;
return `${w[0]}.`;
})
.join("");
};
exports.splitIntoLines = (original) => {
const nameParts = original.split(" ");
if (nameParts.length < 2) {
return [original];
}
const partLength = nameParts.map((p) => p.length);
const totalLength = original.length;
let len1 = totalLength,
len2 = 0,
split = nameParts.length - 1;
for (let i = nameParts.length - 1; i >= 0; i--) {
if (
Math.abs(len1 - partLength[i] - (len2 + partLength[i])) >
Math.abs(len1 - len2)
) {
break;
} else {
len1 -= partLength[i];
len2 += partLength[i];
split--;
}
}
const toReturn = ["", ""];
for (let i = 0; i < nameParts.length; i++) {
if (i <= split) {
toReturn[0] += nameParts[i] + (i == split ? "" : " ");
} else {
toReturn[1] += nameParts[i] + (i + 1 == nameParts.length ? "" : " ");
}
}
return toReturn;
};
exports.pruneContacts = async (contacts, sgClient) => {
console.log("=> Pruning contacts");
console.log("original contacts", contacts.length);
const uniqueEmails = new Set(contacts.map((c) => c["Email Address"]));
let uniqueContacts = Array.from(uniqueEmails).map((email) =>
contacts.find((c) => c["Email Address"] === email)
);
console.log("unique contacts", uniqueContacts.length);
let [, bounces] = await sgClient.request({
method: "GET",
url: "/v3/suppression/bounces",
});
bounces = bounces.map((c) => c.email);
uniqueContacts = uniqueContacts.filter(
(c) => !bounces.includes(c["Email Address"])
);
console.log("remove bounces", uniqueContacts.length);
let [, unsubs] = await sgClient.request({
method: "GET",
url: "/v3/suppression/unsubscribes",
});
unsubs = unsubs.map((c) => c.email);
uniqueContacts = uniqueContacts.filter(
(c) => !unsubs.includes(c["Email Address"])
);
console.log("remove unsubs", uniqueContacts.length);
return uniqueContacts;
};