email-forward-parser 1.0.23 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/utils.js +41 -8
  2. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -13,12 +13,15 @@ var RE2 = require("re2");
13
13
  /**
14
14
  * Executes multiple regular expressions until one succeeds
15
15
  * @public
16
- * @param {object} regexes
17
- * @param {string} str
18
- * @param {string} [mode]
19
- * @return {object} The result of the first successfull regular expression
16
+ * @param {object} regexes
17
+ * @param {string} str
18
+ * @param {string} [mode]
19
+ * @param {boolean} [highestPosition]
20
+ * @return {object} The result of the first successfull regular expression
20
21
  */
21
- var loopRegexes = function(regexes, str, mode = "match") {
22
+ var loopRegexes = function(
23
+ regexes, str, mode = "match", highestPosition = true
24
+ ) {
22
25
  var _match;
23
26
 
24
27
  for (var _i = 0; _i < regexes.length; _i++) {
@@ -29,11 +32,13 @@ var loopRegexes = function(regexes, str, mode = "match") {
29
32
  _regex = new RE2(regexes[_i]);
30
33
  }
31
34
 
32
- _match = (
35
+ var _currentMatch = (
33
36
  _regex[mode](str || "", mode === "replace" ? "" : undefined)
34
37
  );
35
38
 
36
39
  if (mode === "replace") {
40
+ _match = _currentMatch;
41
+
37
42
  // A successfull replace must return a smaller string than the input
38
43
  var _maxLength = str.length;
39
44
 
@@ -44,8 +49,36 @@ var loopRegexes = function(regexes, str, mode = "match") {
44
49
  // A successfull split must return an array with at least two items
45
50
  var _minLength = mode === "split" ? 1 : 0;
46
51
 
47
- if ((_match || []).length > _minLength) {
48
- break;
52
+ if ((_currentMatch || []).length > _minLength) {
53
+ if (highestPosition === true) {
54
+ // No previous match?
55
+ if (!_match) {
56
+ // Save match and continue looking for other matches
57
+ _match = _currentMatch;
58
+ } else {
59
+ var _higher;
60
+
61
+ if (mode === "match") {
62
+ _higher = _match.index > _currentMatch.index;
63
+ } else if (mode === "split") {
64
+ _higher = (
65
+ _match.length === _currentMatch.length &&
66
+ _match[0].length > _currentMatch[0].length
67
+ );
68
+ }
69
+
70
+ // Match higher positioned than previous one?
71
+ if (_higher) {
72
+ // Replace match and continue looking for other matches
73
+ _match = _currentMatch;
74
+ }
75
+ }
76
+ } else {
77
+ // Save match and stop
78
+ _match = _currentMatch;
79
+
80
+ break;
81
+ }
49
82
  }
50
83
  }
51
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "email-forward-parser",
3
- "version": "1.0.23",
3
+ "version": "1.1.0",
4
4
  "description": "Parses forwarded emails and extract content",
5
5
  "author": "Eliott Vincent <eliott@crisp.chat>",
6
6
  "main": "lib/index.js",