UNPKG

@ivandt/json-rules

Version:

Rule parsing engine for JSON rules

89 lines (88 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectDiscovery = void 0; const rule_helper_1 = require("./rule-helper"); class ObjectDiscovery { /** * Returns the type of condition passed to the function. * @param condition The condition to check. */ conditionType(condition) { if (!this.isObject(condition)) return null; if ("any" in condition) return "any"; if ("all" in condition) return "all"; if ("none" in condition) return "none"; return null; } /** * Checks the rule to see if it is granular. * @param rule The rule to check. */ isGranular(rule) { const helper = new rule_helper_1.RuleHelper(); const conditions = rule.conditions instanceof Array ? rule.conditions : [rule.conditions]; // Checks each condition making sure it has a result property. for (const condition of conditions) { if (this.isConditionWithResult(condition)) { return false; } // In such cases, we must check for sub-rules in the condition. const items = helper.extractSubRules(condition); if (!items.length) { continue; } // Check if any sub-rule has a result property. const result = items.reduce((prev, curr) => { return prev || this.isConditionWithResult(curr.subRule); }, false); if (result) return false; } return true; } /** * Checks an object to see if it is a valid condition. * @param obj The object to check. */ isCondition(obj) { if (!this.isObject(obj)) return false; return "any" in obj || "all" in obj || "none" in obj; } /** * Checks an object to see if it is a valid sub-rule. * @param obj The object to check. */ isConditionWithResult(obj) { return this.isCondition(obj) && "result" in obj; } /** * Checks an object to see if it is a valid constraint. * @param obj The object to check. */ isConstraint(obj) { if (!this.isObject(obj)) return false; return "field" in obj && "operator" in obj; } /** * Returns true if the passed parameter is an object. * @param obj The item to test. */ isObject(obj) { return "object" === typeof obj && !Array.isArray(obj) && obj !== null; } /** * Resolves a nested property from a sting as an object path. * @param path The path to resolve. * @param obj The object to resolve the path against. */ resolveNestedProperty(path, obj) { return path.split(".").reduce((prev, curr) => prev === null || prev === void 0 ? void 0 : prev[curr], obj); } } exports.ObjectDiscovery = ObjectDiscovery;