Skip to content

Commit

Permalink
Merge pull request #34 from aldenquimby/master
Browse files Browse the repository at this point in the history
feat: make re2 an optional peer dependency
  • Loading branch information
baptistejamin authored Jan 13, 2025
2 parents ff295b9 + f164344 commit d0be7a5
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ node_modules/
package-lock.json

.npm

.idea/
21 changes: 17 additions & 4 deletions lib/regex.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
var RE2 = require("re2");
let RE2;
let hasRE2;

class RegexList {
constructor() {
// inspiration: https://github.com/spamscanner/url-regex-safe/blob/6c1e2c3b5557709633a2cc971d599469ea395061/src/index.js#L37-L49
this.SafeRegExp = hasRE2 !== false
? (() => {
if (typeof RE2 === 'function') return RE2;
try {
RE2 = require('re2');
return typeof RE2 === 'function' ? RE2 : RegExp;
} catch {
hasRE2 = false;
return RegExp;
}
})()
: RegExp;

this.quoteHeadersRegex = this.buildRe2([
/^-*\s*(On\s.+\s.+\n?wrote:{0,1})\s{0,1}-*$/m, // On DATE, NAME <EMAIL> wrote:
/^-*\s*(Le\s.+\s.+\n?écrit\s?:{0,1})\s{0,1}-*$/m, // Le DATE, NAME <EMAIL> a écrit :
Expand Down Expand Up @@ -75,9 +90,7 @@ class RegexList {
}

buildRe2(regexList) {
return regexList.map((regex) => {
return new RE2(regex);
});
return regexList.map((regex) => new this.SafeRegExp(regex));
}
}

Expand Down
Loading

0 comments on commit d0be7a5

Please sign in to comment.