From 81472421840a77b5e5b4080c73c89d78dbf7dde3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CBastien?= <“bastien.hubert@hotmail.com”> Date: Tue, 28 Jan 2025 17:47:50 +0100 Subject: [PATCH 1/2] Add custom equality check function for Rule Based Styling --- src/extensions/styling/StyleRuleEvaluator.ts | 21 ++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/extensions/styling/StyleRuleEvaluator.ts b/src/extensions/styling/StyleRuleEvaluator.ts index e8908a9c3..4661920a2 100644 --- a/src/extensions/styling/StyleRuleEvaluator.ts +++ b/src/extensions/styling/StyleRuleEvaluator.ts @@ -63,7 +63,7 @@ export const evaluateRulesOnDict = (dict, rules, customizations) => { } for (const [index, rule] of rules.entries()) { // Only check customizations that are specified - return evaluateSingleRuleOnDict (dict, rule, index, customizations) + return evaluateSingleRuleOnDict(dict, rule, index, customizations); } // If no rules are met, return not found (index=-1) return -1; @@ -81,8 +81,7 @@ export const evaluateSingleRuleOnDict = (dict, rule, ruleIndex, customizations) } } return -1; -} - +}; /** * Evaluates the specified rule set on a node object returned by the Neo4j driver. @@ -127,6 +126,20 @@ export const evaluateRules = (entity, customization, defaultValue, rules, entity return defaultValue; }; +/** + * @param realValue the value found in the real data returned by the query + * @param ruleValue the value specified in the rule. + * @returns whether the condition is met. + */ +const isLooselyEqual = (realValue, ruleValue) => { + // In order to avoid having '5' <> {low: 5, high: 0} OR '5' <> 5 + const sensitiveTypes = ['string', 'number', 'object']; + if (sensitiveTypes.includes(typeof realValue) && sensitiveTypes.includes(typeof ruleValue)) { + return realValue == ruleValue; + } + return realValue === ruleValue; +}; + /** * @param realValue the value found in the real data returned by the query * @param condition the condition, one of [=,!=,<,<=,>,>=,contains]. @@ -139,7 +152,7 @@ const evaluateCondition = (realValue, condition, ruleValue) => { return false; } if (condition == '=') { - return realValue === ruleValue; + return isLooselyEqual(realValue, ruleValue); } if (condition == '!=') { return realValue !== ruleValue; From 3f351e6dc423ffd5ad4c8e03dd3aca4fd0107ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CBastien?= <“bastien.hubert@hotmail.com”> Date: Mon, 3 Feb 2025 15:15:08 +0100 Subject: [PATCH 2/2] Use isLooselyEqual to verify "!=" conditions in rule based styling --- src/extensions/styling/StyleRuleEvaluator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/styling/StyleRuleEvaluator.ts b/src/extensions/styling/StyleRuleEvaluator.ts index 4661920a2..43cc6a092 100644 --- a/src/extensions/styling/StyleRuleEvaluator.ts +++ b/src/extensions/styling/StyleRuleEvaluator.ts @@ -155,7 +155,7 @@ const evaluateCondition = (realValue, condition, ruleValue) => { return isLooselyEqual(realValue, ruleValue); } if (condition == '!=') { - return realValue !== ruleValue; + return !isLooselyEqual(realValue, ruleValue); } if (!isNaN(Number(ruleValue))) { ruleValue = Number(ruleValue);