2
0

(condition) Add more comparison operators

Including starts with, ends with, does not contain, is empty

Closes #410
This commit is contained in:
Baptiste Arnaud
2023-03-29 15:18:52 +02:00
parent bb45b33928
commit 80b7dbd19e
3 changed files with 39 additions and 8 deletions

View File

@ -38,13 +38,14 @@ export const ComparisonItem = ({
items={Object.values(ComparisonOperators)}
placeholder="Select an operator"
/>
{item.comparisonOperator !== ComparisonOperators.IS_SET && (
<TextInput
defaultValue={item.value ?? ''}
onChange={handleChangeValue}
placeholder="Type a value..."
/>
)}
{item.comparisonOperator !== ComparisonOperators.IS_SET &&
item.comparisonOperator !== ComparisonOperators.IS_EMPTY && (
<TextInput
defaultValue={item.value ?? ''}
onChange={handleChangeValue}
placeholder="Type a value..."
/>
)}
</Stack>
)
}

View File

@ -31,7 +31,8 @@ export const executeCondition = (
}
const executeComparison =
(variables: Variable[]) => (comparison: Comparison) => {
(variables: Variable[]) =>
(comparison: Comparison): boolean => {
if (!comparison?.variableId) return false
const inputValue =
variables.find((v) => v.id === comparison.variableId)?.value ?? ''
@ -39,6 +40,7 @@ const executeComparison =
findUniqueVariableValue(variables)(comparison.value) ??
parseVariables(variables)(comparison.value)
if (isNotDefined(value)) return false
if (isNotDefined(comparison.comparisonOperator)) return false
switch (comparison.comparisonOperator) {
case ComparisonOperators.CONTAINS: {
const contains = (a: string | null, b: string | null) => {
@ -47,6 +49,13 @@ const executeComparison =
}
return compare(contains, inputValue, value)
}
case ComparisonOperators.NOT_CONTAINS: {
const notContains = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false
return !a.toLowerCase().trim().includes(b.toLowerCase().trim())
}
return compare(notContains, inputValue, value)
}
case ComparisonOperators.EQUAL: {
return compare((a, b) => a === b, inputValue, value)
}
@ -76,6 +85,23 @@ const executeComparison =
case ComparisonOperators.IS_SET: {
return isDefined(inputValue) && inputValue.length > 0
}
case ComparisonOperators.IS_EMPTY: {
return isNotDefined(inputValue) || inputValue.length === 0
}
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 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 compare(endsWith, inputValue, value)
}
}
}

View File

@ -13,9 +13,13 @@ export enum ComparisonOperators {
EQUAL = 'Equal to',
NOT_EQUAL = 'Not equal',
CONTAINS = 'Contains',
NOT_CONTAINS = 'Does not contain',
GREATER = 'Greater than',
LESS = 'Less than',
IS_SET = 'Is set',
IS_EMPTY = 'Is empty',
STARTS_WITH = 'Starts with',
ENDS_WITH = 'Ends with',
}
const comparisonSchema = z.object({