Skip to content

Commit

Permalink
🐛 (condition) Improve comparison on dates
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jun 27, 2023
1 parent 3575fef commit 92f7f3c
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions apps/viewer/src/features/blocks/logic/condition/executeCondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,39 @@ const executeComparison =
if (b === '' || !b || !a) return false
return a.toLowerCase().trim().includes(b.toLowerCase().trim())
}
return compare(contains, inputValue, value)
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 compare(notContains, inputValue, value, true)
return compare(notContains, inputValue, value)
}
case ComparisonOperators.EQUAL: {
return compare((a, b) => a === b, inputValue, value)
}
case ComparisonOperators.NOT_EQUAL: {
return compare((a, b) => a !== b, inputValue, value, true)
return compare((a, b) => a !== b, inputValue, value)
}
case ComparisonOperators.GREATER: {
if (isNotDefined(inputValue) || isNotDefined(value)) return false
if (typeof inputValue === 'string') {
if (typeof value === 'string')
return parseFloat(inputValue) > parseFloat(value)
return parseFloat(inputValue) > value.length
return parseDateOrNumber(inputValue) > parseDateOrNumber(value)
return Number(inputValue) > value.length
}
if (typeof value === 'string')
return inputValue.length > parseFloat(value)
if (typeof value === 'string') return inputValue.length > Number(value)
return inputValue.length > value.length
}
case ComparisonOperators.LESS: {
if (isNotDefined(inputValue) || isNotDefined(value)) return false
if (typeof inputValue === 'string') {
if (typeof value === 'string')
return parseFloat(inputValue) < parseFloat(value)
return parseFloat(inputValue) < value.length
return parseDateOrNumber(inputValue) < parseDateOrNumber(value)
return Number(inputValue) < value.length
}
if (typeof value === 'string')
return inputValue.length < parseFloat(value)
if (typeof value === 'string') return inputValue.length < Number(value)
return inputValue.length < value.length
}
case ComparisonOperators.IS_SET: {
Expand Down Expand Up @@ -98,20 +96,29 @@ const compare = (
compareStrings: (a: string | null, b: string | null) => boolean,
a: Exclude<Variable['value'], undefined>,
b: Exclude<Variable['value'], undefined>,
defaultReturnValue = false
type: 'every' | 'some' = 'every'
): boolean => {
if (!a || typeof a === 'string') {
if (!b || typeof b === 'string') return compareStrings(a, b)
return defaultReturnValue === true
return type === 'every'
? b.every((b) => compareStrings(a, b))
: b.some((b) => compareStrings(a, b))
}
if (!b || typeof b === 'string') {
return defaultReturnValue === true
return type === 'every'
? a.every((a) => compareStrings(a, b))
: a.some((a) => compareStrings(a, b))
}
if (defaultReturnValue === true)
if (type === 'every')
return a.every((a) => b.every((b) => compareStrings(a, b)))
return a.some((a) => b.some((b) => compareStrings(a, b)))
}

const parseDateOrNumber = (value: string): number => {
const parsed = Number(value)
if (isNaN(parsed)) {
const time = Date.parse(value)
return time
}
return parsed
}

1 comment on commit 92f7f3c

@vercel
Copy link

@vercel vercel bot commented on 92f7f3c Jun 27, 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

bii.bj
1stop.au
wasap.nl
x.cr8.ai
yobot.me
klujo.com
me.cr8.ai
wachat.io
wassep.io
247987.com
8jours.top
aginap.com
ai.mprs.in
bee.cr8.ai
bot.aws.bj
bot.bbc.bj
cat.cr8.ai
finplex.be
jxi.cr8.ai
nepkit.com
pig.cr8.ai
sat.cr8.ai
wachats.me
wsapio.com
blogely.com
bot.aipr.kr
bot.joof.it
bot.kloo.me
bull.cr8.ai
docs.cr8.ai
getyour.sbs
icon.cr8.ai
minipost.uk
mole.cr8.ai
team.cr8.ai
wolf.cr8.ai
ai.meant.com
bet7k.online
bot.afric.ai
bot.grace.bj
cinecorn.com
gniorder.com
help.taxt.co
kusamint.com
rhino.cr8.ai
sheep.cr8.ai
snake.cr8.ai
svhm.mprs.in
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
chat.atlasoutfittersk9.com
configurator.bouclidom.com
demo.virtuesocialmedia.com
type.cookieacademyonline.com
upload.atlasoutfittersk9.com
bot.brigadeirosemdrama.com.br
mdb.eventoequipe.progenbr.com
tuttirecepcao.fratucci.com.br
forms.escoladeautomacao.com.br
onboarding.libertydreamcare.ie
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
chat.alfabetizacaobilingue.com.br
gerador.verificadordehospedes.com
personal-trainer.barrettamario.it
sondaggio.mosaicohairboutique.com
mdb.assessoria.ademir.progenbr.com
preagendamento.sergiolimajr.com.br
studiotecnicoimmobiliaremerelli.it
download.thailandmicespecialist.com
register.thailandmicespecialist.com
mdb.evento.equipeinterna.progenbr.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
mdb.assessoria.qrcode.ademir.progenbr.com

Please sign in to comment.