Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make re2 an optional peer dependency #34

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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