🐛 (conditions) Fix negative comparisons
This commit is contained in:
@ -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}
|
||||
>
|
||||
|
@ -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,
|
||||
@ -7,7 +8,6 @@ import {
|
||||
LogicalOperator,
|
||||
Variable,
|
||||
} from '@typebot.io/schemas'
|
||||
import { parseVariables } from 'bot-engine'
|
||||
|
||||
export const executeCondition =
|
||||
(variables: Variable[]) =>
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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 a.some((a) => func(a, b))
|
||||
return a.some((a) => b.some((b) => func(a, b)))
|
||||
if (typeof b === 'string') {
|
||||
return defaultReturnValue === true
|
||||
? a.every((a) => compareStrings(a, b))
|
||||
: a.some((a) => compareStrings(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)))
|
||||
}
|
||||
|
Reference in New Issue
Block a user