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:
Hector Nieva
2020-02-08 00:28:08 +00:00
committed by GitHub
co-authored by Wyatt Johnson
parent 1388e0bc5e
commit 3014017976
2 changed files with 22 additions and 2 deletions
@@ -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);