I have a function that allows strings to be evaluated against an environment
function filt(M, filterString, cb){
var res
try{
res = eval(filterString)
} catch(me){
// If in doubt permit
res = true
}
cb(res)
}
The problem is that I would like this filter to be optimistic. You can see the optimism in the catch
, where the response is set to true if anything goes wrong.
If I run filt({ age : 20 }, 'M.age < 21', callback)
then my callback returns true
. Similarly filt({ age: 21}, 'M.age < 21', callback)
returns false as expected.
I would however like the following to return true also:
filt({}, 'M.age < 21', callback)
. However undefined==true
is false (similar for all boolean expressions with undefined
). I thought about inspecting the string using regular expression (all my variables are passed as fields on the object M
). So that if filterString
contains M.varname
(or M."varname"
or M["varname"]
then replace with true if there is no such field on M
. However prior to coding this I thought I would put the feelers out to see if there is a better approach.
Any ideas? Thanks in advance.
Update:
Thanks to the feedback I can see that my entire approach needs rethinking. To simplify then I am wondering if there is a a method of evaluating expressions with unknown values so that if an expression involves and unknown then the outcome is unknown. For example in pseudocode:
M > 21 = unknown if M == unknown
M > 21 = true if M == 23
M > 21 = false if M == 'fish'
M > 21 | true = true independent of the value of M.