@@ -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-
97var _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
2111function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
2212
13+ var params = require ( 'params' ) ;
2314var debug = require ( 'debug' ) ( 'json-rules-engine' ) ;
24- var warn = require ( 'debug' ) ( 'json-rules-engine:warn ') ;
15+ var isPlainObject = require ( 'lodash.isplainobject ' ) ;
2516
2617var 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
0 commit comments