kibana/packages/kbn-tinymath
Spencer 32491462a9
add kibana.jsonc files to existing packages (#138965)
* [packages] add kibana.jsonc files

* auto-migrate to kibana.jsonc

* support interactive pkg id selection too

* remove old codeowners entry

* skip codeowners generation when .github/CODEOWNERS doesn't exist

* fall back to format validation if user is offline

* update question style

* [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix'

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2022-09-08 13:31:57 -07:00
..
docs Tinymath is now a Kibana package (#89383) 2021-01-28 13:58:37 -05:00
grammar [Lens] Escape field names in formula (#102588) 2021-06-23 14:20:50 -04:00
src chore(NA): eslint rule for disallowing naked eslint-disable (#136408) 2022-07-19 17:11:04 +01:00
test fix all violations 2022-04-16 01:37:30 -05:00
BUILD.bazel [eslint] ensure that all imports are resolvable (#129002) 2022-04-04 15:37:06 -04:00
index.d.ts [Lens] Formula editor (#99297) 2021-06-10 10:09:16 -04:00
jest.config.js Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
kibana.jsonc add kibana.jsonc files to existing packages (#138965) 2022-09-08 13:31:57 -07:00
package.json chore(NA): moving @kbn/tinymath into bazel (#97022) 2021-04-14 14:49:19 -04:00
README.md Tinymath is now a Kibana package (#89383) 2021-01-28 13:58:37 -05:00
tsconfig.json [build_ts_refs] improve caches, allow building a subset of projects (#107981) 2021-08-10 22:12:45 -07:00

kbn-tinymath

kbn-tinymath is a tiny arithmetic and function evaluator for simple numbers and arrays. Named properties can be accessed from an optional scope parameter. It's available as an expression function called math in Canvas, and the grammar/AST structure is available for use by Kibana plugins that want to use math.

See Function Documentation for details on built-in functions available in Tinymath.

const { evaluate } = require('@kbn/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:

const { evaluate } = require('@kbn/tinymath');

evaluate('plustwo(foo)', {foo: 5}, {
    plustwo: function(a) {
        return a + 2;
    }
}); // 7

Parsing

You can get to the parsed AST by importing parse

const { parse } = require('@kbn/tinymath');

parse('1 + random()')
/*
{
   "name": "add",
   "args": [
      1,
      {
         "name": "random",
         "args": []
      }
   ]
}
*/

Notes

  • Floating point operations have the normal Javascript limitations

Building kbn-tinymath

This package is rebuilt when running yarn kbn bootstrap, but can also be build directly using yarn build from the packages/kbn-tinymath directory.

Running tests

To test @kbn/tinymath from Kibana, run yarn run jest --watch packages/kbn-tinymath from the top level of Kibana.