Skip to content

Commit

Permalink
#14
Browse files Browse the repository at this point in the history
  • Loading branch information
mavarazy committed Sep 11, 2017
1 parent 69cb069 commit cf6ffe0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/conditionsMeet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ import checkField from "./checkField";
import { OR, AND, NOT } from "./constants";
import selectn from "selectn";

export function toRelCondition(refCondition, formData) {
if (Array.isArray(refCondition)) {
return refCondition.map(cond => toRelCondition(cond, formData));
} else if (isObject(refCondition)) {
return Object.keys(refCondition).reduce((agg, field) => {
agg[field] = toRelCondition(refCondition[field], formData);
return agg;
}, {});
} else if (typeof refCondition === "string" && refCondition.startsWith("$")) {
return selectn(refCondition.substr(1), formData);
} else {
return refCondition;
}
}

export default function conditionsMeet(condition, formData) {
if (!isObject(condition) || !isObject(formData)) {
toError(
Expand All @@ -21,12 +36,15 @@ export default function conditionsMeet(condition, formData) {
} else {
let refVal = selectn(ref, formData);
if (Array.isArray(refVal)) {
let condMeatOnce = refVal.some(val =>
conditionsMeet(refCondition, val)
);
return (
refVal.some(val => conditionsMeet(refCondition, val)) ||
checkField(refVal, refCondition)
condMeatOnce ||
checkField(refVal, toRelCondition(refCondition, formData))
);
} else {
return checkField(refVal, refCondition);
return checkField(refVal, toRelCondition(refCondition, formData));
}
}
});
Expand Down
38 changes: 38 additions & 0 deletions test/conditionsMeet.toRelCondition.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { toRelCondition } from "../src/conditionsMeet";

test("rel simple condition", () => {
expect(toRelCondition({ less: "$b" }, { b: 11 })).toEqual({ less: 11 });
});

test("rel complicated condition", () => {
let condition = {
decreasedByMoreThanPercent: {
average: "$averages_monthly.cost",
target: 20,
},
};

let formData = {
averages_monthly: { cost: 100 },
};

let expCondition = {
decreasedByMoreThanPercent: {
average: 100,
target: 20,
},
};

expect(toRelCondition(condition, formData)).toEqual(expCondition);
});

test("work with OR condition", () => {
let cond = { or: [{ lessEq: "$b" }, { greaterEq: "$c" }] };
let formData = { b: 16, c: 70 };
let expCond = { or: [{ lessEq: 16 }, { greaterEq: 70 }] };
expect(toRelCondition(cond, formData)).toEqual(expCond);
});

test("keep non relevant", () => {
expect(toRelCondition({ range: [20, 40] }, {})).toEqual({ range: [20, 40] });
});
37 changes: 37 additions & 0 deletions test/issues/14.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Engine from "../../src";

test("simple relevant rules work", () => {
let rules = [
{
conditions: {
a: { less: "$b" },
},
event: {
type: "match",
},
},
];
let engine = new Engine(rules);
return engine.run({ a: 10, b: 11 }).then(events => {
expect(events.length).toEqual(1);
expect(events[0]).toEqual({ type: "match" });
});
});

test("complicated rules work", () => {
let rules = [
{
conditions: {
a: { or: [{ less: "$b" }] },
},
event: {
type: "match",
},
},
];
let engine = new Engine(rules);
return engine.run({ a: 10, b: 11 }).then(events => {
expect(events.length).toEqual(1);
expect(events[0]).toEqual({ type: "match" });
});
});

0 comments on commit cf6ffe0

Please sign in to comment.