Skip to content

Commit

Permalink
⚡ (condition) Add more comparison operators
Browse files Browse the repository at this point in the history
Including starts with, ends with, does not contain, is empty

Closes #410
  • Loading branch information
baptisteArno committed Mar 29, 2023
1 parent bb45b33 commit 80b7dbd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ 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 ?? ''
const value =
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) => {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/schemas/features/blocks/logic/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 80b7dbd

Please sign in to comment.