Skip to content

Commit 0f96f51

Browse files
enable providing subject attributes as string wrapper numerics to be … (#40)
* enable providing subject attributes as string wrapper numerics to be evaluated against numeric rules (FF-1661) * combine unit tests
1 parent f64b57d commit 0f96f51

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/rule_evaluator.spec.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ describe('findMatchingRule', () => {
5555
it('returns null if attributes do not match any rules', () => {
5656
const rules = [numericRule];
5757
expect(findMatchingRule({ totalSales: 101 }, rules, false)).toEqual(null);
58+
59+
// input subject attribute is a string which is not a valid semver nor numeric
60+
// verify that is not parsed to a semver nor a numeric.
61+
expect(
62+
findMatchingRule(
63+
{ version: '1.2.03' },
64+
[
65+
{
66+
allocationKey: 'test',
67+
conditions: [
68+
{
69+
operator: OperatorType.GTE,
70+
attribute: 'version',
71+
value: '1.2.0',
72+
},
73+
],
74+
},
75+
],
76+
false,
77+
),
78+
).toEqual(null);
5879
});
5980

6081
it('returns the rule if attributes match AND conditions', () => {
@@ -79,10 +100,10 @@ describe('findMatchingRule', () => {
79100
expect(findMatchingRule({ totalSales: 101 }, rules, false)).toEqual(ruleWithEmptyConditions);
80101
});
81102

82-
it('returns null if using numeric operator with string', () => {
103+
it('allows for a mix of numeric and string values', () => {
83104
const rules = [numericRule, ruleWithMatchesCondition];
84105
expect(findMatchingRule({ totalSales: 'stringValue' }, rules, false)).toEqual(null);
85-
expect(findMatchingRule({ totalSales: '20' }, rules, false)).toEqual(null);
106+
expect(findMatchingRule({ totalSales: '20' }, rules, false)).toEqual(numericRule);
86107
});
87108

88109
it('handles rule with matches operator', () => {

src/rule_evaluator.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,7 @@ function compareNumber(
153153
conditionValue: any,
154154
compareFn: (a: number, b: number) => boolean,
155155
): boolean {
156-
return (
157-
typeof attributeValue === 'number' &&
158-
typeof conditionValue === 'number' &&
159-
compareFn(attributeValue, conditionValue)
160-
);
156+
return compareFn(Number(attributeValue), Number(conditionValue));
161157
}
162158

163159
function compareSemVer(

0 commit comments

Comments
 (0)