# tinymath

> A tiny math expression library with only the stuff I personally care about. Deal with it.

Latest version **1.2.1** (published 2019-01-16) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install tinymath
pnpm add tinymath
yarn add tinymath
bun add tinymath
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2019-01-16 |
| First published | 2017-12-17 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 689.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 22 |
| Author | Rashid Khan |
| Maintainers | cqliu1, rashidkpc, w33ble |

## Links

- npm: https://www.npmjs.com/package/tinymath
- Repository: https://github.com/elastic/tinymath
- Issues: https://github.com/elastic/tinymath
- npm.io page: https://npm.io/package/tinymath

## Recent versions

- 1.2.1 (latest) — 2019-01-16
- 0.2.1 (next) — 2018-03-04
- 1.2.0 — 2019-01-16
- 1.1.1 — 2018-12-04
- 1.1.0 — 2018-10-25
- 1.0.0 — 2018-10-24
- 0.5.0 — 2018-03-29
- 0.4.0 — 2018-03-27
- 0.3.1 — 2018-03-26
- 0.3.0 — 2018-03-26
- 0.2.0 — 2018-02-22
- 0.1.12 — 2018-02-22
- 0.1.11 — 2018-02-20
- 0.1.10 — 2018-02-16
- 0.1.9 — 2018-02-12
- … 10 more at https://npm.io/package/tinymath/versions

## README

# Tinymath

[![Apache License](https://img.shields.io/badge/license-apache_2.0-a9215a.svg)](https://raw.githubusercontent.com/elastic/tinymath/master/LICENSE)
[![Build Status](https://img.shields.io/travis/elastic/tinymath.svg?branch=master)](https://travis-ci.org/elastic/tinymath)
[![npm](https://img.shields.io/npm/v/tinymath.svg)](https://www.npmjs.com/package/tinymath)

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, or use the ES5 compatible version by importing `tinymath/lib/tinymath.es5.js`.

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

---
_Source: https://npm.io/package/tinymath · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
