1

My code dynamically generates string/number operations. The program dynamically builds something similar to the following:

"My name " + "is " + "G-Man"
"Your age is " + "21"
"5" * "5"

I want to output this:

My Name is G-Man
Your age is 21
25

I can write a library for this, but I currently am under time constraints. If anyone aware of a library that can perform equations similar to above (int + int = int), (string + int = string), etc.?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
harsimranb
  • 2,231
  • 1
  • 35
  • 55

1 Answers1

0

I think you probably just want to use JavaScript's built in EVAL function.

var a = eval("5 + 5");
console.log(a); // >> 10

EDIT Wow I got 2 down-votes at almost robotic speed when I answered this question ~weird, but again EVAL is probably what you want.

var a = eval("'Your age is ' + '22'");
console.log(a); // >> Your age is 22 

EDIT 2 Here's a starting point for doing some quick validation of the input to make sure nothing naughty gets Eval'd.

var test1 = [
"testing"
,"+"
,"123"
];
var test2 = [
"8"
,"*"
,"5"
,"/"
,"3"
];
var test3 = [
"window.alert('bad');"
];
var test4 = [
"\"It's hard to escape things\", said "
," + "
,"Bob"
];

function supereval(arr) {
    var sEval = '';
    for(var i=0; i<arr.length; i++) {
        if(!isNaN(parseFloat(arr[i])) && isFinite(arr[i])) { // number
            sEval += arr[i];
//console.log("> number");
        } else if( /^\s?[\+\/\*\-]\s?$/i.test(arr[i]) ) { // operation
            sEval += arr[i];
//console.log("> operation");
        } else { // string
            sEval += "\"" + arr[i].replace(/"/g, '\\"') + "\"";
//console.log("> string");
        }
    }
console.log("DEBUG:" + sEval);
    return eval(sEval);
}

console.log(supereval(test1));
console.log(supereval(test2));
console.log(supereval(test3));
console.log(supereval(test4));
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
  • I did look into that, but it would not work for strings....i didn't vote you down though, because that was what I tried first.... :) – harsimranb Oct 02 '12 at 19:31
  • @Pathachiever11 - My added example shows it does work for strings too. – Louis Ricci Oct 02 '12 at 19:34
  • I have to agree that `eval` is the best choice when it comes to calculating things 'on-the-fly', but I would suggest at least putting a guard in front of it to whitelist just identifiers, numbers and operators. Minimize the possible injection executable code. – Jeremy J Starcher Oct 02 '12 at 19:38
  • 1
    I'm not a knee-jerk **eval is evil** robot, but it is probably best to use it with caution and make a case for why it's proper to use it in a given case. Since it sounds like the strings are coming from user input(?), just blindly eval'ing it is probably not the best way to go. – ultranaut Oct 02 '12 at 19:38
  • @Jeremy J Starcher - Some validation is definitely necessary if your allowing users to put in anything. I've updated the answer with a simple wrapper for eval that takes array input and does some extra sanity checks. – Louis Ricci Oct 03 '12 at 12:02
  • a bit dangerous, but this is what I'll have to use for now....I will definitely take precautions... – harsimranb Oct 04 '12 at 18:53