-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (35 loc) · 1009 Bytes
/
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
const fs = require('fs');
const moment = require('moment');
module.exports = async (file) => {
let myLines;
try {
myLines = fs.readFileSync(file).toString();
} catch (e) {
throw new Error(`No such file ${file}`);
}
myLines = myLines.split(/([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{1,2}, [0-9]{1,2}:[0-9]{1,2}\s?[A-z]{0,2} - )/gm||[]).filter(e => e);
let messages = [];
for(let i = 0; i < myLines.length; i++) {
let time = myLines[i].replace(/ - /, '');
time = moment(time, "MM/DD/YY, hh:mm a").toDate();
let chat = myLines[i+1] && myLines[i+1].split(/: (.+)?/);
let sender, msg;
if (chat) {
if(chat[1]) {
sender = chat[0];
msg = chat[1].replace(/\n/g, " ");
} else {
sender = 'Announcement';
msg = chat[0].replace(/\n/g, " ");
}
i = i+1;
messages.push({
time, sender, msg
});
};
};
if (messages.length === 0) {
throw new Error('Wrong file type or chat format');
};
return messages;
}