@shepherdnerds/json-rules-engine 7.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.OperatorDecorator = exports.ScopedAlmanac = exports.Almanac = exports.Engine = exports.Operator = exports.Rule = exports.Fact = undefined;
7
+
8
+ exports.default = function (rules, options) {
9
+ return new _engine2.default(rules, options);
10
+ };
11
+
12
+ var _engine = require('./engine');
13
+
14
+ var _engine2 = _interopRequireDefault(_engine);
15
+
16
+ var _fact = require('./fact');
17
+
18
+ var _fact2 = _interopRequireDefault(_fact);
19
+
20
+ var _rule = require('./rule');
21
+
22
+ var _rule2 = _interopRequireDefault(_rule);
23
+
24
+ var _operator = require('./operator');
25
+
26
+ var _operator2 = _interopRequireDefault(_operator);
27
+
28
+ var _almanac = require('./almanac');
29
+
30
+ var _almanac2 = _interopRequireDefault(_almanac);
31
+
32
+ var _scopedAlmanac = require('./scoped-almanac');
33
+
34
+ var _scopedAlmanac2 = _interopRequireDefault(_scopedAlmanac);
35
+
36
+ var _operatorDecorator = require('./operator-decorator');
37
+
38
+ var _operatorDecorator2 = _interopRequireDefault(_operatorDecorator);
39
+
40
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
41
+
42
+ exports.Fact = _fact2.default;
43
+ exports.Rule = _rule2.default;
44
+ exports.Operator = _operator2.default;
45
+ exports.Engine = _engine2.default;
46
+ exports.Almanac = _almanac2.default;
47
+ exports.ScopedAlmanac = _scopedAlmanac2.default;
48
+ exports.OperatorDecorator = _operatorDecorator2.default;
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ 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; }; }();
8
+
9
+ var _operator = require('./operator');
10
+
11
+ var _operator2 = _interopRequireDefault(_operator);
12
+
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
+
15
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
16
+
17
+ var OperatorDecorator = function () {
18
+ /**
19
+ * Constructor
20
+ * @param {string} name - decorator identifier
21
+ * @param {function(factValue, jsonValue, next)} callback - callback that takes the next operator as a parameter
22
+ * @param {function} [factValueValidator] - optional validator for asserting the data type of the fact
23
+ * @returns {OperatorDecorator} - instance
24
+ */
25
+ function OperatorDecorator(name, cb, factValueValidator) {
26
+ _classCallCheck(this, OperatorDecorator);
27
+
28
+ this.name = String(name);
29
+ if (!name) throw new Error('Missing decorator name');
30
+ if (typeof cb !== 'function') throw new Error('Missing decorator callback');
31
+ this.cb = cb;
32
+ this.factValueValidator = factValueValidator;
33
+ if (!this.factValueValidator) this.factValueValidator = function () {
34
+ return true;
35
+ };
36
+ }
37
+
38
+ /**
39
+ * Takes the fact result and compares it to the condition 'value', using the callback
40
+ * @param {Operator} operator - fact result
41
+ * @returns {Operator} - whether the values pass the operator test
42
+ */
43
+
44
+
45
+ _createClass(OperatorDecorator, [{
46
+ key: 'decorate',
47
+ value: function decorate(operator) {
48
+ var _this = this;
49
+
50
+ var next = operator.evaluate.bind(operator);
51
+ return new _operator2.default(this.name + ':' + operator.name, function (factValue, jsonValue) {
52
+ return _this.cb(factValue, jsonValue, next);
53
+ }, this.factValueValidator);
54
+ }
55
+ }]);
56
+
57
+ return OperatorDecorator;
58
+ }();
59
+
60
+ exports.default = OperatorDecorator;
@@ -0,0 +1,178 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ 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; }; }();
8
+
9
+ var _operator = require('./operator');
10
+
11
+ var _operator2 = _interopRequireDefault(_operator);
12
+
13
+ var _operatorDecorator = require('./operator-decorator');
14
+
15
+ var _operatorDecorator2 = _interopRequireDefault(_operatorDecorator);
16
+
17
+ var _debug = require('./debug');
18
+
19
+ var _debug2 = _interopRequireDefault(_debug);
20
+
21
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
+
23
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
24
+
25
+ var OperatorMap = function () {
26
+ function OperatorMap() {
27
+ _classCallCheck(this, OperatorMap);
28
+
29
+ this.operators = new Map();
30
+ this.decorators = new Map();
31
+ }
32
+
33
+ /**
34
+ * Add a custom operator definition
35
+ * @param {string} operatorOrName - operator identifier within the condition; i.e. instead of 'equals', 'greaterThan', etc
36
+ * @param {function(factValue, jsonValue)} callback - the method to execute when the operator is encountered.
37
+ */
38
+
39
+
40
+ _createClass(OperatorMap, [{
41
+ key: 'addOperator',
42
+ value: function addOperator(operatorOrName, cb) {
43
+ var operator = void 0;
44
+ if (operatorOrName instanceof _operator2.default) {
45
+ operator = operatorOrName;
46
+ } else {
47
+ operator = new _operator2.default(operatorOrName, cb);
48
+ }
49
+ (0, _debug2.default)('operatorMap::addOperator', { name: operator.name });
50
+ this.operators.set(operator.name, operator);
51
+ }
52
+
53
+ /**
54
+ * Remove a custom operator definition
55
+ * @param {string} operatorOrName - operator identifier within the condition; i.e. instead of 'equals', 'greaterThan', etc
56
+ * @param {function(factValue, jsonValue)} callback - the method to execute when the operator is encountered.
57
+ */
58
+
59
+ }, {
60
+ key: 'removeOperator',
61
+ value: function removeOperator(operatorOrName) {
62
+ var operatorName = void 0;
63
+ if (operatorOrName instanceof _operator2.default) {
64
+ operatorName = operatorOrName.name;
65
+ } else {
66
+ operatorName = operatorOrName;
67
+ }
68
+
69
+ // Delete all the operators that end in :operatorName these
70
+ // were decorated on-the-fly leveraging this operator
71
+ var suffix = ':' + operatorName;
72
+ var operatorNames = Array.from(this.operators.keys());
73
+ for (var i = 0; i < operatorNames.length; i++) {
74
+ if (operatorNames[i].endsWith(suffix)) {
75
+ this.operators.delete(operatorNames[i]);
76
+ }
77
+ }
78
+
79
+ return this.operators.delete(operatorName);
80
+ }
81
+
82
+ /**
83
+ * Add a custom operator decorator
84
+ * @param {string} decoratorOrName - decorator identifier within the condition; i.e. instead of 'everyFact', 'someValue', etc
85
+ * @param {function(factValue, jsonValue, next)} callback - the method to execute when the decorator is encountered.
86
+ */
87
+
88
+ }, {
89
+ key: 'addOperatorDecorator',
90
+ value: function addOperatorDecorator(decoratorOrName, cb) {
91
+ var decorator = void 0;
92
+ if (decoratorOrName instanceof _operatorDecorator2.default) {
93
+ decorator = decoratorOrName;
94
+ } else {
95
+ decorator = new _operatorDecorator2.default(decoratorOrName, cb);
96
+ }
97
+ (0, _debug2.default)('operatorMap::addOperatorDecorator', { name: decorator.name });
98
+ this.decorators.set(decorator.name, decorator);
99
+ }
100
+
101
+ /**
102
+ * Remove a custom operator decorator
103
+ * @param {string} decoratorOrName - decorator identifier within the condition; i.e. instead of 'everyFact', 'someValue', etc
104
+ */
105
+
106
+ }, {
107
+ key: 'removeOperatorDecorator',
108
+ value: function removeOperatorDecorator(decoratorOrName) {
109
+ var decoratorName = void 0;
110
+ if (decoratorOrName instanceof _operatorDecorator2.default) {
111
+ decoratorName = decoratorOrName.name;
112
+ } else {
113
+ decoratorName = decoratorOrName;
114
+ }
115
+
116
+ // Delete all the operators that include decoratorName: these
117
+ // were decorated on-the-fly leveraging this decorator
118
+ var prefix = decoratorName + ':';
119
+ var operatorNames = Array.from(this.operators.keys());
120
+ for (var i = 0; i < operatorNames.length; i++) {
121
+ if (operatorNames[i].includes(prefix)) {
122
+ this.operators.delete(operatorNames[i]);
123
+ }
124
+ }
125
+
126
+ return this.decorators.delete(decoratorName);
127
+ }
128
+
129
+ /**
130
+ * Get the Operator, or null applies decorators as needed
131
+ * @param {string} name - the name of the operator including any decorators
132
+ * @returns an operator or null
133
+ */
134
+
135
+ }, {
136
+ key: 'get',
137
+ value: function get(name) {
138
+ var decorators = [];
139
+ var opName = name;
140
+ // while we don't already have this operator
141
+ while (!this.operators.has(opName)) {
142
+ // try splitting on the decorator symbol (:)
143
+ var firstDecoratorIndex = opName.indexOf(':');
144
+ if (firstDecoratorIndex > 0) {
145
+ // if there is a decorator, and it's a valid decorator
146
+ var decoratorName = opName.slice(0, firstDecoratorIndex);
147
+ var decorator = this.decorators.get(decoratorName);
148
+ if (!decorator) {
149
+ (0, _debug2.default)('operatorMap::get invalid decorator', { name: decoratorName });
150
+ return null;
151
+ }
152
+ // we're going to apply this later, use unshift since we'll apply in reverse order
153
+ decorators.unshift(decorator);
154
+ // continue looking for a known operator with the rest of the name
155
+ opName = opName.slice(firstDecoratorIndex + 1);
156
+ } else {
157
+ (0, _debug2.default)('operatorMap::get no operator', { name: opName });
158
+ return null;
159
+ }
160
+ }
161
+
162
+ var op = this.operators.get(opName);
163
+ // apply all the decorators
164
+ for (var i = 0; i < decorators.length; i++) {
165
+ op = decorators[i].decorate(op);
166
+ // create an entry for the decorated operation so we don't need
167
+ // to do this again
168
+ this.operators.set(op.name, op);
169
+ }
170
+ // return the operation
171
+ return op;
172
+ }
173
+ }]);
174
+
175
+ return OperatorMap;
176
+ }();
177
+
178
+ exports.default = OperatorMap;
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ 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; }; }();
8
+
9
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
+
11
+ var Operator = function () {
12
+ /**
13
+ * Constructor
14
+ * @param {string} name - operator identifier
15
+ * @param {function(factValue, jsonValue)} callback - operator evaluation method
16
+ * @param {function} [factValueValidator] - optional validator for asserting the data type of the fact
17
+ * @returns {Operator} - instance
18
+ */
19
+ function Operator(name, cb, factValueValidator) {
20
+ _classCallCheck(this, Operator);
21
+
22
+ this.name = String(name);
23
+ if (!name) throw new Error('Missing operator name');
24
+ if (typeof cb !== 'function') throw new Error('Missing operator callback');
25
+ this.cb = cb;
26
+ this.factValueValidator = factValueValidator;
27
+ if (!this.factValueValidator) this.factValueValidator = function () {
28
+ return true;
29
+ };
30
+ }
31
+
32
+ /**
33
+ * Takes the fact result and compares it to the condition 'value', using the callback
34
+ * @param {mixed} factValue - fact result
35
+ * @param {mixed} jsonValue - "value" property of the condition
36
+ * @returns {Boolean} - whether the values pass the operator test
37
+ */
38
+
39
+
40
+ _createClass(Operator, [{
41
+ key: 'evaluate',
42
+ value: function evaluate(factValue, jsonValue) {
43
+ return this.factValueValidator(factValue) && this.cb(factValue, jsonValue);
44
+ }
45
+ }]);
46
+
47
+ return Operator;
48
+ }();
49
+
50
+ exports.default = Operator;
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
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
+
9
+ 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; }; }();
10
+
11
+ var _clone = require('clone');
12
+
13
+ var _clone2 = _interopRequireDefault(_clone);
14
+
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+
17
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
18
+
19
+ var RuleResult = function () {
20
+ function RuleResult(conditions, event, priority, name) {
21
+ _classCallCheck(this, RuleResult);
22
+
23
+ this.conditions = (0, _clone2.default)(conditions);
24
+ this.event = (0, _clone2.default)(event);
25
+ this.priority = (0, _clone2.default)(priority);
26
+ this.name = (0, _clone2.default)(name);
27
+ this.result = null;
28
+ }
29
+
30
+ _createClass(RuleResult, [{
31
+ key: 'setResult',
32
+ value: function setResult(result) {
33
+ this.result = result;
34
+ }
35
+ }, {
36
+ key: 'resolveEventParams',
37
+ value: function resolveEventParams(almanac) {
38
+ var _this = this;
39
+
40
+ if (this.event.params !== null && _typeof(this.event.params) === 'object') {
41
+ var updates = [];
42
+
43
+ var _loop = function _loop(key) {
44
+ if (Object.prototype.hasOwnProperty.call(_this.event.params, key)) {
45
+ updates.push(almanac.getValue(_this.event.params[key]).then(function (val) {
46
+ return _this.event.params[key] = val;
47
+ }));
48
+ }
49
+ };
50
+
51
+ for (var key in this.event.params) {
52
+ _loop(key);
53
+ }
54
+ return Promise.all(updates);
55
+ }
56
+ return Promise.resolve();
57
+ }
58
+ }, {
59
+ key: 'toJSON',
60
+ value: function toJSON() {
61
+ var stringify = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
62
+
63
+ var props = {
64
+ conditions: this.conditions.toJSON(false),
65
+ event: this.event,
66
+ priority: this.priority,
67
+ name: this.name,
68
+ result: this.result
69
+ };
70
+ if (stringify) {
71
+ return JSON.stringify(props);
72
+ }
73
+ return props;
74
+ }
75
+ }]);
76
+
77
+ return RuleResult;
78
+ }();
79
+
80
+ exports.default = RuleResult;