arithmetic-js

2.0.0 • Public • Published

arithmetic-js

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.

differences between eval() and arithmetic-js

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

Package Sidebar

Install

npm i arithmetic-js

Weekly Downloads

2

Version

2.0.0

License

MIT

Last publish

Collaborators

  • steckel