kibana/packages/kbn-tinymath
Kibana Machine af68e95d1e
[8.9] Remove eslint-plugin-prefer-object-spread dependency (#162439) (#162455)
# Backport

This will backport the following commits from `main` to `8.9`:
- [Remove eslint-plugin-prefer-object-spread dependency
(#162439)](https://github.com/elastic/kibana/pull/162439)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Thomas
Watson","email":"watson@elastic.co"},"sourceCommit":{"committedDate":"2023-07-25T08:04:44Z","message":"Remove
eslint-plugin-prefer-object-spread dependency (#162439)\n\nRemove the
`eslint-plugin-prefer-object-spread` dependency as
the\r\n`prefer-object-spread` rule has been part of ESLint since
v5","sha":"a79e9c737498bc3f3bda3d82ef1b3387be88b9ed","branchLabelMapping":{"^v8.10.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Operations","release_note:skip","v7.17.10","v8.10.0","v7.17.12"],"number":162439,"url":"https://github.com/elastic/kibana/pull/162439","mergeCommit":{"message":"Remove
eslint-plugin-prefer-object-spread dependency (#162439)\n\nRemove the
`eslint-plugin-prefer-object-spread` dependency as
the\r\n`prefer-object-spread` rule has been part of ESLint since
v5","sha":"a79e9c737498bc3f3bda3d82ef1b3387be88b9ed"}},"sourceBranch":"main","suggestedTargetBranches":["7.17"],"targetPullRequestStates":[{"branch":"7.17","label":"v7.17.10","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.10.0","labelRegex":"^v8.10.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/162439","number":162439,"mergeCommit":{"message":"Remove
eslint-plugin-prefer-object-spread dependency (#162439)\n\nRemove the
`eslint-plugin-prefer-object-spread` dependency as
the\r\n`prefer-object-spread` rule has been part of ESLint since
v5","sha":"a79e9c737498bc3f3bda3d82ef1b3387be88b9ed"}}]}] BACKPORT-->

Co-authored-by: Thomas Watson <watson@elastic.co>
2023-07-25 02:22:26 -07:00
..
docs [Lens] Add conditional operations in Formula (#142325) 2022-10-12 12:16:58 +02:00
src [8.9] Remove eslint-plugin-prefer-object-spread dependency (#162439) (#162455) 2023-07-25 02:22:26 -07:00
test Transpile packages on demand, validate all TS projects (#146212) 2022-12-22 19:00:29 -06: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 Transpile packages on demand, validate all TS projects (#146212) 2022-12-22 19:00:29 -06:00
package.json [packages] migrate all plugins to packages (#148130) 2023-02-08 21:06:50 -06:00
README.md [Lens] Add conditional operations in Formula (#142325) 2022-10-12 12:16:58 +02:00
tsconfig.json Transpile packages on demand, validate all TS projects (#146212) 2022-12-22 19:00:29 -06: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 node scripts/jest --config packages/kbn-tinymath/jest.config.js from the top level of Kibana.

To test grammar changes it is required to run a build task before the test suite.