Skip to content

Commit f164344

Browse files
committed
feat: make re2 an optional peer dependency
1 parent 71d8cb5 commit f164344

File tree

4 files changed

+182
-55
lines changed

4 files changed

+182
-55
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ node_modules/
66
package-lock.json
77

88
.npm
9+
10+
.idea/

lib/regex.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
var RE2 = require("re2");
1+
let RE2;
2+
let hasRE2;
23

34
class RegexList {
45
constructor() {
6+
// inspiration: https://github.com/spamscanner/url-regex-safe/blob/6c1e2c3b5557709633a2cc971d599469ea395061/src/index.js#L37-L49
7+
this.SafeRegExp = hasRE2 !== false
8+
? (() => {
9+
if (typeof RE2 === 'function') return RE2;
10+
try {
11+
RE2 = require('re2');
12+
return typeof RE2 === 'function' ? RE2 : RegExp;
13+
} catch {
14+
hasRE2 = false;
15+
return RegExp;
16+
}
17+
})()
18+
: RegExp;
19+
520
this.quoteHeadersRegex = this.buildRe2([
621
/^-*\s*(On\s.+\s.+\n?wrote:{0,1})\s{0,1}-*$/m, // On DATE, NAME <EMAIL> wrote:
722
/^-*\s*(Le\s.+\s.+\n?écrit\s?:{0,1})\s{0,1}-*$/m, // Le DATE, NAME <EMAIL> a écrit :
@@ -75,9 +90,7 @@ class RegexList {
7590
}
7691

7792
buildRe2(regexList) {
78-
return regexList.map((regex) => {
79-
return new RE2(regex);
80-
});
93+
return regexList.map((regex) => new this.SafeRegExp(regex));
8194
}
8295
}
8396

0 commit comments

Comments
 (0)