-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail-stripper.js
61 lines (52 loc) · 1.68 KB
/
mail-stripper.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
// Generated by CoffeeScript 1.6.3
(function() {
var MailStripper;
MailStripper = (function() {
function MailStripper(opts) {
this.opts = opts;
if (this.opts.patterns) {
this.patterns = this.patterns.concat(this.opts.patterns);
}
}
MailStripper.prototype.patterns = [/^\s*--/, /\d{4}-\d{2}-\d{2} \d{2}:\d{2} GMT\+\d{2}:\d{2}/, /^—/, /<[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,8}>/i];
MailStripper.prototype.lineShouldBeStripped = function(line) {
var pattern, _i, _len, _ref;
_ref = this.patterns;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
pattern = _ref[_i];
if (pattern.test(line)) {
return true;
}
}
return false;
};
MailStripper.prototype.lineContainsName = function(line, name) {
var name_re;
name_re = new RegExp("^\\s*" + name + "\\s*$", 'i');
return name_re.test(line);
};
MailStripper.prototype.parse = function(body, name) {
var line, lines, message_lines, type, _i, _len;
lines = body.replace(/\r\n/g, '\n').split('\n');
message_lines = [];
for (_i = 0, _len = lines.length; _i < _len; _i++) {
line = lines[_i];
type = this.lineShouldBeStripped(line);
if (name) {
type = type || this.lineContainsName(line, name);
}
if (!type) {
message_lines.push(line);
} else {
break;
}
}
while (message_lines.length > 0 && message_lines[message_lines.length - 1] === '') {
message_lines.pop();
}
return message_lines.join('\n');
};
return MailStripper;
})();
module.exports = MailStripper;
}).call(this);