A simple JavaScript calculator.
Calc implements UMD so you can load it up via CommonJS and AMD module loaders as well as the trusty <script> tag. For example, in Node:
let calc = require('calc');
The calc object has two functions attached to it:
compute: Computes the value of the string expression passed to it.createComputer: Returns a function that can be used to compute the string expression passed to it.
compute works like this:
let value = calc.compute('1 + 2');
console.log(value); // 3
compute supports the following mathematical operators with the correct order of operations:
-
-
-
- /
- %
- ()
createComputer works like this:
let obj = { a: 1, b: 2 };
let computeValue = calc.createComputer(obj, 'a + b');
let value = computeValue();
console.log(value); // 3
Yes, we just took a two line example and made it four lines. There is a reason for this:
// Continuing from the `createComputer` example above
obj.a = 2;
value = computeValue();
console.log(value); // 4
createComputer supports all the same operators as compute.
Development requires Node and npm. Once you have those installed grab all the development dependencies with:
npm install
Once you've completed that there are three simple commands:
npm test: run all the tests.npm run lint: lint the source code and test code.npm run doc: build the jsdoc documentation.
If you want to contribute to calc.js you must:
- Write code.
- Lint your code. Repeat step 1 as needed.
- Document your code.
- Write tests for your code that your code passes.
- Submit a pull request.
Licensed under the MIT license. See LICENSE.md for more details.