Skip to content

Commit ca4b637

Browse files
author
Cache Hamm
committed
remove "params" package
1 parent fc9944e commit ca4b637

10 files changed

Lines changed: 100 additions & 55 deletions

File tree

dist/condition.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ var Condition = function () {
137137
key: 'evaluate',
138138
value: function () {
139139
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(almanac, operatorMap) {
140-
var op, rightHandSideValue, leftHandSideValue, evaluationResult;
140+
var op, rightHandSideValue, leftHandSideValue, result;
141141
return regeneratorRuntime.wrap(function _callee2$(_context2) {
142142
while (1) {
143143
switch (_context2.prev = _context2.next) {
@@ -186,10 +186,10 @@ var Condition = function () {
186186

187187
case 14:
188188
leftHandSideValue = _context2.sent;
189-
evaluationResult = op.evaluate(leftHandSideValue, rightHandSideValue);
189+
result = op.evaluate(leftHandSideValue, rightHandSideValue);
190190

191-
debug('condition::evaluate <' + leftHandSideValue + ' ' + this.operator + ' ' + rightHandSideValue + '?> (' + evaluationResult + ')');
192-
return _context2.abrupt('return', evaluationResult);
191+
debug('condition::evaluate <' + leftHandSideValue + ' ' + this.operator + ' ' + rightHandSideValue + '?> (' + result + ')');
192+
return _context2.abrupt('return', { result: result, leftHandSideValue: leftHandSideValue, rightHandSideValue: rightHandSideValue, operator: this.operator });
193193

194194
case 18:
195195
case 'end':

dist/engine.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ var Engine = function (_EventEmitter) {
224224
debug('engine::run status:' + _this2.status + '; skipping remaining rules');
225225
return;
226226
}
227-
return rule.evaluate(almanac).then(function (rulePasses) {
228-
debug('engine::run ruleResult:' + rulePasses);
229-
if (rulePasses) {
230-
_this2.emit('success', rule.event, almanac);
231-
_this2.emit(rule.event.type, rule.event.params, _this2);
227+
return rule.evaluate(almanac).then(function (ruleResult) {
228+
debug('engine::run ruleResult:' + ruleResult.result);
229+
if (ruleResult.result) {
230+
_this2.emit('success', rule.event, almanac, ruleResult);
232231
almanac.factValue('success-events', { event: rule.event });
232+
} else {
233+
_this2.emit('failure', rule.event, almanac, ruleResult);
233234
}
234-
if (!rulePasses) _this2.emit('failure', rule, almanac);
235235
});
236236
})));
237237

dist/rule.js

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ var _condition2 = _interopRequireDefault(_condition);
1616

1717
var _events = require('events');
1818

19+
var _lodash = require('lodash.clonedeep');
20+
21+
var _lodash2 = _interopRequireDefault(_lodash);
22+
1923
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2024

2125
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"); }); }; }
@@ -175,7 +179,7 @@ var Rule = function (_EventEmitter) {
175179
/**
176180
* Evaluates the rule, starting with the root boolean operator and recursing down
177181
* All evaluation is done within the context of an almanac
178-
* @return {Promise(boolean)} rule evaluation result
182+
* @return {Promise(RuleResult)} rule evaluation result
179183
*/
180184

181185
}, {
@@ -184,19 +188,27 @@ var Rule = function (_EventEmitter) {
184188
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee6(almanac) {
185189
var _this3 = this;
186190

187-
var evaluateCondition, evaluateConditions, prioritizeAndRun, any, all;
191+
var ruleResult, evaluateCondition, evaluateConditions, prioritizeAndRun, any, all, processResult, result, _result;
192+
188193
return regeneratorRuntime.wrap(function _callee6$(_context6) {
189194
while (1) {
190195
switch (_context6.prev = _context6.next) {
191196
case 0:
197+
ruleResult = {
198+
conditions: (0, _lodash2.default)(this.conditions),
199+
event: (0, _lodash2.default)(this.event),
200+
priority: (0, _lodash2.default)(this.priority)
201+
};
202+
192203
/**
193204
* Evaluates the rule conditions
194205
* @param {Condition} condition - condition to evaluate
195206
* @return {Promise(true|false)} - resolves with the result of the condition evaluation
196207
*/
208+
197209
evaluateCondition = function () {
198210
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee(condition) {
199-
var comparisonValue, passes, subConditions;
211+
var comparisonValue, passes, subConditions, evaluationResult;
200212
return regeneratorRuntime.wrap(function _callee$(_context) {
201213
while (1) {
202214
switch (_context.prev = _context.next) {
@@ -234,7 +246,7 @@ var Rule = function (_EventEmitter) {
234246
case 13:
235247
// for booleans, rule passing is determined by the all/any result
236248
passes = comparisonValue === true;
237-
_context.next = 29;
249+
_context.next = 31;
238250
break;
239251

240252
case 16:
@@ -243,41 +255,39 @@ var Rule = function (_EventEmitter) {
243255
return condition.evaluate(almanac, _this3.engine.operators, comparisonValue);
244256

245257
case 19:
246-
passes = _context.sent;
247-
_context.next = 29;
258+
evaluationResult = _context.sent;
259+
260+
passes = evaluationResult.result;
261+
condition.factResult = evaluationResult.leftHandSideValue;
262+
_context.next = 31;
248263
break;
249264

250-
case 22:
251-
_context.prev = 22;
265+
case 24:
266+
_context.prev = 24;
252267
_context.t0 = _context['catch'](16);
253268

254269
if (!(_this3.engine.allowUndefinedFacts && _context.t0.code === 'UNDEFINED_FACT')) {
255-
_context.next = 28;
270+
_context.next = 30;
256271
break;
257272
}
258273

259274
passes = false;
260-
_context.next = 29;
275+
_context.next = 31;
261276
break;
262277

263-
case 28:
278+
case 30:
264279
throw _context.t0;
265280

266-
case 29:
267-
268-
if (passes) {
269-
_this3.emit('success', _this3.event, almanac);
270-
} else {
271-
_this3.emit('failure', _this3.event, almanac);
272-
}
281+
case 31:
282+
condition.result = passes;
273283
return _context.abrupt('return', passes);
274284

275-
case 31:
285+
case 33:
276286
case 'end':
277287
return _context.stop();
278288
}
279289
}
280-
}, _callee, _this3, [[16, 22]]);
290+
}, _callee, _this3, [[16, 24]]);
281291
}));
282292

283293
return function evaluateCondition(_x3) {
@@ -453,25 +463,39 @@ var Rule = function (_EventEmitter) {
453463
};
454464
}();
455465

456-
if (!this.conditions.any) {
457-
_context6.next = 11;
466+
/**
467+
* Emits based on rule evaluation result, and decorates ruleResult with 'result' property
468+
* @param {Boolean} result
469+
*/
470+
471+
472+
processResult = function processResult(result) {
473+
ruleResult.result = result;
474+
if (result) _this3.emit('success', ruleResult.event, almanac, ruleResult);else _this3.emit('failure', ruleResult.event, almanac, ruleResult);
475+
return ruleResult;
476+
};
477+
478+
if (!ruleResult.conditions.any) {
479+
_context6.next = 14;
458480
break;
459481
}
460482

461-
_context6.next = 8;
462-
return any(this.conditions.any);
483+
_context6.next = 10;
484+
return any(ruleResult.conditions.any);
463485

464-
case 8:
465-
return _context6.abrupt('return', _context6.sent);
486+
case 10:
487+
result = _context6.sent;
488+
return _context6.abrupt('return', processResult(result));
466489

467-
case 11:
468-
_context6.next = 13;
469-
return all(this.conditions.all);
490+
case 14:
491+
_context6.next = 16;
492+
return all(ruleResult.conditions.all);
470493

471-
case 13:
472-
return _context6.abrupt('return', _context6.sent);
494+
case 16:
495+
_result = _context6.sent;
496+
return _context6.abrupt('return', processResult(_result));
473497

474-
case 14:
498+
case 18:
475499
case 'end':
476500
return _context6.stop();
477501
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"lodash.clonedeep": "4.5.0",
7070
"lodash.isplainobject": "4.0.6",
7171
"object-hash": "1.1.5",
72-
"params": "0.1.1",
7372
"selectn": "1.1.1"
7473
}
7574
}

src/condition.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict'
22

3-
let params = require('params')
43
let debug = require('debug')('json-rules-engine')
54
let isPlainObject = require('lodash.isplainobject')
65

76
export default class Condition {
87
constructor (properties) {
8+
if (!properties) throw new Error('Condition: constructor options required')
99
let booleanOperator = Condition.booleanOperator(properties)
1010
Object.assign(this, properties)
1111
if (booleanOperator) {
@@ -20,7 +20,10 @@ export default class Condition {
2020
return new Condition(c)
2121
})
2222
} else {
23-
properties = params(properties).require(['fact', 'operator', 'value'])
23+
if (!properties.hasOwnProperty('fact')) throw new Error('Condition: constructor "fact" property required')
24+
if (!properties.hasOwnProperty('operator')) throw new Error('Condition: constructor "operator" property required')
25+
if (!properties.hasOwnProperty('value')) throw new Error('Condition: constructor "value" property required')
26+
2427
// a non-boolean condition does not have a priority by default. this allows
2528
// priority to be dictated by the fact definition
2629
if (properties.hasOwnProperty('priority')) {

src/engine.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
import params from 'params'
43
import Fact from './fact'
54
import Rule from './rule'
65
import Operator from './operator'
@@ -41,7 +40,9 @@ class Engine extends EventEmitter {
4140
* @param {Object} properties.conditions - conditions to evaluate when processing this rule
4241
*/
4342
addRule (properties) {
44-
params(properties).require(['conditions', 'event'])
43+
if (!properties) throw new Error('Engine: addRule() requires options')
44+
if (!properties.hasOwnProperty('conditions')) throw new Error('Engine: addRule() argument requires "conditions" property')
45+
if (!properties.hasOwnProperty('event')) throw new Error('Engine: addRule() argument requires "event" property')
4546

4647
let rule
4748
if (properties instanceof Rule) {

src/rule.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
import params from 'params'
43
import Condition from './condition'
54
import { EventEmitter } from 'events'
65
import deepClone from 'lodash.clonedeep'
@@ -70,7 +69,12 @@ class Rule extends EventEmitter {
7069
* @param {string} event.params - parameters to emit as the argument of the event emission
7170
*/
7271
setEvent (event) {
73-
this.event = params(event).only(['type', 'params'])
72+
if (!event) throw new Error('Rule: setEvent() requires event object')
73+
if (!event.hasOwnProperty('type')) throw new Error('Rule: setEvent() requires event object with "type" property')
74+
this.event = {
75+
type: event.type
76+
}
77+
if (event.params) this.event.params = event.params
7478
return this
7579
}
7680

test/condition.test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,26 @@ describe('Condition', () => {
231231
})
232232

233233
describe('atomic facts', () => {
234+
it('throws if no options are provided', () => {
235+
expect(() => new Condition()).to.throw(/Condition: constructor options required/)
236+
})
237+
234238
it('throws for a missing "operator"', () => {
235239
let conditions = condition()
236240
delete conditions.all[0].operator
237-
expect(() => new Condition(conditions)).to.throw(/Missing key "operator"/)
241+
expect(() => new Condition(conditions)).to.throw(/Condition: constructor "operator" property required/)
238242
})
239243

240244
it('throws for a missing "fact"', () => {
241245
let conditions = condition()
242246
delete conditions.all[0].fact
243-
expect(() => new Condition(conditions)).to.throw(/Missing key "fact"/)
247+
expect(() => new Condition(conditions)).to.throw(/Condition: constructor "fact" property required/)
244248
})
245249

246250
it('throws for a missing "value"', () => {
247251
let conditions = condition()
248252
delete conditions.all[0].value
249-
expect(() => new Condition(conditions)).to.throw(/Missing key "value"/)
253+
expect(() => new Condition(conditions)).to.throw(/Condition: constructor "value" property required/)
250254
})
251255
})
252256

@@ -288,7 +292,7 @@ describe('Condition', () => {
288292
it('throws if a nested condition is invalid', () => {
289293
let conditions = complexCondition()
290294
delete conditions.all[2].any[0].fact
291-
expect(() => new Condition(conditions)).to.throw(/Missing key "fact"/)
295+
expect(() => new Condition(conditions)).to.throw(/Condition: constructor "fact" property required/)
292296
})
293297
})
294298
})

test/engine.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ describe('Engine', () => {
5858
delete rule.conditions
5959
expect(() => {
6060
engine.addRule(rule)
61-
}).to.throw(/Missing key "conditions"/)
61+
}).to.throw(/Engine: addRule\(\) argument requires "conditions" property/)
6262
})
6363

6464
it('.event', () => {
6565
let rule = factories.rule()
6666
delete rule.event
6767
expect(() => {
6868
engine.addRule(rule)
69-
}).to.throw(/Missing key "event"/)
69+
}).to.throw(/Engine: addRule\(\) argument requires "event" property/)
7070
})
7171
})
7272
})

test/rule.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ describe('Rule', () => {
8282
})
8383
})
8484

85+
describe('setEvent()', () => {
86+
it('throws if no argument provided', () => {
87+
expect(() => rule.setEvent()).to.throw(/Rule: setEvent\(\) requires event object/)
88+
})
89+
90+
it('throws if argument is missing "type" property', () => {
91+
expect(() => rule.setEvent({})).to.throw(/Rule: setEvent\(\) requires event object with "type" property/)
92+
})
93+
})
94+
8595
describe('setConditions()', () => {
8696
describe('validations', () => {
8797
it('throws an exception for invalid root conditions', () => {

0 commit comments

Comments
 (0)