Skip to content

Commit

Permalink
🐛 Normalize user inputs before comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Aug 8, 2023
1 parent a72cb5e commit 2b4ada0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ const matchComparison = (
switch (comparisonOperator) {
case ComparisonOperators.CONTAINS: {
if (!inputValue || !value) return false
return inputValue.toLowerCase().includes(value.toLowerCase())
return inputValue
.toLowerCase()
.trim()
.normalize()
.includes(value.toLowerCase().trim().normalize())
}
case ComparisonOperators.EQUAL: {
return inputValue === value
return inputValue?.normalize() === value?.normalize()
}
case ComparisonOperators.NOT_EQUAL: {
return inputValue !== value
return inputValue?.normalize() !== value?.normalize()
}
case ComparisonOperators.GREATER: {
if (!inputValue || !value) return false
Expand All @@ -65,15 +69,27 @@ const matchComparison = (
}
case ComparisonOperators.STARTS_WITH: {
if (!inputValue || !value) return false
return inputValue.toLowerCase().startsWith(value.toLowerCase())
return inputValue
.toLowerCase()
.trim()
.normalize()
.startsWith(value.toLowerCase().trim().normalize())
}
case ComparisonOperators.ENDS_WITH: {
if (!inputValue || !value) return false
return inputValue.toLowerCase().endsWith(value.toLowerCase())
return inputValue
.toLowerCase()
.trim()
.normalize()
.endsWith(value.toLowerCase().trim().normalize())
}
case ComparisonOperators.NOT_CONTAINS: {
if (!inputValue || !value) return false
return !inputValue?.toLowerCase().includes(value.toLowerCase())
return !inputValue
?.toLowerCase()
.trim()
.normalize()
.includes(value.toLowerCase().trim().normalize())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,46 @@ const executeComparison =
case ComparisonOperators.CONTAINS: {
const contains = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false
return a.toLowerCase().trim().includes(b.toLowerCase().trim())
return a
.toLowerCase()
.trim()
.normalize()
.includes(b.toLowerCase().trim().normalize())
}
return compare(contains, inputValue, value, 'some')
}
case ComparisonOperators.NOT_CONTAINS: {
const notContains = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return true
return !a.toLowerCase().trim().includes(b.toLowerCase().trim())
return !a
.toLowerCase()
.trim()
.normalize()
.includes(b.toLowerCase().trim().normalize())
}
return compare(notContains, inputValue, value)
}
case ComparisonOperators.EQUAL: {
return compare((a, b) => a === b, inputValue, value)
return compare(
(a, b) => {
if (typeof a === 'string' && typeof b === 'string')
return a.normalize() === b.normalize()
return a === b
},
inputValue,
value
)
}
case ComparisonOperators.NOT_EQUAL: {
return compare((a, b) => a !== b, inputValue, value)
return compare(
(a, b) => {
if (typeof a === 'string' && typeof b === 'string')
return a.normalize() !== b.normalize()
return a !== b
},
inputValue,
value
)
}
case ComparisonOperators.GREATER: {
if (isNotDefined(inputValue) || isNotDefined(value)) return false
Expand Down Expand Up @@ -78,14 +102,22 @@ const executeComparison =
case ComparisonOperators.STARTS_WITH: {
const startsWith = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false
return a.toLowerCase().trim().startsWith(b.toLowerCase().trim())
return a
.toLowerCase()
.trim()
.normalize()
.startsWith(b.toLowerCase().trim().normalize())
}
return compare(startsWith, inputValue, value)
}
case ComparisonOperators.ENDS_WITH: {
const endsWith = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false
return a.toLowerCase().trim().endsWith(b.toLowerCase().trim())
return a
.toLowerCase()
.trim()
.normalize()
.endsWith(b.toLowerCase().trim().normalize())
}
return compare(endsWith, inputValue, value)
}
Expand Down
21 changes: 6 additions & 15 deletions apps/viewer/src/features/chat/helpers/continueBotFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,21 +266,10 @@ const getOutgoingEdgeId =
reply
) {
const matchedItem = block.items.find(
(item) => parseVariables(variables)(item.content) === reply
(item) =>
parseVariables(variables)(item.content).normalize() ===
reply.normalize()
)
// TEMPORARY LOG
if (block.id === 'n62dzuz2ye73tivnjw7bnxd5') {
console.log(
'Debug Safari bug:',
JSON.stringify({
items: block.items.map((item) =>
parseVariables(variables)(item.content)
),
reply,
})
)
}

if (matchedItem?.outgoingEdgeId) return matchedItem.outgoingEdgeId
}
if (
Expand All @@ -289,7 +278,9 @@ const getOutgoingEdgeId =
reply
) {
const matchedItem = block.items.find(
(item) => parseVariables(variables)(item.title) === reply
(item) =>
parseVariables(variables)(item.title).normalize() ===
reply.normalize()
)
if (matchedItem?.outgoingEdgeId) return matchedItem.outgoingEdgeId
}
Expand Down

