Skip to content

Commit

Permalink
🐛 (conditions) Fix negative comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jun 19, 2023
1 parent 0e31a6c commit cec072b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const ButtonsItemSettings = ({ item, onSettingsChange }: Props) => {
<Stack spacing={4}>
<SwitchWithRelatedSettings
label="Display condition"
moreInfoContent="Only display this item if a condition is met."
initialValue={item.displayCondition?.isEnabled ?? false}
onCheckChange={updateIsDisplayConditionEnabled}
>
Expand Down
38 changes: 25 additions & 13 deletions apps/viewer/src/features/blocks/logic/condition/executeCondition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { findUniqueVariableValue } from '@/features/variables/findUniqueVariableValue'
import { parseVariables } from '@/features/variables/parseVariables'
import { isNotDefined, isDefined } from '@typebot.io/lib'
import {
Comparison,
Expand All @@ -7,7 +8,6 @@ import {
LogicalOperator,
Variable,
} from '@typebot.io/schemas'
import { parseVariables } from 'bot-engine'

export const executeCondition =
(variables: Variable[]) =>
Expand All @@ -20,8 +20,9 @@ const executeComparison =
(variables: Variable[]) =>
(comparison: Comparison): boolean => {
if (!comparison?.variableId) return false
const inputValue =
variables.find((v) => v.id === comparison.variableId)?.value ?? ''
const inputValue = variables.find(
(v) => v.id === comparison.variableId
)?.value
const value =
findUniqueVariableValue(variables)(comparison.value) ??
parseVariables(variables)(comparison.value)
Expand All @@ -37,18 +38,19 @@ const executeComparison =
}
case ComparisonOperators.NOT_CONTAINS: {
const notContains = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false
if (b === '' || !b || !a) return true
return !a.toLowerCase().trim().includes(b.toLowerCase().trim())
}
return compare(notContains, inputValue, value)
return compare(notContains, inputValue, value, true)
}
case ComparisonOperators.EQUAL: {
return compare((a, b) => a === b, inputValue, value)
}
case ComparisonOperators.NOT_EQUAL: {
return compare((a, b) => a !== b, inputValue, value)
return compare((a, b) => a !== b, inputValue, value, true)
}
case ComparisonOperators.GREATER: {
if (isNotDefined(inputValue)) return false
if (typeof inputValue === 'string') {
if (typeof value === 'string')
return parseFloat(inputValue) > parseFloat(value)
Expand All @@ -59,6 +61,7 @@ const executeComparison =
return inputValue.length > value.length
}
case ComparisonOperators.LESS: {
if (isNotDefined(inputValue)) return false
if (typeof inputValue === 'string') {
if (typeof value === 'string')
return parseFloat(inputValue) < parseFloat(value)
Expand Down Expand Up @@ -92,15 +95,24 @@ const executeComparison =
}

const compare = (
func: (a: string | null, b: string | null) => boolean,
compareStrings: (a: string | null, b: string | null) => boolean,
a: Variable['value'],
b: Variable['value']
b: Variable['value'],
defaultReturnValue = false
): boolean => {
if (!a || !b) return false
if (!a || !b) return defaultReturnValue
if (typeof a === 'string') {
if (typeof b === 'string') return func(a, b)
return b.some((b) => func(a, b))
if (typeof b === 'string') return compareStrings(a, b)
return defaultReturnValue === true
? b.every((b) => compareStrings(a, b))
: b.some((b) => compareStrings(a, b))
}
if (typeof b === 'string') {
return defaultReturnValue === true
? a.every((a) => compareStrings(a, b))
: a.some((a) => compareStrings(a, b))
}
if (typeof b === 'string') return a.some((a) => func(a, b))
return a.some((a) => b.some((b) => func(a, b)))
if (defaultReturnValue === true)
return a.every((a) => b.every((b) => compareStrings(a, b)))
return a.some((a) => b.some((b) => compareStrings(a, b)))
}

4 comments on commit cec072b

@vercel
Copy link

@vercel vercel bot commented on cec072b Jun 19, 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-typebot-io.vercel.app
docs-git-main-typebot-io.vercel.app
docs.typebot.io

@vercel
Copy link

@vercel vercel bot commented on cec072b Jun 19, 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 cec072b Jun 19, 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:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app
app.typebot.io

@vercel
Copy link

@vercel vercel bot commented on cec072b Jun 19, 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

apo.nigerias.io
app.blogely.com
apr.nigerias.io
aso.nigerias.io
blackcan.cr8.ai
blackvip.online
bot.4display.nl
bot.artiweb.app
bot.devitus.com
bot.jesopizz.it
bot.reeplai.com
bot.renovato.it
bot.scayver.com
bot.tc-mail.com
carspecs.lam.ee
chat.lalmon.com
chat.sureb4.com
eventhub.com.au
fitness.riku.ai
games.klujo.com
proscale.com.br
sakuranembro.it
sellmycarbr.com
typebot.aloe.do
bot.contakit.com
bot.piccinato.co
bot.sv-energy.it
botc.ceox.com.br
clo.closeer.work
cockroach.cr8.ai
desafioem21d.com
faqs.nigerias.io
form.syncwin.com
go.chatbotcv.com
haymanevents.com
kw.wpwakanda.com
myrentalhost.com
stan.vselise.com
start.taxtree.io
typebot.aloe.bot
voicehelp.cr8.ai
zap.fundviser.in
app.bouclidom.com
app.chatforms.net
bot.aldoemaria.it
bot.chatbotcv.com
chat.thisiscrushhouse.com
chat2.ambassadorelena.com
healthandsafetycourses.uk
sellmyharleylouisiana.com
testbot.sharemyreview.net
typebot-viewer.vercel.app
verfica.botmachine.com.br
ap-help.algorithmpress.com
ap-main.algorithmpress.com
asking.aschenputtel.agency
bcorporate.carlosbusch.com
bot.adventureconsulting.hu
bot2.fusionstarreviews.com
casestudyemb.wpwakanda.com
desabafe.sergiolimajr.com.br
download.venturemarketing.in
open.campus.aalen.university
piazzatorre.barrettamario.it
poll.mosaicohairboutique.com
type.cookieacademyonline.com
upload.atlasoutfittersk9.com
bot.brigadeirosemdrama.com.br
tuttirecepcao.fratucci.com.br
forms.escoladeautomacao.com.br
onboarding.libertydreamcare.ie
recepcao.tutti.fratucci.com.br
type.talitasouzamarques.com.br
agendamento.sergiolimajr.com.br
anamnese.clinicamegasjdr.com.br
bookings.littlepartymonkeys.com
bot.comercializadoraomicron.com
elevateyourmind.groovepages.com
viewer-v2-typebot-io.vercel.app
yourfeedback.comebackreward.com
baleia.testeeventos.progenbr.com
bot.cabin-rentals-of-georgia.net
chat.portaloficialautorizado.com
open.campus.bot.aalen.university
sondaggio.mosaicohairboutique.it
baleia.testegabinete.progenbr.com
gerador.verificadordehospedes.com
personal-trainer.barrettamario.it
sondaggio.mosaicohairboutique.com
preagendamento.sergiolimajr.com.br
studiotecnicoimmobiliaremerelli.it
download.thailandmicespecialist.com
register.thailandmicespecialist.com
bot.studiotecnicoimmobiliaremerelli.it
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
chrome-os-inquiry-system.itschromeos.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com

Please sign in to comment.