-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
111 lines (89 loc) · 2.16 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
'use strict';
const fs = require('fs');
const moment = require('moment');
const debug = require('debug')('whatsapp-parser');
module.exports.parseFile = parseFile;
module.exports._parseLine = parseLine;
function parseFile(filepath) {
return readFile(filepath)
.then(splitLines)
.then(parseLines)
.then(filterMessagesWithoutContent)
.catch((err) => {
if (err.errno === -2) {
return [];
}
debug(err.message);
});
}
function readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) {
reject(err);
}
resolve(data ? data.toString('utf8') : '');
});
});
}
function splitLines(input) {
return input.split('\n');
}
function parseLines(lines) {
return lines.map(parseLine);
}
function filterMessagesWithoutContent(messages) {
return messages.filter((message) => {
return !!message;
});
}
function parseLine(line) {
var rawParts = getParts(line);
var result = {
date: rawParts.version === 1 ?
parseDateForFormatV1(rawParts.date) :
parseDateForFormatV2(rawParts.date),
author: parseAuthor(rawParts.author),
content: parseContent(rawParts.content)
};
if (!result.content) {
result = null;
}
return result;
}
function getParts(line) {
return line.startsWith('[') ?
getPartsFromLineInExportFormatV2(line) :
getPartsFromLineInExportFormatV1(line);
}
function getPartsFromLineInExportFormatV1(line) {
var splitted = line.split(': ');
return {
date: splitted[0],
author: splitted[1],
content: splitted[2],
version: 1
};
}
function getPartsFromLineInExportFormatV2(line) {
var splitted = line.split('] ');
var splittedSecondPart = splitted[1].split(': ');
return {
date: splitted[0].substr(1, 20),
author: splittedSecondPart[0],
content: splittedSecondPart[1],
version: 2
};
}
function parseDateForFormatV1(input) {
return moment(input, 'DD-MM-YY HH:mm:ss').toDate();
}
function parseDateForFormatV2(input) {
return moment(input, 'DD/MM/YYYY HH:mm:ss').toDate();
}
function parseAuthor(input) {
return input;
}
function parseContent(input) {
return input;
}