3 comments on commit 2b4ada0

@vercel
Copy link

@vercel vercel bot commented on 2b4ada0 Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2b4ada0 Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

stan.vselise.com
typebot.aloe.bot
vidalimentar.com
voicehelp.cr8.ai
www.chatgpt-biliran.com
91181264.your-access.one
abg-assistent.m-vogel.de
ai.chromebookstoreph.com
assistenza.mariofusco.it
bot.ativandograna.online
bot.conversaprivada.site
chat.ambassadorelena.com
chat.hiabhaykulkarni.com
concept.socialcliques.me
contextone.wpwakanda.com
form.sergiolimajr.com.br
hunterbot.saleshunter.ai
invite.bridesquadapp.com
link.cascadigital.com.br
listasecreta.utuahub.com
login.algorithmpress.com
mail2wa.barrettamario.it
onboarding.growthside.io
reducao.onewebcenter.com
reward.onlinebotdemo.xyz
sondaggio.thegymgame.com
stap.venturemarketing.in
type.boardacademy.com.br
type.opaulovieira.com.br
aibot.angrybranding.co.uk
alistasecreta.utuahub.com
app.photosbackupin1.click
bot.aidigitalmarketing.kr
bot.arraesecenteno.com.br
bot.blackboxsports.com.br
bot.cabinrentalagency.com
bot.draelzanakahagi.cloud
bot.fusionstarreviews.com
bot.ristorantekintsugi.it
boyfriend-breakup.riku.ai
brigadeirosemdrama.com.br
chat.ertcrebateportal.com
chat.sucessonarede.com.br
chat.thehomebuyersusa.com
chat.thisiscrushhouse.com
chat2.ambassadorelena.com
consulta.jornalextra.site
healthandsafetycourses.uk
mdb.imprensa.progenbr.com
sellmyharleylouisiana.com
testbot.sharemyreview.net
thesoulmatedrawing.online
typebot-viewer.vercel.app
verfica.botmachine.com.br
viewer-v2-typebot-io.vercel.app
prenotazione.ristorantekintsugi.it
download.thailandmicespecialist.com
mdb.assessoria.aloisio.progenbr.com
mdb.assessoria.girotto.progenbr.com
mdb.assessoria.marinho.progenbr.com
mdb.assessoria.rodrigo.progenbr.com
register.thailandmicespecialist.com
mdb.assessoria.desideri.progenbr.com
mdb.assessoria.fernanda.progenbr.com
mdb.assessoria.jbatista.progenbr.com
mdb.assessoria.mauricio.progenbr.com
form.shopmercedesbenzsouthorlando.com
mdb.evento.equipeinterna.progenbr.com
bot.studiotecnicoimmobiliaremerelli.it
mdb.assessoria.boaventura.progenbr.com
mdb.assessoria.jtrebesqui.progenbr.com
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
mdb.assessoria.carreirinha.progenbr.com
chrome-os-inquiry-system.itschromeos.com
mdb.assessoria.paulomarques.progenbr.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com
mdb.assessoria.qrcode.ademir.progenbr.com
mdb.assessoria.qrcode.arthur.progenbr.com
mdb.assessoria.qrcode.danilo.progenbr.com
mdb.assessoria.qrcode.marcao.progenbr.com
mdb.assessoria.qrcode.marcio.progenbr.com
mdb.assessoria.qrcode.aloisio.progenbr.com
mdb.assessoria.qrcode.girotto.progenbr.com
mdb.assessoria.qrcode.marinho.progenbr.com
mdb.assessoria.qrcode.rodrigo.progenbr.com
mdb.assessoria.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.desideri.progenbr.com
mdb.assessoria.qrcode.fernanda.progenbr.com
mdb.assessoria.qrcode.jbatista.progenbr.com
mdb.assessoria.qrcode.mauricio.progenbr.com
mdb.assessoria.fernanda.regional.progenbr.com
mdb.assessoria.qrcode.boaventura.progenbr.com
mdb.assessoria.qrcode.jtrebesqui.progenbr.com
mdb.assessoria.qrcode.carreirinha.progenbr.com
mdb.assessoria.qrcode.paulomarques.progenbr.com
mdb.assessoria.qrcode.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.fernanda.regional.progenbr.com

@vercel
Copy link

@vercel vercel bot commented on 2b4ada0 Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs.typebot.io
docs-typebot-io.vercel.app

Please sign in to comment.