@@ -32,6 +32,7 @@ class LogicEngine {
3232 this . disableInline = options . disableInline
3333 this . disableInterpretedOptimization = options . disableInterpretedOptimization
3434 this . methods = { ...methods }
35+ this . precision = null
3536
3637 this . optimizedMap = new WeakMap ( )
3738 this . missesSinceSeen = 0
@@ -67,7 +68,7 @@ class LogicEngine {
6768 const [ func ] = Object . keys ( logic )
6869 const data = logic [ func ]
6970
70- if ( this . isData ( logic , func ) ) return logic
71+ if ( this . isData ( logic , func ) || ( this . precision && logic instanceof this . precision ) ) return logic
7172
7273 if ( ! this . methods [ func ] ) throw new Error ( `Method '${ func } ' was not found in the Logic Engine.` )
7374
@@ -77,9 +78,9 @@ class LogicEngine {
7778 }
7879
7980 if ( typeof this . methods [ func ] === 'object' ) {
80- const { method, traverse } = this . methods [ func ]
81+ const { method, traverse, precise } = this . methods [ func ]
8182 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
8384 return method ( parsedData , context , above , this )
8485 }
8586
@@ -123,7 +124,7 @@ class LogicEngine {
123124 *
124125 * @param {* } logic The logic to be executed
125126 * @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
127128 * @returns {* }
128129 */
129130 run ( logic , data = { } , options = { } ) {
@@ -149,11 +150,14 @@ class LogicEngine {
149150
150151 if ( Array . isArray ( logic ) ) {
151152 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 ) )
153154 return res
154155 }
155156
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+ }
157161
158162 return logic
159163 }
0 commit comments