-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
128 lines (114 loc) · 3.41 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import fs from 'node:fs';
import { parse, stringify } from 'csv/sync';
import jsonToTable from 'json-to-table';
import { table } from 'table';
import {
aggregateAllChannels as aggregateAllChannelsTransform,
cleanse as cleanseTransform,
deduplicate as deduplicateTransform,
filterToCreatedAfter2010 as filterToCreatedAfter2010Transform,
filterToFirstNameStartingWithB as filterToFirstNameStartingWithBTransform,
getInterestedRepeatCustomers as getInterestedRepeatCustomersTransform,
getTotalValueOfAllCustomers as getTotalValueOfAllCustomersTransform,
getUsersWithNonMatchingEmails as getUsersWithNonMatchingEmailsTransform,
sortByFirstName as sortByFirstNameTransform,
sortBySubscriptionDate as sortBySubscriptionDateTransform,
} from './transformations.js';
function parseCsv(inFile) {
const fileContent = fs.readFileSync('dataIn/' + inFile, 'utf8');
return parse(fileContent, { columns: true });
}
const inFiles = {
hubspot: parseCsv('hubspot-export.csv'),
mailchimp: parseCsv('mailchimp-export.csv'),
stripe: parseCsv('stripe-export.csv'),
};
export function sortBySubscriptionDate() {
return {
outFile: 'mailchimp-sorted-subscription-date.csv',
data: sortBySubscriptionDateTransform(inFiles.mailchimp),
};
}
export function sortByFirstName() {
return {
outFile: 'mailchimp-sorted-first-name.csv',
data: sortByFirstNameTransform(inFiles.mailchimp),
};
}
export function filterToFirstNameStartingWithB() {
return {
outFile: 'hubspot-first-name-b.csv',
data: filterToFirstNameStartingWithBTransform(inFiles.hubspot),
};
}
export function filterToCreatedAfter2010() {
return {
outFile: 'stripe-created-after-2010.csv',
data: filterToCreatedAfter2010Transform(inFiles.stripe),
};
}
export function aggregateAllChannels() {
return {
outFile: 'all-channels-aggregate.csv',
data: aggregateAllChannelsTransform(
inFiles.hubspot,
inFiles.mailchimp,
inFiles.stripe,
),
};
}
export function deduplicate() {
return {
outFile: 'stripe-deduplicated.csv',
data: deduplicateTransform(inFiles.stripe),
};
}
export function cleanse() {
return {
outFile: 'hubspot-cleansed.csv',
data: cleanseTransform(inFiles.hubspot),
};
}
export function getInterestedRepeatCustomers() {
return {
outFile: 'all-channels-interested-repeat-customers.csv',
data: getInterestedRepeatCustomersTransform(
inFiles.hubspot,
inFiles.stripe,
),
};
}
export function getTotalValueOfAllCustomers() {
return {
outFile: 'hubspot-total-value-all-customers.csv',
data: getTotalValueOfAllCustomersTransform(inFiles.hubspot),
};
}
export function getUsersWithNonMatchingEmails() {
return {
outFile: 'all-channels-users-non-matching-emails.csv',
data: getUsersWithNonMatchingEmailsTransform(
inFiles.mailchimp,
inFiles.stripe,
),
};
}
const commandFn = {
sortByFirstName,
sortBySubscriptionDate,
filterToFirstNameStartingWithB,
filterToCreatedAfter2010,
aggregateAllChannels,
deduplicate,
cleanse,
getInterestedRepeatCustomers,
getTotalValueOfAllCustomers,
getUsersWithNonMatchingEmails,
}[process.argv[2]];
if (commandFn) {
const { outFile, data } = commandFn();
const outFilePath = 'dataOut/' + outFile;
fs.writeFileSync(outFilePath, stringify(data, { header: true }));
console.log(`Wrote file ${outFilePath} successfully with following data:`);
console.log(table(jsonToTable(data)));
}