1

I am trying to convert a string representation of an operator to the operator itself in comparisons.


For example, something like

{obj1} '==' {obj2}

should be:

{obj1} == {obj2}

OR

{obj1} '!==' {obj2}

should be:

{obj1} !== {obj2}

I can use eval(), but that forces the objects to be compared to be converted to a string, causing === (strict type check) comparisons useless.

For example, in the following example, eval would give wrong results:

const obj = "5"
const obj2 = 5

const operator = '===';

eval(`console.log(${obj} ${operator} ${obj2})`) //outputs true, incorrect (obj is of type string, obj2 is of type number)

console.log(obj === obj2) //outputs false, correct

How should I approach this?


Possible approach

One way that I came up with is just to go through the if else ladder and let js handle the comparison instead of parsing the comparison operator.

Something like

if(condition === '!=='){
  return before_OrderRequest.Id !== after_OrderRequest.Id
}else if (condition === '==='){
  return before_OrderRequest.Id === after_OrderRequest.Id
}
//...more if statements
3
  • 1
    I described exactly why eval() is falling short of our requirement, or maybe there is a way to just eval the condition operator that I don't know of, please explain. Commented Sep 16, 2021 at 2:03
  • 1
    @Spectric There is literally a sample string in the second line. Commented Sep 16, 2021 at 2:04
  • 1
    Currently the string contains the placeholders and the operator, we are programatically replacing the placeholders with the actual values, after which we are then running the string through an eval() Commented Sep 16, 2021 at 2:05

2 Answers 2

4

You can define an object with the string representation of operators as properties that return an arrow function performing the corresponding comparison.

const compare = {
  '==': (a, b) => a == b,
  '!=': (a, b) => a != b,
  '===' : (a, b) => a === b,
  '!==' : (a, b) => a !== b
}

const obj1 = 1;
const obj2 = 2;

console.log(compare['=='](obj1, obj2))
console.log(compare['!='](obj1, obj2))
console.log(compare['==='](obj1, obj2))
console.log(compare['!=='](obj1, obj2))

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

1 Comment

This looks like something similar to what I came up with, but a bit more elegant, so I'm gonna go ahead with this unless there is some better alternative. Thanks.
0

If you are dealing with the similar issue, Please understand that my question is built on sort of a false premise. Allow me to explain...

If you look at my sample executable snippet, you'll notice that I'm using the eval like below

eval(`console.log(${obj} ${operator} ${obj2})`)

But this usage by me is wrong, here I'm using string interpolation which is forcing the value for obj2 into a string.

The correct usage should have been as below...

const obj = "5"
const obj2 = 5

const operator1 = '===';
const operator2 = '!==';

eval(`console.log(obj ${operator1} obj2)`) //outputs false which is correct
eval(`console.log(obj ${operator2} obj2)`) //outputs true which is correct

console.log(obj === obj2) //outputs false which is correct

Have added this as an answer, instead of just editing the question, so that there is context for someone who is facing similar type of challenge.

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.