From 8e7479a1bde96fa6f46d40b7e96eea23dbc9fbfd Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 22 Jun 2023 11:39:03 +0200 Subject: [PATCH] :bug: (condition) Fix condition parsing with value "null" --- .../logic/condition/executeCondition.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/apps/viewer/src/features/blocks/logic/condition/executeCondition.ts b/apps/viewer/src/features/blocks/logic/condition/executeCondition.ts index c72eabbcf9..ad75e3682f 100644 --- a/apps/viewer/src/features/blocks/logic/condition/executeCondition.ts +++ b/apps/viewer/src/features/blocks/logic/condition/executeCondition.ts @@ -20,13 +20,14 @@ 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 ?? null + if (isNotDefined(comparison.value)) return false const value = - findUniqueVariableValue(variables)(comparison.value) ?? - parseVariables(variables)(comparison.value) - if (isNotDefined(value)) return false + comparison.value === 'undefined' || comparison.value === 'null' + ? null + : findUniqueVariableValue(variables)(comparison.value) ?? + parseVariables(variables)(comparison.value) if (isNotDefined(comparison.comparisonOperator)) return false switch (comparison.comparisonOperator) { case ComparisonOperators.CONTAINS: { @@ -50,7 +51,7 @@ const executeComparison = return compare((a, b) => a !== b, inputValue, value, true) } case ComparisonOperators.GREATER: { - if (isNotDefined(inputValue)) return false + if (isNotDefined(inputValue) || isNotDefined(value)) return false if (typeof inputValue === 'string') { if (typeof value === 'string') return parseFloat(inputValue) > parseFloat(value) @@ -61,7 +62,7 @@ const executeComparison = return inputValue.length > value.length } case ComparisonOperators.LESS: { - if (isNotDefined(inputValue)) return false + if (isNotDefined(inputValue) || isNotDefined(value)) return false if (typeof inputValue === 'string') { if (typeof value === 'string') return parseFloat(inputValue) < parseFloat(value) @@ -96,18 +97,17 @@ const executeComparison = const compare = ( compareStrings: (a: string | null, b: string | null) => boolean, - a: Variable['value'], - b: Variable['value'], + a: Exclude, + b: Exclude, defaultReturnValue = false ): boolean => { - if (!a || !b) return defaultReturnValue - if (typeof a === 'string') { - if (typeof b === 'string') return compareStrings(a, b) + if (!a || typeof a === 'string') { + if (!b || 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') { + if (!b || typeof b === 'string') { return defaultReturnValue === true ? a.every((a) => compareStrings(a, b)) : a.some((a) => compareStrings(a, b))