0

I have a string that is building a condition, it could read:

"true || false" or "false || false" or "true && false" etc..

I want to simply check if it passes or not. Any suggestions?

P̲̳x͓L̳
  • 3,615
  • 3
  • 29
  • 37
Envious
  • 584
  • 1
  • 6
  • 20
  • 2
    Someone will propose `eval`. So just ahead of time: don't use `eval` without knowing exactly what that string contains (e.g. if it's user input). – Ingo Bürk Mar 31 '14 at 19:19
  • 1
    You're probably barking up the wrong tree. What's the greater purpose of this string? – SomeKittens Mar 31 '14 at 19:19
  • Where is this string coming from? – gen_Eric Mar 31 '14 at 19:20
  • The string is being built by a user defined option in a widget. For instance "val1 > val2 || val2 < val1" but it could be a number of things the user is asking to check. – Envious Mar 31 '14 at 19:21
  • this can be done easily...but why put efforts in wrong direction ? if string building is under your control .. you can do it in a simple way. – Adil Shaikh Mar 31 '14 at 19:21

2 Answers2

0

Why don't you just use a boolean instead of assembling the string? I'm assuming you're running something like:

str += ' && ' + !!(Find some value)

Instead, track with a boolean:

trackingBool = trackingBool && !!(Find some value)

This way you avoid using a string as a boolean (an antipattern if I've ever seen one) and others reading your code have a better idea of what's going on.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
-1

eval seems to be what I am looking for even though Ingo said not to use it ;)

Envious
  • 584
  • 1
  • 6
  • 20