rulepilot
Version:
Rule parsing engine for JSON rules
226 lines (225 loc) • 10.1 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _a, _RulePilot_rulePilot, _RulePilot_mutator, _RulePilot_validator, _RulePilot_evaluator, _RulePilot_introspector;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RulePilot = void 0;
const mutator_1 = require("./mutator");
const builder_1 = require("../builder");
const errors_1 = require("../errors");
const evaluator_1 = require("./evaluator");
const introspector_1 = require("./introspector");
const validator_1 = require("./validator");
class RulePilot {
constructor() {
_RulePilot_mutator.set(this, new mutator_1.Mutator());
_RulePilot_validator.set(this, new validator_1.Validator());
_RulePilot_evaluator.set(this, new evaluator_1.Evaluator());
_RulePilot_introspector.set(this, new introspector_1.Introspector());
}
/**
* Returns a rule builder class instance.
* Allows for the construction of rules using a fluent interface.
*/
builder() {
return new builder_1.Builder(__classPrivateFieldGet(this, _RulePilot_validator, "f"));
}
/**
* Adds a mutation to the rule pilot instance.
* Mutations allow for the modification of the criteria before
* it is evaluated against a rule.
*
* @param name The name of the mutation.
* @param mutation The mutation function.
*/
addMutation(name, mutation) {
__classPrivateFieldGet(this, _RulePilot_mutator, "f").add(name, mutation);
return this;
}
/**
* Removes a mutation to the rule pilot instance.
* Any cached mutation values for this mutation will be purged.
*
* @param name The name of the mutation.
*/
removeMutation(name) {
__classPrivateFieldGet(this, _RulePilot_mutator, "f").remove(name);
return this;
}
/**
* Clears the mutator cache.
* The entire cache, or cache for a specific mutator, can be cleared
* by passing or omitting the mutator name as an argument.
*
* @param name The mutator name to clear the cache for.
*/
clearMutationCache(name) {
__classPrivateFieldGet(this, _RulePilot_mutator, "f").clearCache(name);
return this;
}
/**
* Evaluates a rule against a set of criteria and returns the result.
* If the criteria is an array (indicating multiple criteria to test),
* the rule will be evaluated against each item in the array and
* an array of results will be returned.
*
* @param rule The rule to evaluate.
* @param criteria The criteria to evaluate the rule against.
* @param trustRule Set true to avoid validating the rule before evaluating it (faster).
* @throws RuleError if the rule is invalid.
*/
evaluate(rule_1, criteria_1) {
return __awaiter(this, arguments, void 0, function* (rule, criteria, trustRule = false) {
// 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 errors_1.RuleError(validationResult);
}
return __classPrivateFieldGet(this, _RulePilot_evaluator, "f").evaluate(rule, yield __classPrivateFieldGet(this, _RulePilot_mutator, "f").mutate(criteria));
});
}
/**
* Given a rule, checks the constraints and conditions to determine
* the possible range of input criteria which would be satisfied by the rule.
*
* @param rule The rule to evaluate.
* @param criteria The criteria to introspect against.
* @param subjects The subjects to introspect for.
* @throws RuleError if the rule is invalid
* @throws RuleTypeError if the rule is not granular
*/
introspect(rule, criteria, subjects) {
// Before we proceed with the rule, we should validate it.
const validationResult = this.validate(rule);
if (!validationResult.isValid) {
throw new errors_1.RuleError(validationResult);
}
return __classPrivateFieldGet(this, _RulePilot_introspector, "f").introspect(rule, Array.isArray(criteria) ? criteria : [criteria], subjects);
}
/**
* Returns the number of outcomes that a rule has.
* @param rule The rule to check.
*/
numOutcomes(rule) {
// Before we proceed with the rule, we should validate it.
const validationResult = this.validate(rule);
if (!validationResult.isValid) {
throw new errors_1.RuleError(validationResult);
}
return __classPrivateFieldGet(this, _RulePilot_introspector, "f").numOutcomes(rule);
}
/**
* Takes in a rule as a parameter and returns a ValidationResult
* indicating whether the rule is valid or not.
*
* Invalid rules will contain an error property which contains a message and the element
* that caused the validation to fail.
*
* @param rule The rule to validate.
*/
validate(rule) {
return __classPrivateFieldGet(this, _RulePilot_validator, "f").validate(rule);
}
/**
* Returns a rule builder class instance.
* Allows for the construction of rules using a fluent interface.
*/
static builder() {
return __classPrivateFieldGet(this, _a, "f", _RulePilot_rulePilot).builder();
}
/**
* Evaluates a rule against a set of criteria and returns the result.
* If the criteria is an array (indicating multiple criteria to test),
* the rule will be evaluated against each item in the array and
* an array of results will be returned.
*
* @param rule The rule to evaluate.
* @param criteria The criteria to evaluate the rule against.
* @param trustRule Set true to avoid validating the rule before evaluating it (faster).
* @throws RuleError if the rule is invalid.
*/
static evaluate(rule_1, criteria_1) {
return __awaiter(this, arguments, void 0, function* (rule, criteria, trustRule = false) {
return __classPrivateFieldGet(_a, _a, "f", _RulePilot_rulePilot).evaluate(rule, criteria, trustRule);
});
}
/**
* Given a rule, checks the constraints and conditions to determine
* the possible range of input criteria which would be satisfied by the rule.
*
* @param rule The rule to introspect.
* @param criteria The criteria to introspect against.
* @param subjects The subjects to introspect for.
* @throws RuleError if the rule is invalid
* @throws RuleTypeError if the rule is not granular
*/
static introspect(rule, criteria, subjects) {
return __classPrivateFieldGet(_a, _a, "f", _RulePilot_rulePilot).introspect(rule, Array.isArray(criteria) ? criteria : [criteria], subjects);
}
/**
* Returns the number of outcomes that a rule has.
* @param rule The rule to check.
*/
static numOutcomes(rule) {
return __classPrivateFieldGet(_a, _a, "f", _RulePilot_rulePilot).numOutcomes(rule);
}
/**
* Takes in a rule as a parameter and returns a ValidationResult
* indicating whether the rule is valid or not.
*
* Invalid rules will contain an error property which contains a message and the element
* that caused the validation to fail.
*
* @param rule The rule to validate.
*/
static validate(rule) {
return __classPrivateFieldGet(_a, _a, "f", _RulePilot_rulePilot).validate(rule);
}
/**
* Adds a mutation.
*
* Mutations allow for the modification of the criteria before
* it is evaluated against a rule.
*
* @param name The name of the mutation.
* @param mutation The mutation function.
*/
static addMutation(name, mutation) {
return __classPrivateFieldGet(_a, _a, "f", _RulePilot_rulePilot).addMutation(name, mutation);
}
/**
* Removes a mutation to the rule pilot instance.
* Any cached mutation values for this mutation will be purged.
*
* @param name The name of the mutation.
*/
static removeMutation(name) {
return __classPrivateFieldGet(_a, _a, "f", _RulePilot_rulePilot).removeMutation(name);
}
/**
* Clears the mutator cache.
* The entire cache, or cache for a specific mutator, can be cleared
* by passing or omitting the mutator name as an argument.
*
* @param name The mutator name to clear the cache for.
*/
static clearMutationCache(name) {
return __classPrivateFieldGet(_a, _a, "f", _RulePilot_rulePilot).clearMutationCache(name);
}
}
exports.RulePilot = RulePilot;
_a = RulePilot, _RulePilot_mutator = new WeakMap(), _RulePilot_validator = new WeakMap(), _RulePilot_evaluator = new WeakMap(), _RulePilot_introspector = new WeakMap();
_RulePilot_rulePilot = { value: new _a() };