5

I'm working on a webapp that for the last 2 years has used the unofficial API for Google Calculator like: http://www.google.com/ig/calculator?hl=en&q=

Now that iGoogle has been closed last week (https://support.google.com/websearch/answer/2664197) the google calculator api doesn't work either.

What's the best alternative for doing a javascript calculator? (Where I give it a string and it evaluates the answer.)

Some ideas I've had:

  • Find another Google search query that will return the results (I'm not afraid of doing some html scraping.)
  • Find another api provided by someone else (something like YQL - I am already using them for currency conversion
  • Use a pure javascript calculator - Is there a good javascript library or script you can recommend? (Most scripts I've seen are just doing eval(), which I can't use, and most seem to be tutorials to introduce javascript.)

Any suggestions?

Here are some sample queries that the Google Calculator handles. The more I can do, the better:



YQL currency conversion info for anyone that comes looking for it later: https://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys#h=select%20Rate%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22nzDgbp%22%29
"https://query.yahooapis.com/v1/public/yql?q=select%20Rate%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22" + from + to + "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=callBackFunctionName"
Laurel
  • 5,965
  • 14
  • 31
  • 57
Redzarf
  • 2,578
  • 4
  • 30
  • 40
  • why can't you use eval? (it's pretty widely supported) – dandavis Nov 05 '13 at 17:10
  • @dandavis The calculator also runs in a Chrome Extension which has a tight security policy that blocks eval(). It's possible to disable the restriction if there's no better option, but I'd rather stick with best-practices. http://developer.chrome.com/extensions/contentSecurityPolicy.html – Redzarf Nov 05 '13 at 18:32
  • Sure a plain Google search works, and html-scraping that may be my best option, but it's bound to break again regularly :-( – Redzarf Nov 05 '13 at 20:46

1 Answers1

2

You can use the math.js library:

http://mathjs.org/

It supports units and has a powerful expression parser.

Jos de Jong
  • 6,602
  • 3
  • 38
  • 58