Skip to content

Commit cf6ffe0

Browse files
committed
1 parent 69cb069 commit cf6ffe0

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

src/conditionsMeet.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import checkField from "./checkField";
33
import { OR, AND, NOT } from "./constants";
44
import selectn from "selectn";
55

6+
export function toRelCondition(refCondition, formData) {
7+
if (Array.isArray(refCondition)) {
8+
return refCondition.map(cond => toRelCondition(cond, formData));
9+
} else if (isObject(refCondition)) {
10+
return Object.keys(refCondition).reduce((agg, field) => {
11+
agg[field] = toRelCondition(refCondition[field], formData);
12+
return agg;
13+
}, {});
14+
} else if (typeof refCondition === "string" && refCondition.startsWith("$")) {
15+
return selectn(refCondition.substr(1), formData);
16+
} else {
17+
return refCondition;
18+
}
19+
}
20+
621
export default function conditionsMeet(condition, formData) {
722
if (!isObject(condition) || !isObject(formData)) {
823
toError(
@@ -21,12 +36,15 @@ export default function conditionsMeet(condition, formData) {
2136
} else {
2237
let refVal = selectn(ref, formData);
2338
if (Array.isArray(refVal)) {
39+
let condMeatOnce = refVal.some(val =>
40+
conditionsMeet(refCondition, val)
41+
);
2442
return (
25-
refVal.some(val => conditionsMeet(refCondition, val)) ||
26-
checkField(refVal, refCondition)
43+
condMeatOnce ||
44+
checkField(refVal, toRelCondition(refCondition, formData))
2745
);
2846
} else {
29-
return checkField(refVal, refCondition);
47+
return checkField(refVal, toRelCondition(refCondition, formData));
3048
}
3149
}
3250
});
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { toRelCondition } from "../src/conditionsMeet";
2+
3+
test("rel simple condition", () => {
4+
expect(toRelCondition({ less: "$b" }, { b: 11 })).toEqual({ less: 11 });
5+
});
6+
7+
test("rel complicated condition", () => {
8+
let condition = {
9+
decreasedByMoreThanPercent: {
10+
average: "$averages_monthly.cost",
11+
target: 20,
12+
},
13+
};
14+
15+
let formData = {
16+
averages_monthly: { cost: 100 },
17+
};
18+
19+
let expCondition = {
20+
decreasedByMoreThanPercent: {
21+
average: 100,
22+
target: 20,
23+
},
24+
};
25+
26+
expect(toRelCondition(condition, formData)).toEqual(expCondition);
27+
});
28+
29+
test("work with OR condition", () => {
30+
let cond = { or: [{ lessEq: "$b" }, { greaterEq: "$c" }] };
31+
let formData = { b: 16, c: 70 };
32+
let expCond = { or: [{ lessEq: 16 }, { greaterEq: 70 }] };
33+
expect(toRelCondition(cond, formData)).toEqual(expCond);
34+
});
35+
36+
test("keep non relevant", () => {
37+
expect(toRelCondition({ range: [20, 40] }, {})).toEqual({ range: [20, 40] });
38+
});

test/issues/14.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Engine from "../../src";
2+
3+
test("simple relevant rules work", () => {
4+
let rules = [
5+
{
6+
conditions: {
7+
a: { less: "$b" },
8+
},
9+
event: {
10+
type: "match",
11+
},
12+
},
13+
];
14+
let engine = new Engine(rules);
15+
return engine.run({ a: 10, b: 11 }).then(events => {
16+
expect(events.length).toEqual(1);
17+
expect(events[0]).toEqual({ type: "match" });
18+
});
19+
});
20+
21+
test("complicated rules work", () => {
22+
let rules = [
23+
{
24+
conditions: {
25+
a: { or: [{ less: "$b" }] },
26+
},
27+
event: {
28+
type: "match",
29+
},
30+
},
31+
];
32+
let engine = new Engine(rules);
33+
return engine.run({ a: 10, b: 11 }).then(events => {
34+
expect(events.length).toEqual(1);
35+
expect(events[0]).toEqual({ type: "match" });
36+
});
37+
});

0 commit comments

Comments
 (0)