Skip to content

Commit

Permalink
docs(core): Update readme and add generics
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbrg committed Jan 28, 2024
1 parent 2de758d commit 5df043a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ const criteria = {
};

/** Evaluate the criteria against the rule */
let result = await RulePilot.evaluate(rule, criteria);
let result = await RulePilot.evaluate<boolean>(rule, criteria);
// result == true

// However, if any of the criteria do not pass the check, the result will be false
criteria.totalCheckoutPrice = 25.0;

/** Evaluate the new criteria against the rule */
result = await RulePilot.evaluate(rule, criteria);
result = await RulePilot.evaluate<boolean>(rule, criteria);
// result == false
```

Expand Down Expand Up @@ -195,13 +195,13 @@ const criteria = {
};

/** Evaluate the criteria against the rule */
let result = await RulePilot.evaluate(rule, criteria);
let result = await RulePilot.evaluate<boolean>(rule, criteria);
// result == false

criteria.hasStudentCard = true;

/** Evaluate the new criteria against the rule */
result = await RulePilot.evaluate(rule, criteria);
result = await RulePilot.evaluate<boolean>(rule, criteria);
// result == true
```

Expand Down Expand Up @@ -438,23 +438,23 @@ const criteria = {


/** Evaluate the criteria against the rule */
let result = await RulePilot.evaluate(rule, criteria);
let result = await RulePilot.evaluate<number>(rule, criteria);
// result = 5

criteria.country = "SE";
criteria.city = "Linköping";


/** Evaluate the new criteria against the rule */
result = await RulePilot.evaluate(rule, criteria);
result = await RulePilot.evaluate<number>(rule, criteria);
// result = 10

criteria.country = "IT";
criteria.age = 17;
criteria.hasStudentCard = false;

/** Evaluate the new criteria against the rule */
result = await RulePilot.evaluate(rule, criteria);
result = await RulePilot.evaluate<number>(rule, criteria);
// result = false
```

Expand All @@ -480,7 +480,7 @@ const rule: Rule = {


/** Evaluate the criteria against the rule */
let result = await RulePilot.evaluate(rule, {});
let result = await RulePilot.evaluate<number>(rule, {});
// result = 2.5
```

Expand Down
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.2.1",
"version": "1.2.2",
"description": "Rule parsing engine for JSON rules",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
10 changes: 5 additions & 5 deletions src/services/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ export class Evaluator {
* @param rule The rule to evaluate.
* @param criteria The criteria to evaluate the rule against.
*/
evaluate(rule: Rule, criteria: object | object[]): boolean | any {
evaluate<T>(rule: Rule, criteria: object | object[]): boolean | any {
// Cater for the case where the conditions property is not an array.
const conditions =
rule.conditions instanceof Array ? rule.conditions : [rule.conditions];

if (criteria instanceof Array) {
const result = [];
for (const c of criteria) {
result.push(this.evaluateRule(conditions, c, rule?.default));
result.push(this.evaluateRule<T>(conditions, c, rule?.default));
}

return result;
}

return this.evaluateRule(conditions, criteria, rule?.default);
return this.evaluateRule<T>(conditions, criteria, rule?.default);
}

/**
Expand All @@ -36,11 +36,11 @@ export class Evaluator {
* @param criteria The criteria to evaluate the conditions against.
* @param defaultResult The default result to return if no conditions pass.
*/
private evaluateRule(
private evaluateRule<T>(
conditions: Condition[],
criteria: object,
defaultResult?: any
): boolean | any {
): T | boolean {
// We should evaluate all conditions and return the result
// of the first condition that passes.
for (const condition of conditions) {
Expand Down
9 changes: 6 additions & 3 deletions src/services/rule-pilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ export class RulePilot {
rule: Rule,
criteria: object | object[],
trustRule = false
): Promise<T> {
): Promise<T | boolean> {
// Before we evaluate the rule, we should validate it.
// If `trustRuleset` is set to true, we will skip validation.
const validationResult = !trustRule && this.validate(rule);
if (!trustRule && !validationResult.isValid) {
throw new RuleError(validationResult);
}

return this.#evaluator.evaluate(rule, await this.#mutator.mutate(criteria));
return this.#evaluator.evaluate<T>(
rule,
await this.#mutator.mutate(criteria)
);
}

/**
Expand Down Expand Up @@ -139,7 +142,7 @@ export class RulePilot {
rule: Rule,
criteria: object | object[],
trustRule = false
): Promise<T> {
): Promise<T | boolean> {
return RulePilot.#rulePilot.evaluate<T>(rule, criteria, trustRule);
}

Expand Down

0 comments on commit 5df043a

Please sign in to comment.