-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (147 loc) · 5.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"use strict";
let tweetsFileList,
userFilelist,
results,
tweetsFileOutput,
userFileOutput,
finaleOutput,
finalTweetObject = [],
followerMap = [];
window.onload = function () {
tweetsFileList = document.getElementById('tweetsFile');
userFilelist = document.getElementById('userFile');
tweetsFileOutput = document.getElementById('tweetsFileOutput');
userFileOutput = document.getElementById('userFileOutput');
finaleOutput = document.getElementById('finaleOutput');
};
//processing tweets file
function processTweestFiles() {
writeTweetsfiles(tweetsFileList.files[0]);
}
//processing User file
function processUsersFiles() {
writeUsersfiles2(userFilelist.files[0]);
}
//reading tweets file
function writeTweetsfiles(file) {
if (file.type.match('text.*')) {
let reader = new FileReader();
reader.onload = function () {
results = reader.result;
tweetsFileOutput.innerText = results;
manageTweets(results);
//document.getElementById('output').innerText += reader.result;
};
reader.readAsText(file, "UTF-8");
} else {
tweetsFileOutput.innerText = "File type not supported!";
}
}
//reading User file
function writeUsersfiles2(file) {
if (file.type.match('text.*')) {
let reader2 = new FileReader();
reader2.onload = function () {
userFileOutput.innerText += reader2.result;
manageFollowers(reader2.result);
};
reader2.readAsText(file, "UTF-8");
}
}
function finalOutPut(e) {
e.preventDefault();
for (let x = 0; x < followerMap.length; x++) {
for (let i = 0; i < finalTweetObject.length; i++) {
followerMap[x].followers.forEach((follower) => {
modifyFollowerMap(follower); // Implement modifyFollowerMap by passing followers
});
}
}
console.log('finalTweetObject ', finalTweetObject);
console.log('FollowerMap', followerMap);
displayResults();
}
function displayResults () {
let output = "";
followerMap = _.orderBy(followerMap,'user','asc'); // order by user ascending
console.log(followerMap);
followerMap.forEach((obj)=>{
output += obj.user + '\n';
//Print own tweets
for(let i = 0 ; i<finalTweetObject.length; i++){
if(obj.user.trim() === finalTweetObject[i].name.trim()){
for(let j = 0; j<finalTweetObject[i].tweets.length; j++){
output += '@'+obj.user+': '+finalTweetObject[i].tweets[j]+'\n'
}
}
}
//Print Followers tweets
for(let i = 0; i<obj.followers.length; i++){
for(let j = 0; j<finalTweetObject.length; j++){
if(obj.followers[i].trim() === finalTweetObject[j].name.trim()){
for(let k = 0; k<finalTweetObject[j].tweets.length; k++){
output += "@"+finalTweetObject[j].name+': '+finalTweetObject[j].tweets[k]+'\n'
}
}
}
}
});
finaleOutput.innerHTML = output; // Final Print Out
console.log(output);
}
// Modify the followerMap array to include the user without tweets
function modifyFollowerMap(user){
let userPresent = false;
for(let i = 0; i<followerMap.length; i++){ // check if user has tweets
if(followerMap[i].user.trim() === user.trim()){
userPresent = true;
}
}
if(userPresent == false){ // if no tweets available then modify the followerMap
followerMap.push({
followers: [],
user: user
})
}
}
function manageTweets(tweets) {
let tweetArray = tweets.split('\n');
let userArray = [];
tweetArray.forEach((tweet) => {
userArray.push(tweet.split('>')[0]);
});
userArray =_.uniqBy(userArray); // Lodash remove duplicates from array
//Create users with tweets
userArray.forEach((user) => {
finalTweetObject.push({
name: user,
tweets: []
})
});
finalTweetObject.forEach((userTweet) => {
tweetArray.forEach((tweet) => {
if (tweet.split('>')[0] === userTweet.name) {
userTweet.tweets.push(tweet.split('>')[1]);
}
});
});
}
function manageFollowers(users) {
let userEntryArray = users.split('\n')
//Create followerMap
userEntryArray.forEach((entry) => {
followerMap.push({
user: entry.indexOf('follows') > -1 ? entry.split('follows')[0].trim() : [entry],
followers: entry.indexOf('follows') > -1 ? entry.split('follows')[1].trim().split(',') : [entry]
});
});
//get follower and merging all the followers based on the user
for (let i = 0; i < followerMap.length; i++) {
for (let j = i + 1; j < followerMap.length; j++) {
if (followerMap[i].user === followerMap[j].user) {
followerMap[i].followers = _.merge(followerMap[i].followers, followerMap[j].followers);
followerMap.splice(j, 1);
}
}
}
}