Skip to content

Commit

Permalink
Add typing for rule results
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbrg committed May 31, 2023
1 parent d0b003c commit aea5f9f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rulepilot",
"version": "1.1.9",
"version": "1.1.10",
"description": "Rule parsing engine for JSON rules",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
26 changes: 8 additions & 18 deletions src/types/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,14 @@ export interface Constraint {
| (string | number | boolean | object)[];
}

export interface Condition {
any?: (Constraint | Condition)[];
all?: (Constraint | Condition)[];
none?: (Constraint | Condition)[];
result?:
| string
| number
| boolean
| object
| (string | number | boolean | object)[];
export interface Condition<R = any> {
any?: (Constraint | Condition<R>)[];
all?: (Constraint | Condition<R>)[];
none?: (Constraint | Condition<R>)[];
result?: R;
}

export interface Rule {
conditions: Condition | Condition[];
default?:
| string
| number
| boolean
| object
| (string | number | boolean | object)[];
export interface Rule<R = any> {
conditions: Condition<R> | Condition<R>[];
default?: R;
}
27 changes: 24 additions & 3 deletions test/mutator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { RulePilot } from "../src";
import { valid1Json } from "./rulesets/valid1.json";
import { valid3Json } from "./rulesets/valid3.json";

let rp: RulePilot;

const mutation1 = async (value) => {
const result = await axios.get(
`https://restcountries.com/v3.1/name/${value}?fullText=true`
Expand Down Expand Up @@ -41,19 +39,22 @@ const criteria = [

describe("RulePilot mutator correctly", () => {
beforeEach(() => {
rp = new RulePilot();
console.debug = jest.fn();
process.env.DEBUG = "true";
});

it("Performs desired mutation", async () => {
const rp = new RulePilot();

rp.addMutation("ProfitPercentage", (value) => value * 2);
expect(await rp.evaluate(valid1Json, { ProfitPercentage: 5 })).toEqual(
true
);
});

it("Performs multiple mutations", async () => {
const rp = new RulePilot();

rp.addMutation("WinRate", (value) => value * 2);
rp.addMutation("AverageTradeDuration", (value) => value / 2);

Expand All @@ -68,6 +69,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Performs async mutation", async () => {
const rp = new RulePilot();

rp.addMutation("CountryIso", mutation1);

expect(
Expand All @@ -83,6 +86,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Performs nested mutation", async () => {
const rp = new RulePilot();

rp.addMutation("foo.bar", (value) => value * 2);
expect(
await rp.evaluate(
Expand All @@ -99,6 +104,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Caches async mutation results", async () => {
const rp = new RulePilot();

rp.addMutation("Leverage", (value) => value);
rp.addMutation("CountryIso", mutation1);

Expand All @@ -112,6 +119,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Performs a migration with an array parameter", async () => {
const rp = new RulePilot();

rp.addMutation("CountryIso", mutation2);

const result = await rp.evaluate(
Expand All @@ -129,6 +138,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Performs a migration with a nested array parameter", async () => {
const rp = new RulePilot();

rp.addMutation("foo.bar", mutation2);

const result = await rp.evaluate(
Expand All @@ -146,6 +157,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Mutation cache works properly", async () => {
const rp = new RulePilot();

rp.addMutation("Leverage", (value) => value);
rp.addMutation("CountryIso", mutation1);

Expand All @@ -161,6 +174,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Removes a mutation properly", async () => {
const rp = new RulePilot();

rp.addMutation("CountryIso", mutation1);
rp.removeMutation("CountryIso");

Expand All @@ -175,6 +190,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Clears mutation cache properly", async () => {
const rp = new RulePilot();

rp.addMutation("CountryIso", mutation1);

await rp.evaluate(valid3Json, {
Expand All @@ -198,6 +215,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Clears all mutation cache properly", async () => {
const rp = new RulePilot();

rp.addMutation("CountryIso", mutation1);

await rp.evaluate(valid3Json, {
Expand All @@ -221,6 +240,8 @@ describe("RulePilot mutator correctly", () => {
});

it("Static methods behave as expected", async () => {
const rp = new RulePilot();

RulePilot.addMutation("CountryIso", mutation1);

const result1 = await RulePilot.evaluate(valid3Json, {
Expand Down

0 comments on commit aea5f9f

Please sign in to comment.