UNPKG

2.1 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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
9function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
11var 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
50exports.default = Operator;
\No newline at end of file