https://github.com/qxip/pipemath
TinyMath fork spooning with pipeLion
https://github.com/qxip/pipemath
Last synced: about 2 months ago
JSON representation
TinyMath fork spooning with pipeLion
- Host: GitHub
- URL: https://github.com/qxip/pipemath
- Owner: QXIP
- License: apache-2.0
- Created: 2021-06-07T16:47:10.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-06-07T16:52:31.000Z (about 5 years ago)
- Last Synced: 2024-05-01T11:38:01.440Z (about 2 years ago)
- Language: JavaScript
- Size: 149 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pipeMath: Tinymath fork spooning with pipeLion
[](https://raw.githubusercontent.com/qxip/pipemath/master/LICENSE)
[](https://www.npmjs.com/package/pipemath)
Tinymath is a tiny arithmetic and function evaluator for simple numbers and arrays. Named properties can be accessed from an optional scope parameter and new functions can be added without rebuilding. Enjoy.
**NOTE:** Tinymath requires an ES6 or newer environment. You can use it with your build system of choice to run in older environments.
See [Function Documentation](/docs/functions.md) for details on built-in functions available in Tinymath.
```javascript
import { evaluate } from tinymath
// Simple math
evaluate('10 + 20'); // 30
evaluate('round(3.141592)') // 3
// Named properties
evaluate('foo + 20', {foo: 5}); // 25
// Arrays
evaluate('bar + 20', {bar: [1, 2, 3]}); // [21, 22, 23]
evaluate('bar + baz', {bar: [1, 2, 3], baz: [4, 5, 6]}); // [5, 7, 9]
evaluate('multiply(bar, baz) / 10', {bar: [1, 2, 3], baz: [4, 5, 6]}); // [0.4, 1, 1.8]
```
### Adding Functions
Functions can be injected, and built in function overwritten, via the 3rd argument to `evaluate`:
```javascript
import { evaluate } from tinymath
evaluate('plustwo(foo)', {foo: 5}, {
plustwo: function(a) {
return a + 2;
}
}); // 7
```
### Parsing
You can get to the parsed AST by importing `parse`
```javascript
import { parse } from tinymath
parse('1 + random()')
/*
{
"name": "add",
"args": [
1,
{
"name": "random",
"args": []
}
]
}
*/
```
#### Notes
* Floating point operations have the normal Javascript limitations