UNPKG

json-rules-engine

Version:
60 lines (46 loc) 2.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _operator = require('./operator'); var _operator2 = _interopRequireDefault(_operator); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var OperatorDecorator = function () { /** * Constructor * @param {string} name - decorator identifier * @param {function(factValue, jsonValue, next)} callback - callback that takes the next operator as a parameter * @param {function} [factValueValidator] - optional validator for asserting the data type of the fact * @returns {OperatorDecorator} - instance */ function OperatorDecorator(name, cb, factValueValidator) { _classCallCheck(this, OperatorDecorator); this.name = String(name); if (!name) throw new Error('Missing decorator name'); if (typeof cb !== 'function') throw new Error('Missing decorator callback'); this.cb = cb; this.factValueValidator = factValueValidator; if (!this.factValueValidator) this.factValueValidator = function () { return true; }; } /** * Takes the fact result and compares it to the condition 'value', using the callback * @param {Operator} operator - fact result * @returns {Operator} - whether the values pass the operator test */ _createClass(OperatorDecorator, [{ key: 'decorate', value: function decorate(operator) { var _this = this; var next = operator.evaluate.bind(operator); return new _operator2.default(this.name + ':' + operator.name, function (factValue, jsonValue) { return _this.cb(factValue, jsonValue, next); }, this.factValueValidator); } }]); return OperatorDecorator; }(); exports.default = OperatorDecorator;