Skip to content

Commit bfc8bd5

Browse files
Merge pull request #1029 from neo4j-labs/fix/930-rule-based-styling-with-numbers
Fix/930 rule based styling with numbers
2 parents c909b59 + 3f351e6 commit bfc8bd5

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/extensions/styling/StyleRuleEvaluator.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const evaluateRulesOnDict = (dict, rules, customizations) => {
6363
}
6464
for (const [index, rule] of rules.entries()) {
6565
// Only check customizations that are specified
66-
return evaluateSingleRuleOnDict (dict, rule, index, customizations)
66+
return evaluateSingleRuleOnDict(dict, rule, index, customizations);
6767
}
6868
// If no rules are met, return not found (index=-1)
6969
return -1;
@@ -81,8 +81,7 @@ export const evaluateSingleRuleOnDict = (dict, rule, ruleIndex, customizations)
8181
}
8282
}
8383
return -1;
84-
}
85-
84+
};
8685

8786
/**
8887
* 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
127126
return defaultValue;
128127
};
129128

129+
/**
130+
* @param realValue the value found in the real data returned by the query
131+
* @param ruleValue the value specified in the rule.
132+
* @returns whether the condition is met.
133+
*/
134+
const isLooselyEqual = (realValue, ruleValue) => {
135+
// In order to avoid having '5' <> {low: 5, high: 0} OR '5' <> 5
136+
const sensitiveTypes = ['string', 'number', 'object'];
137+
if (sensitiveTypes.includes(typeof realValue) && sensitiveTypes.includes(typeof ruleValue)) {
138+
return realValue == ruleValue;
139+
}
140+
return realValue === ruleValue;
141+
};
142+
130143
/**
131144
* @param realValue the value found in the real data returned by the query
132145
* @param condition the condition, one of [=,!=,<,<=,>,>=,contains].
@@ -139,10 +152,10 @@ const evaluateCondition = (realValue, condition, ruleValue) => {
139152
return false;
140153
}
141154
if (condition == '=') {
142-
return realValue === ruleValue;
155+
return isLooselyEqual(realValue, ruleValue);
143156
}
144157
if (condition == '!=') {
145-
return realValue !== ruleValue;
158+
return !isLooselyEqual(realValue, ruleValue);
146159
}
147160
if (!isNaN(Number(ruleValue))) {
148161
ruleValue = Number(ruleValue);

0 commit comments

Comments
 (0)