mirror of
https://github.com/wassname/talk.git
synced 2026-08-11 11:27:10 +08:00
Fix regexp mathing of banned word list by exchanging .test() with .match() (#2828)
* [Fix] Use .match instead of .test to avoid false positives when matching regex returned groups * Add test to assure memoized regexp for banned words will match properly * fix: fixes bug with wordlist matching Co-authored-by: Wyatt Johnson <accounts+github@wyattjoh.ca>
This commit is contained in:
co-authored by
Wyatt Johnson
parent
1388e0bc5e
commit
3014017976
@@ -1,4 +1,7 @@
|
||||
import { containsMatchingPhrase } from "coral-server/services/comments/pipeline/wordList";
|
||||
import {
|
||||
containsMatchingPhrase,
|
||||
containsMatchingPhraseMemoized,
|
||||
} from "coral-server/services/comments/pipeline/wordList";
|
||||
|
||||
const phrases = [
|
||||
"cookies",
|
||||
@@ -44,3 +47,20 @@ describe("containsMatchingPhrase", () => {
|
||||
expect(containsMatchingPhrase([], "test")).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("containsMatchingPhraseMemoized", () => {
|
||||
it("return true for all cases after memoizing the first result", () => {
|
||||
[
|
||||
"cookies 1",
|
||||
"cookies 2",
|
||||
"cookies 4",
|
||||
"cookies 5",
|
||||
"this is for cookies 6",
|
||||
"this is for cookies 7",
|
||||
"this is for cookies 8",
|
||||
"this is for cookies 9",
|
||||
].forEach(word => {
|
||||
expect(containsMatchingPhraseMemoized(phrases, word)).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,7 +22,7 @@ export function generateRegExp(phrases: string[]) {
|
||||
.join('[\\s"?!.]+')
|
||||
)
|
||||
.join("|");
|
||||
return new RegExp(`(^|[^\\w])(${inner})(?=[^\\w]|$)`, "gmiu");
|
||||
return new RegExp(`(^|[^\\w])(${inner})(?=[^\\w]|$)`, "miu");
|
||||
}
|
||||
|
||||
export const generateRegExpMemoized = memoize(generateRegExp);
|
||||
|
||||
Reference in New Issue
Block a user