Basic arithmetic without eval()
.
import arithmetic from "arithmetic-js"
arithmetic("100 * 13 + (75% - 0.05) * 10 + 30"); // 1337
arithmetic-js
was built primarily for use behind a proprietary
css-calc polyfill to
satisfy a Content Security Policy which
banned the use of eval()
.
It can be useful for evaluating basic arithmetic expressions from dynamic sources.
The primary difference between eval()
and arithmetic-js
is that
arithmetic-js
is not intended to execute arbitrary JavaScript.
Other than that, use of the %
is interpreted as a percentage operand and not
modulo.
arithmetic("100 * 13 + (75% - 0.05) * 10 + 30");
// 100 * 13 + (.75 - 0.05) * 10 + 30 === 1337
eval("100 * 13 + (75% - 0.05) * 10 + 30");
// 100 * 13 + (75 % -0.05) * 10 + 30 === 1330.5