Skip to content

Commit ac38086

Browse files
author
Cache Hamm
committed
/dist generation for 1.5.0
1 parent 32f8147 commit ac38086

3 files changed

Lines changed: 162 additions & 67 deletions

File tree

dist/almanac.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
2222

2323
var debug = require('debug')('json-rules-engine');
2424
var verbose = require('debug')('json-rules-engine-verbose');
25+
var selectn = require('selectn');
26+
var isPlainObject = require('lodash.isplainobject');
27+
var warn = require('debug')('json-rules-engine:warn');
2528

2629
/**
2730
* Fact results lookup
@@ -118,6 +121,7 @@ var Almanac = function () {
118121
* by the engine, which cache's fact computations based on parameters provided
119122
* @param {string} factId - fact identifier
120123
* @param {Object} params - parameters to feed into the fact. By default, these will also be used to compute the cache key
124+
* @param {String} path - object
121125
* @return {Promise} a promise which will resolve with the fact computation.
122126
*/
123127

@@ -126,7 +130,8 @@ var Almanac = function () {
126130
value: function () {
127131
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(factId) {
128132
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
129-
var fact, cacheKey, cacheVal;
133+
var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
134+
var fact, cacheKey, cacheVal, factValue;
130135
return regeneratorRuntime.wrap(function _callee$(_context) {
131136
while (1) {
132137
switch (_context.prev = _context.next) {
@@ -147,9 +152,23 @@ var Almanac = function () {
147152

148153
case 6:
149154
verbose('almanac::factValue cache miss for fact:' + factId + '; calculating');
150-
return _context.abrupt('return', this._setFactValue(fact, params, fact.calculate(params, this)));
155+
_context.next = 9;
156+
return this._setFactValue(fact, params, fact.calculate(params, this));
157+
158+
case 9:
159+
factValue = _context.sent;
160+
161+
if (path) {
162+
if (isPlainObject(factValue) || Array.isArray(factValue)) {
163+
factValue = selectn(path)(factValue);
164+
debug('condition::evaluate extracting object property ' + path + ', received: ' + factValue);
165+
} else {
166+
warn('condition::evaluate could not compute object path(' + path + ') of non-object: ' + factValue + ' <' + (typeof factValue === 'undefined' ? 'undefined' : _typeof(factValue)) + '>; continuing with ' + factValue);
167+
}
168+
}
169+
return _context.abrupt('return', factValue);
151170

152-
case 8:
171+
case 12:
153172
case 'end':
154173
return _context.stop();
155174
}

dist/condition.js

Lines changed: 120 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,15 @@ Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
66

7-
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8-
97
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; }; }();
108

11-
var _params = require('params');
12-
13-
var _params2 = _interopRequireDefault(_params);
14-
15-
var _selectn = require('selectn');
16-
17-
var _selectn2 = _interopRequireDefault(_selectn);
18-
19-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9+
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
2010

2111
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2212

13+
var params = require('params');
2314
var debug = require('debug')('json-rules-engine');
24-
var warn = require('debug')('json-rules-engine:warn');
15+
var isPlainObject = require('lodash.isplainobject');
2516

2617
var Condition = function () {
2718
function Condition(properties) {
@@ -41,7 +32,7 @@ var Condition = function () {
4132
return new Condition(c);
4233
});
4334
} else {
44-
properties = (0, _params2.default)(properties).require(['fact', 'operator', 'value']);
35+
properties = params(properties).require(['fact', 'operator', 'value']);
4536
// a non-boolean condition does not have a priority by default. this allows
4637
// priority to be dictated by the fact definition
4738
if (properties.hasOwnProperty('priority')) {
@@ -88,38 +79,132 @@ var Condition = function () {
8879
return props;
8980
}
9081

82+
/**
83+
* Interprets .value as either a primitive, or if a fact, retrieves the fact value
84+
*/
85+
86+
}, {
87+
key: '_getValue',
88+
value: function () {
89+
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(almanac) {
90+
var value;
91+
return regeneratorRuntime.wrap(function _callee$(_context) {
92+
while (1) {
93+
switch (_context.prev = _context.next) {
94+
case 0:
95+
value = this.value;
96+
97+
if (!(isPlainObject(value) && value.hasOwnProperty('fact'))) {
98+
_context.next = 5;
99+
break;
100+
}
101+
102+
_context.next = 4;
103+
return almanac.factValue(value.fact, value.params, value.path);
104+
105+
case 4:
106+
value = _context.sent;
107+
108+
case 5:
109+
return _context.abrupt('return', value);
110+
111+
case 6:
112+
case 'end':
113+
return _context.stop();
114+
}
115+
}
116+
}, _callee, this);
117+
}));
118+
119+
function _getValue(_x2) {
120+
return _ref.apply(this, arguments);
121+
}
122+
123+
return _getValue;
124+
}()
125+
91126
/**
92127
* Takes the fact result and compares it to the condition 'value', using the operator
93-
* @param {mixed} comparisonValue - fact result
128+
* LHS OPER RHS
129+
* <fact + params + path> <operator> <value>
130+
*
131+
* @param {Almanac} almanac
94132
* @param {Map} operatorMap - map of available operators, keyed by operator name
95133
* @returns {Boolean} - evaluation result
96134
*/
97135

98136
}, {
99137
key: 'evaluate',
100-
value: function evaluate(comparisonValue, operatorMap) {
101-
// for any/all, simply comparisonValue that the sub-condition array evaluated truthy
102-
if (this.isBooleanOperator()) return comparisonValue === true;
103-
104-
// if the fact has provided an object, and a path is specified, retrieve the object property
105-
if (this.path) {
106-
if ((typeof comparisonValue === 'undefined' ? 'undefined' : _typeof(comparisonValue)) === 'object') {
107-
comparisonValue = (0, _selectn2.default)(this.path)(comparisonValue);
108-
debug('condition::evaluate extracting object property ' + this.path + ', received: ' + comparisonValue);
109-
} else {
110-
warn('condition::evaluate could not compute object path(' + this.path + ') of non-object: ' + comparisonValue + ' <' + (typeof comparisonValue === 'undefined' ? 'undefined' : _typeof(comparisonValue)) + '>; continuing with ' + comparisonValue);
111-
}
138+
value: function () {
139+
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(almanac, operatorMap) {
140+
var op, rightHandSideValue, leftHandSideValue, evaluationResult;
141+
return regeneratorRuntime.wrap(function _callee2$(_context2) {
142+
while (1) {
143+
switch (_context2.prev = _context2.next) {
144+
case 0:
145+
if (almanac) {
146+
_context2.next = 2;
147+
break;
148+
}
149+
150+
throw new Error('almanac required');
151+
152+
case 2:
153+
if (operatorMap) {
154+
_context2.next = 4;
155+
break;
156+
}
157+
158+
throw new Error('operatorMap required');
159+
160+
case 4:
161+
if (!this.isBooleanOperator()) {
162+
_context2.next = 6;
163+
break;
164+
}
165+
166+
throw new Error('Cannot evaluate() a boolean condition');
167+
168+
case 6:
169+
op = operatorMap.get(this.operator);
170+
171+
if (op) {
172+
_context2.next = 9;
173+
break;
174+
}
175+
176+
throw new Error('Unknown operator: ' + this.operator);
177+
178+
case 9:
179+
_context2.next = 11;
180+
return this._getValue(almanac);
181+
182+
case 11:
183+
rightHandSideValue = _context2.sent;
184+
_context2.next = 14;
185+
return almanac.factValue(this.fact, this.params, this.path);
186+
187+
case 14:
188+
leftHandSideValue = _context2.sent;
189+
evaluationResult = op.evaluate(leftHandSideValue, rightHandSideValue);
190+
191+
debug('condition::evaluate <' + leftHandSideValue + ' ' + this.operator + ' ' + rightHandSideValue + '?> (' + evaluationResult + ')');
192+
return _context2.abrupt('return', evaluationResult);
193+
194+
case 18:
195+
case 'end':
196+
return _context2.stop();
197+
}
198+
}
199+
}, _callee2, this);
200+
}));
201+
202+
function evaluate(_x3, _x4) {
203+
return _ref2.apply(this, arguments);
112204
}
113205

114-
var op = operatorMap.get(this.operator);
115-
if (!op) throw new Error('Unknown operator: ' + this.operator);
116-
117-
var evaluationResult = op.evaluate(comparisonValue, this.value);
118-
if (!this.isBooleanOperator()) {
119-
debug('condition::evaluate <' + comparisonValue + ' ' + this.operator + ' ' + this.value + '?> (' + evaluationResult + ')');
120-
}
121-
return evaluationResult;
122-
}
206+
return evaluate;
207+
}()
123208

124209
/**
125210
* Returns the boolean operator for the condition

dist/rule.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ var Rule = function (_EventEmitter) {
205205
passes = void 0;
206206

207207
if (!condition.isBooleanOperator()) {
208-
_context.next = 15;
208+
_context.next = 16;
209209
break;
210210
}
211211

@@ -232,61 +232,52 @@ var Rule = function (_EventEmitter) {
232232
comparisonValue = _context.sent;
233233

234234
case 13:
235-
_context.next = 28;
235+
// for booleans, rule passing is determined by the all/any result
236+
passes = comparisonValue === true;
237+
_context.next = 29;
236238
break;
237239

238-
case 15:
239-
_context.prev = 15;
240-
_context.next = 18;
241-
return almanac.factValue(condition.fact, condition.params);
240+
case 16:
241+
_context.prev = 16;
242+
_context.next = 19;
243+
return condition.evaluate(almanac, _this3.engine.operators, comparisonValue);
242244

243-
case 18:
244-
comparisonValue = _context.sent;
245-
_context.next = 28;
245+
case 19:
246+
passes = _context.sent;
247+
_context.next = 29;
246248
break;
247249

248-
case 21:
249-
_context.prev = 21;
250-
_context.t0 = _context['catch'](15);
250+
case 22:
251+
_context.prev = 22;
252+
_context.t0 = _context['catch'](16);
251253

252254
if (!(_this3.engine.allowUndefinedFacts && _context.t0.code === 'UNDEFINED_FACT')) {
253-
_context.next = 27;
255+
_context.next = 28;
254256
break;
255257
}
256258

257259
passes = false;
258-
_context.next = 28;
260+
_context.next = 29;
259261
break;
260262

261-
case 27:
262-
throw _context.t0;
263-
264263
case 28:
265-
if (!(passes === undefined)) {
266-
_context.next = 32;
267-
break;
268-
}
264+
throw _context.t0;
269265

270-
_context.next = 31;
271-
return condition.evaluate(comparisonValue, _this3.engine.operators);
266+
case 29:
272267

273-
case 31:
274-
passes = _context.sent;
275-
276-
case 32:
277268
if (passes) {
278269
_this3.emit('success', _this3.event, almanac);
279270
} else {
280271
_this3.emit('failure', _this3.event, almanac);
281272
}
282273
return _context.abrupt('return', passes);
283274

284-
case 34:
275+
case 31:
285276
case 'end':
286277
return _context.stop();
287278
}
288279
}
289-
}, _callee, _this3, [[15, 21]]);
280+
}, _callee, _this3, [[16, 22]]);
290281
}));
291282

292283
return function evaluateCondition(_x3) {

0 commit comments

Comments
 (0)