-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailCount.js
67 lines (54 loc) · 1.65 KB
/
emailCount.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
const fs = require("fs");
const filePath = "./test.txt";
const softwire = /[^\s]+@softwire\.com/g;
const emailReg = /[^\s]+@\w+\.[^\s]+/g;
const emailReg2 = /[^\s]+(@\w+\.)[^\s]+/g;
const domainReg = /@[^\s]+\.[^\s]+/g;
const readFileToString = (filePath) => fs.readFileSync(filePath, "utf-8");
const countDomainManually = (text, domain) => {
let counter = 0;
for (let i = 0; i < text.length; i++) {
if (text.substring(i, domain.length + i) === domain) {
counter++;
}
}
return counter;
};
const pickHighest = (dict) => {
const sortedDict = Object.keys(dict).sort((a, b) => dict[b] - dict[a]);
const topKeyList = [];
sortedDict.forEach((key, value) => {
if (value < num) {
topKeyList.push(key);
}
});
return topKeyList;
};
const pickGreaterThan = (dict, num) => {
return Object.keys(dict).filter(key => dict[key] >= num)
};
const createDictOfStringMatches = (text, regex) => {
const matches = text.match(regex);
const dict = {};
matches.forEach((match) => {
dict[match] = (dict[match] || 0) + 1});
return dict;
};
const createDictOfGroupedStringMatches = (text, regex) => {
const matches = [...text.matchAll(regex)].map(e => e[1]);
const dict = {};
matches.forEach((match) => {
if (dict[match]) {
dict[match]++;
} else {
dict[match] = 1;
}
});
return dict;
};
const testString = readFileToString(filePath);
const dictOfDomains = createDictOfStringMatches(testString, domainReg);
// const dictOfDomains = createDictOfGroupedStringMatches(testString, emailReg2);
console.log(dictOfDomains)
// console.log(pickHighest(dictOfDomains, 10));
// console.log(pickGreaterThan(dictOfDomains, 63));