json-rules-engine
Advanced tools
Comparing version 6.3.1 to 6.4.0
@@ -226,2 +226,16 @@ 'use strict'; | ||
} | ||
/** | ||
* Interprets value as either a primitive, or if a fact, retrieves the fact value | ||
*/ | ||
}, { | ||
key: 'getValue', | ||
value: function getValue(value) { | ||
if ((0, _lodash2.default)(value) && Object.prototype.hasOwnProperty.call(value, 'fact')) { | ||
// value = { fact: 'xyz' } | ||
return this.factValue(value.fact, value.params, value.path); | ||
} | ||
return Promise.resolve(value); | ||
} | ||
}]); | ||
@@ -228,0 +242,0 @@ |
@@ -7,2 +7,4 @@ 'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -14,6 +16,2 @@ | ||
var _lodash = require('lodash.isobjectlike'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -33,4 +31,8 @@ | ||
var subConditionsIsArray = Array.isArray(subConditions); | ||
if (booleanOperator !== 'not' && !subConditionsIsArray) throw new Error('"' + booleanOperator + '" must be an array'); | ||
if (booleanOperator === 'not' && subConditionsIsArray) throw new Error('"' + booleanOperator + '" cannot be an array'); | ||
if (booleanOperator !== 'not' && !subConditionsIsArray) { | ||
throw new Error('"' + booleanOperator + '" must be an array'); | ||
} | ||
if (booleanOperator === 'not' && subConditionsIsArray) { | ||
throw new Error('"' + booleanOperator + '" cannot be an array'); | ||
} | ||
this.operator = booleanOperator; | ||
@@ -47,5 +49,11 @@ // boolean conditions always have a priority; default 1 | ||
} else if (!Object.prototype.hasOwnProperty.call(properties, 'condition')) { | ||
if (!Object.prototype.hasOwnProperty.call(properties, 'fact')) throw new Error('Condition: constructor "fact" property required'); | ||
if (!Object.prototype.hasOwnProperty.call(properties, 'operator')) throw new Error('Condition: constructor "operator" property required'); | ||
if (!Object.prototype.hasOwnProperty.call(properties, 'value')) throw new Error('Condition: constructor "value" property required'); | ||
if (!Object.prototype.hasOwnProperty.call(properties, 'fact')) { | ||
throw new Error('Condition: constructor "fact" property required'); | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(properties, 'operator')) { | ||
throw new Error('Condition: constructor "operator" property required'); | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(properties, 'value')) { | ||
throw new Error('Condition: constructor "value" property required'); | ||
} | ||
@@ -114,17 +122,2 @@ // a non-boolean condition does not have a priority by default. this allows | ||
/** | ||
* Interprets .value as either a primitive, or if a fact, retrieves the fact value | ||
*/ | ||
}, { | ||
key: '_getValue', | ||
value: function _getValue(almanac) { | ||
var value = this.value; | ||
if ((0, _lodash2.default)(value) && Object.prototype.hasOwnProperty.call(value, 'fact')) { | ||
// value: { fact: 'xyz' } | ||
return almanac.factValue(value.fact, value.params, value.path); | ||
} | ||
return Promise.resolve(value); | ||
} | ||
/** | ||
* Takes the fact result and compares it to the condition 'value', using the operator | ||
@@ -146,14 +139,24 @@ * LHS OPER RHS | ||
if (!operatorMap) return Promise.reject(new Error('operatorMap required')); | ||
if (this.isBooleanOperator()) return Promise.reject(new Error('Cannot evaluate() a boolean condition')); | ||
if (this.isBooleanOperator()) { | ||
return Promise.reject(new Error('Cannot evaluate() a boolean condition')); | ||
} | ||
var op = operatorMap.get(this.operator); | ||
if (!op) return Promise.reject(new Error('Unknown operator: ' + this.operator)); | ||
if (!op) { | ||
return Promise.reject(new Error('Unknown operator: ' + this.operator)); | ||
} | ||
return this._getValue(almanac) // todo - parallelize | ||
.then(function (rightHandSideValue) { | ||
return almanac.factValue(_this.fact, _this.params, _this.path).then(function (leftHandSideValue) { | ||
var result = op.evaluate(leftHandSideValue, rightHandSideValue); | ||
(0, _debug2.default)('condition::evaluate <' + JSON.stringify(leftHandSideValue) + ' ' + _this.operator + ' ' + JSON.stringify(rightHandSideValue) + '?> (' + result + ')'); | ||
return { result: result, leftHandSideValue: leftHandSideValue, rightHandSideValue: rightHandSideValue, operator: _this.operator }; | ||
}); | ||
return Promise.all([almanac.getValue(this.value), almanac.factValue(this.fact, this.params, this.path)]).then(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
rightHandSideValue = _ref2[0], | ||
leftHandSideValue = _ref2[1]; | ||
var result = op.evaluate(leftHandSideValue, rightHandSideValue); | ||
(0, _debug2.default)('condition::evaluate <' + JSON.stringify(leftHandSideValue) + ' ' + _this.operator + ' ' + JSON.stringify(rightHandSideValue) + '?> (' + result + ')'); | ||
return { | ||
result: result, | ||
leftHandSideValue: leftHandSideValue, | ||
rightHandSideValue: rightHandSideValue, | ||
operator: _this.operator | ||
}; | ||
}); | ||
@@ -160,0 +163,0 @@ } |
@@ -72,2 +72,3 @@ 'use strict'; | ||
_this.allowUndefinedConditions = options.allowUndefinedConditions || false; | ||
_this.replaceFactsInEventParams = options.replaceFactsInEventParams || false; | ||
_this.pathResolver = options.pathResolver; | ||
@@ -74,0 +75,0 @@ _this.operators = new Map(); |
@@ -13,2 +13,4 @@ 'use strict'; | ||
var _lodash = require('lodash'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -35,2 +37,25 @@ | ||
}, { | ||
key: 'resolveEventParams', | ||
value: function resolveEventParams(almanac) { | ||
var _this = this; | ||
if ((0, _lodash.isObject)(this.event.params)) { | ||
var updates = []; | ||
var _loop = function _loop(key) { | ||
if (Object.prototype.hasOwnProperty.call(_this.event.params, key)) { | ||
updates.push(almanac.getValue(_this.event.params[key]).then(function (val) { | ||
return _this.event.params[key] = val; | ||
})); | ||
} | ||
}; | ||
for (var key in this.event.params) { | ||
_loop(key); | ||
} | ||
return Promise.all(updates); | ||
} | ||
return Promise.resolve(); | ||
} | ||
}, { | ||
key: 'toJSON', | ||
@@ -37,0 +62,0 @@ value: function toJSON() { |
@@ -432,4 +432,10 @@ 'use strict'; | ||
ruleResult.setResult(result); | ||
var processEvent = Promise.resolve(); | ||
if (_this3.engine.replaceFactsInEventParams) { | ||
processEvent = ruleResult.resolveEventParams(almanac); | ||
} | ||
var event = result ? 'success' : 'failure'; | ||
return _this3.emitAsync(event, ruleResult.event, almanac, ruleResult).then(function () { | ||
return processEvent.then(function () { | ||
return _this3.emitAsync(event, ruleResult.event, almanac, ruleResult); | ||
}).then(function () { | ||
return ruleResult; | ||
@@ -436,0 +442,0 @@ }); |
{ | ||
"name": "json-rules-engine", | ||
"version": "6.3.1", | ||
"version": "6.4.0", | ||
"description": "Rules Engine expressed in simple json", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
85728
1605