1

Is it possible in javascript to convert a string to an operator?

I'm building a comparator for input in AngularJS.

function comparator(operatorString, value1, value2){
    return value1 converted_operator(operatorString) value2;
}

var myVar1 = comparator("<", 12, 13);//true
var myVar2 = comparator(">=", 12, 13);//false
var myvar3 = comparator("!=" 12, 13);//true

And there are many possible combinations of operators. I could achieve that with an if else. But I would like to know if there is any other alternative for the same.

Pac0
  • 21,465
  • 8
  • 65
  • 74
Sreekanth Reddy Balne
  • 3,264
  • 1
  • 18
  • 44
  • 2
    There is no direct way to do that besides `eval()` (well or via `new Function()` which is essentially the same thing). – Pointy Oct 08 '18 at 15:36
  • You can look into`eval` – Sid Oct 08 '18 at 15:36
  • 5
    you could use a mapping with a some kind of dictionary from the string operator to a *function*, and think in a functional approach. That would imply prewritng a list of accepted functions, which would somehow be similar to several if/else, but maybe a bit more beautiful. and more reusable. – Pac0 Oct 08 '18 at 15:36
  • I think I'm poor in understanding English or the terms you people are using. Could you please answer it? with various ways to do it. I know `eval()` from python. Is it the same here? – Sreekanth Reddy Balne Oct 08 '18 at 15:38
  • Note that just because you *can* use `eval` for this, [doesn't mean you *should*](https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea). I'd recommend Pac0's approach. – Joe Clay Oct 08 '18 at 15:38
  • haaaa I was writing a more complete answer but it was a duplicate... :'( (which was basically the accepted answer in the duplicate, but with arrow functions and comparisons operators instead of arithmetic ones) – Pac0 Oct 08 '18 at 15:43
  • `eval` is fine client-side or for small operations like this that run no fear of injection. Developers are spooked about it to the point that it's considered immediately wrong to suggest, and to be honest it's like a ghost story that just kind of perpetuates itself. Personally `new Function` is a better alternative for code sanity and scope, but `eval` isn't evil, and for something like this it would be acceptable. – zfrisch Oct 08 '18 at 15:55
  • @zfrisch agreed, though it should be noted that code that uses `eval()` or from-string functions generally gets tagged by the underlying runtime as being ineligible for optimization (because it's basically impossible to know what such things might do to the environment). – Pointy Oct 08 '18 at 15:58
  • And of course that's a thing to know but probably not important for most code. – Pointy Oct 08 '18 at 15:59

1 Answers1

0

Right from the node console:

**> function comparator(operatorString, value1, value2){
    return eval(value1 + operatorString + value2)
   }
undefined
> var myVar1 = comparator("<", 12, 13);
undefined
> myVar1
true
>**
Sid
  • 4,893
  • 14
  • 55
  • 110