@@ -32,6 +32,7 @@ class LogicEngine {
32
32
this . disableInline = options . disableInline
33
33
this . disableInterpretedOptimization = options . disableInterpretedOptimization
34
34
this . methods = { ...methods }
35
+ this . precision = null
35
36
36
37
this . optimizedMap = new WeakMap ( )
37
38
this . missesSinceSeen = 0
@@ -67,7 +68,7 @@ class LogicEngine {
67
68
const [ func ] = Object . keys ( logic )
68
69
const data = logic [ func ]
69
70
70
- if ( this . isData ( logic , func ) ) return logic
71
+ if ( this . isData ( logic , func ) || ( this . precision && logic instanceof this . precision ) ) return logic
71
72
72
73
if ( ! this . methods [ func ] ) throw new Error ( `Method '${ func } ' was not found in the Logic Engine.` )
73
74
@@ -77,9 +78,9 @@ class LogicEngine {
77
78
}
78
79
79
80
if ( typeof this . methods [ func ] === 'object' ) {
80
- const { method, traverse } = this . methods [ func ]
81
+ const { method, traverse, precise } = this . methods [ func ]
81
82
const shouldTraverse = typeof traverse === 'undefined' ? true : traverse
82
- const parsedData = shouldTraverse ? ( ( ! data || typeof data !== 'object' ) ? [ data ] : coerceArray ( this . run ( data , context , { above } ) ) ) : data
83
+ const parsedData = shouldTraverse ? ( ( ! data || typeof data !== 'object' ) ? [ data ] : coerceArray ( this . run ( data , context , { above, precise } ) ) ) : data
83
84
return method ( parsedData , context , above , this )
84
85
}
85
86
@@ -123,7 +124,7 @@ class LogicEngine {
123
124
*
124
125
* @param {* } logic The logic to be executed
125
126
* @param {* } data The data being passed in to the logic to be executed against.
126
- * @param {{ above?: any } } options Options for the invocation
127
+ * @param {{ above?: any, precise?: boolean } } options Options for the invocation
127
128
* @returns {* }
128
129
*/
129
130
run ( logic , data = { } , options = { } ) {
@@ -149,11 +150,14 @@ class LogicEngine {
149
150
150
151
if ( Array . isArray ( logic ) ) {
151
152
const res = [ ]
152
- for ( let i = 0 ; i < logic . length ; i ++ ) res . push ( this . run ( logic [ i ] , data , { above } ) )
153
+ for ( let i = 0 ; i < logic . length ; i ++ ) res . push ( this . run ( logic [ i ] , data , options ) )
153
154
return res
154
155
}
155
156
156
- if ( logic && typeof logic === 'object' && Object . keys ( logic ) . length > 0 ) return this . _parse ( logic , data , above )
157
+ if ( logic && typeof logic === 'object' ) {
158
+ if ( this . precision && ! options . precise && logic . toNumber ) return Number ( logic )
159
+ if ( Object . keys ( logic ) . length > 0 ) return this . _parse ( logic , data , above )
160
+ }
157
161
158
162
return logic
159
163
}
0 commit comments