Skip to content

Commit

Permalink
Lowercase post text before comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Feb 26, 2024
1 parent e5bc997 commit 45c14cd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/lib/__tests__/moderatePost_wrapped.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,35 @@ describe(`hasMutedWord`, () => {
})
})

describe(`Why so S@assy?`, () => {
const rt = new RichText({
text: `Why so S@assy?`,
})
rt.detectFacetsWithoutResolution()

it(`match: S@assy`, () => {
const match = hasMutedWord(
[{value: 'S@assy', targets: ['content']}],
rt.text,
rt.facets,
[],
)

expect(match).toBe(true)
})

it(`match: s@assy`, () => {
const match = hasMutedWord(
[{value: 's@assy', targets: ['content']}],
rt.text,
rt.facets,
[],
)

expect(match).toBe(true)
})
})

describe(`!command`, () => {
const rt = new RichText({
text: `Idk maybe a bot !command`,
Expand Down
11 changes: 6 additions & 5 deletions src/lib/moderatePost_wrapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ export function hasMutedWord(

for (const mute of mutedWords) {
const mutedWord = mute.value.toLowerCase()
const postText = text.toLowerCase()

// `content` applies to tags as well
if (tags.includes(mutedWord)) return true
// rest of the checks are for `content` only
if (!mute.targets.includes('content')) continue
// single character, has to use includes
if (mutedWord.length === 1 && text.includes(mutedWord)) return true
if (mutedWord.length === 1 && postText.includes(mutedWord)) return true
// too long
if (mutedWord.length > text.length) continue
if (mutedWord.length > postText.length) continue
// exact match
if (mutedWord === text) return true
if (mutedWord === postText) return true
// any muted phrase with space or punctuation
if (/(?:\s|\p{P})+?/u.test(mutedWord) && text.includes(mutedWord))
if (/(?:\s|\p{P})+?/u.test(mutedWord) && postText.includes(mutedWord))
return true

// check individual character groups
const words = text.toLowerCase().split(REGEX.WORD_BOUNDARY)
const words = postText.split(REGEX.WORD_BOUNDARY)
for (const word of words) {
if (word === mutedWord) return true

Expand Down

0 comments on commit 45c14cd

Please sign in to comment.