UNPKG

1.41 kBJavaScriptView Raw
1// vendored from https://github.com/amasad/sane/blob/64ff3a870c42e84f744086884bf55a4f9c22d376/src/utils/recrawl-warning-dedupe.js
2'use strict';
3
4class RecrawlWarning {
5 constructor(root, count) {
6 this.root = root;
7 this.count = count;
8 }
9
10 static findByRoot(root) {
11 for (let i = 0; i < this.RECRAWL_WARNINGS.length; i++) {
12 const warning = this.RECRAWL_WARNINGS[i];
13
14 if (warning.root === root) {
15 return warning;
16 }
17 }
18
19 return undefined;
20 }
21
22 static isRecrawlWarningDupe(warningMessage) {
23 if (typeof warningMessage !== 'string') {
24 return false;
25 }
26
27 const match = warningMessage.match(this.REGEXP);
28
29 if (!match) {
30 return false;
31 }
32
33 const count = Number(match[1]);
34 const root = match[2];
35 const warning = this.findByRoot(root);
36
37 if (warning) {
38 // only keep the highest count, assume count to either stay the same or
39 // increase.
40 if (warning.count >= count) {
41 return true;
42 } else {
43 // update the existing warning to the latest (highest) count
44 warning.count = count;
45 return false;
46 }
47 } else {
48 this.RECRAWL_WARNINGS.push(new RecrawlWarning(root, count));
49 return false;
50 }
51 }
52}
53
54RecrawlWarning.RECRAWL_WARNINGS = [];
55RecrawlWarning.REGEXP =
56 /Recrawled this watch (\d+) times, most recently because:\n([^:]+)/;
57module.exports = RecrawlWarning;