0

I have string that have variable conditions in it, like this:

(@IS_VERIFIED = 'True' OR @CONFIRMATION_NEEDED != 'True') AND @REQUEST_LIMIT != '0'

This is just an example and there are unknown number of variables & cases.

  1. Every variable starts with @
  2. There are ORs and ANDs with sometimes parenthesis
  3. Values are always in quotation marks like 'xyz', so all can be considered strings.
  4. Conditions are always checked with either = or !=

I also have a javascript map which holds all the variables like:

const vars = {
 IS_VERIFIED: 'True',
 CONFIRMATION_NEEDED: 'True',
 REQUEST_LIMIT: '2'
}

What I need to do is parse the condition string and check the values using the map above to reach a final result, true or false. I came up with few ideas but they were all too complicated. Is there any known, easy method for this or do I have to create something from scratch?

Any help is appreciated, thanks.

Edit: After achieving this, next goal for me will be showing which variables break the condition, somehow.

3
  • Regular Expressions might come in handy here. Did you try using that? Commented Apr 21, 2022 at 10:30
  • @AnindyaDey yeah, I thought about it but couldn't figure out how to apply it to cases where there might be multiple paranthesis inside each other and all. I think I have to create a recursive logic but not sure how. Commented Apr 21, 2022 at 10:32
  • I can think of a solution using eval, but are you willing to use eval? Commented Apr 21, 2022 at 11:03

2 Answers 2

1

Caution: eval solution ahead, be very careful while using this!

Simply modify the string to be a valid JS expression and then use eval.

const vars = {
  IS_VERIFIED: "True",
  CONFIRMATION_NEEDED: "True",
  REQUEST_LIMIT: "2",
};

const str = `(@IS_VERIFIED = 'True' OR @CONFIRMATION_NEEDED != 'True') AND @REQUEST_LIMIT != '0'`;

const evalStr = str
  .replaceAll("@", "vars.")
  .replaceAll("OR", "||")
  .replaceAll("AND", "&&")
  .replaceAll("!=", "!")
  .replaceAll("=", "===")
  .replaceAll("!", "!==");

const res = eval(evalStr);
console.log(res);

Sign up to request clarification or add additional context in comments.

3 Comments

I know eval is not going to be liked much here, but I loved this. Very clever.
I thought about using eval but the condition string will be a user input, it is not an option for me due to security risks and I am not allowed to use it. Also even if I could eliminate the security risks somehow, than it would be impossible to do the next step(check the edit). So I guess I have to parse the string somehow.
@Nanoturka That's unfortunate! Some fancy string parsing with regex might be able to help.
0

Here is a snippet using RegExp:

let test = "@XYZ='True' AND @ABC='False'";

const regexp = /(@\w+)|(\'\w+\')/g;

const regexpResult = [...test.match(regexp)]
// Returns something like this:
// ["@XYZ", "'True'", "@ABC", "'False'"]

const vars = {};

for (let i = 0; i < regexpResult.length; i += 2) // We will be using two consecutive values at a time
{
  let keyName = regexpResult[i].replace("@", "");
  let keyValue = regexpResult[i+1].replaceAll("'", "");
  
  vars[keyName] = keyValue;
}

// OUTPUT of "vars":
// {
//   XYZ: "True",
//   ABC: "False"
// }

I hope this gives you an idea about how to solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.