Skip to content

Commit

Permalink
check the normalised text
Browse files Browse the repository at this point in the history
  • Loading branch information
bwsarge committed Dec 21, 2024
1 parent 3d357ba commit 70c7373
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 7 additions & 0 deletions functions/src/validators/whatsapp/checkWhatsappText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@ Referral Code/推荐码: XYZ789
"Referral Code/推荐码: ABC123 incomplete template"
)
})

it("should remove the second template completely", () => {
const message = `S/N: 031jFpSWu
Simply send this message to get started! (按发送按钮!) 👉`
expect(stripTemplate(message, template2)).toBe("")
})
})
5 changes: 3 additions & 2 deletions functions/src/validators/whatsapp/checkWhatsappText.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WhatsappMessageObject } from "../../types"
import { normalizeSpaces } from "../../definitions/common/utils"

function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
Expand Down Expand Up @@ -35,11 +36,11 @@ export function checkTemplate(message: string, template: string) {
}

export function stripTemplate(message: string, template: string) {
const textNormalised = normalizeSpaces(message)
// Build the regex pattern from the template without anchors
const regex = buildRegexFromTemplate(template, true)

// Remove all occurrences of the template and trim whitespace
return message.replace(regex, "").trim()
return textNormalised.replace(regex, "").trim()
}

export function checkMenu(text: string) {
Expand Down

0 comments on commit 70c7373

Please sign in to comment.