@nocobase/evaluators 0.9.2-alpha.1 → 0.9.2-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
10
10
  var _default = {
11
11
  label: 'Math.js',
12
12
  tooltip: `{{t('Math.js comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types')}}`,
13
- link: "https://mathjs.org/",
13
+ link: 'https://mathjs.org/',
14
14
  evaluate: _utils.evaluate.bind(_mathjs.default)
15
15
  };
16
16
  exports.default = _default;
@@ -1,4 +1,4 @@
1
- import { Registry } from "@nocobase/utils";
1
+ import { Registry } from '@nocobase/utils';
2
2
  import { Evaluator } from '../utils';
3
3
  export { Evaluator, evaluate, appendArrayColumn } from '../utils';
4
4
  export declare const evaluators: Registry<Evaluator>;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/evaluators",
3
- "version": "0.9.2-alpha.1",
3
+ "version": "0.9.2-alpha.3",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
7
  "license": "Apache-2.0",
8
8
  "dependencies": {
9
9
  "@formulajs/formulajs": "4.2.0",
10
- "@nocobase/utils": "0.9.2-alpha.1",
10
+ "@nocobase/utils": "0.9.2-alpha.3",
11
11
  "mathjs": "^10.6.0"
12
12
  },
13
13
  "repository": {
@@ -15,5 +15,5 @@
15
15
  "url": "git+https://github.com/nocobase/nocobase.git",
16
16
  "directory": "packages/evaluators"
17
17
  },
18
- "gitHead": "a4f103eb0144f07205aff7b2a6be8ea6aa1330df"
18
+ "gitHead": "b6b5f9372202d942c97d2d90a4197e060db05124"
19
19
  }
@@ -1,11 +1,9 @@
1
1
  import { evaluate } from '../../utils';
2
2
  import formulajs from '../../utils/formulajs';
3
3
 
4
-
5
-
6
4
  export default {
7
5
  label: 'Formula.js',
8
6
  tooltip: '{{t("Formula.js supports most Microsoft Excel formula functions.")}}',
9
7
  link: 'https://formulajs.info/functions/',
10
- evaluate: evaluate.bind(formulajs)
8
+ evaluate: evaluate.bind(formulajs),
11
9
  };
@@ -1,11 +1,9 @@
1
- import { evaluate } from "../../utils";
2
- import mathjs from "../../utils/mathjs";
3
-
4
-
1
+ import { evaluate } from '../../utils';
2
+ import mathjs from '../../utils/mathjs';
5
3
 
6
4
  export default {
7
5
  label: 'Math.js',
8
6
  tooltip: `{{t('Math.js comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types')}}`,
9
- link: "https://mathjs.org/",
10
- evaluate: evaluate.bind(mathjs)
7
+ link: 'https://mathjs.org/',
8
+ evaluate: evaluate.bind(mathjs),
11
9
  };
@@ -16,7 +16,10 @@ evaluators.register('math.js', mathjs);
16
16
  evaluators.register('formula.js', formulajs);
17
17
 
18
18
  export function getOptions() {
19
- return Array.from((evaluators as Registry<Evaluator>).getEntities()).reduce((result: any[], [value, options]) => result.concat({ value, ...options }), []);
19
+ return Array.from((evaluators as Registry<Evaluator>).getEntities()).reduce(
20
+ (result: any[], [value, options]) => result.concat({ value, ...options }),
21
+ [],
22
+ );
20
23
  }
21
24
 
22
25
  export default evaluators;
@@ -3,8 +3,6 @@ import evaluators from '..';
3
3
  const mathEval = evaluators.get('math.js');
4
4
  const formulaEval = evaluators.get('formula.js');
5
5
 
6
-
7
-
8
6
  describe('evaluate', () => {
9
7
  it('reference null or undefined', () => {
10
8
  const result = formulaEval('{{a.b}}', { a: null });
@@ -12,12 +10,20 @@ describe('evaluate', () => {
12
10
  });
13
11
 
14
12
  it('function result string with quote', () => {
15
- const result = formulaEval('{{a}}', { a() { return "I'm done." } });
13
+ const result = formulaEval('{{a}}', {
14
+ a() {
15
+ return "I'm done.";
16
+ },
17
+ });
16
18
  expect(result).toBe("I'm done.");
17
19
  });
18
20
 
19
21
  it('function result number', () => {
20
- const result = formulaEval('{{a}}', { a() { return 1 } });
22
+ const result = formulaEval('{{a}}', {
23
+ a() {
24
+ return 1;
25
+ },
26
+ });
21
27
  expect(result).toBe(1);
22
28
  });
23
29
 
@@ -1,10 +1,8 @@
1
- import { Registry } from "@nocobase/utils";
1
+ import { Registry } from '@nocobase/utils';
2
2
 
3
3
  import { evaluate, Evaluator } from '../utils';
4
- import mathjs from "../utils/mathjs";
5
- import formulajs from "../utils/formulajs";
6
-
7
-
4
+ import mathjs from '../utils/mathjs';
5
+ import formulajs from '../utils/formulajs';
8
6
 
9
7
  export { Evaluator, evaluate, appendArrayColumn } from '../utils';
10
8
 
@@ -1,11 +1,9 @@
1
1
  import * as functions from '@formulajs/formulajs';
2
2
 
3
+ const fnNames = Object.keys(functions).filter((key) => key !== 'default');
4
+ const fns = fnNames.map((key) => functions[key]);
3
5
 
4
-
5
- const fnNames = Object.keys(functions).filter(key => key !== 'default');
6
- const fns = fnNames.map(key => functions[key]);
7
-
8
- export default function(expression: string, scope = {}) {
6
+ export default function (expression: string, scope = {}) {
9
7
  const fn = new Function(...fnNames, ...Object.keys(scope), `return ${expression}`);
10
8
  const result = fn(...fns, ...Object.values(scope));
11
9
  if (typeof result === 'number') {
@@ -1,6 +1,4 @@
1
- import { get, cloneDeep } from "lodash";
2
-
3
-
1
+ import { get, cloneDeep } from 'lodash';
4
2
 
5
3
  export type Scope = { [key: string]: any };
6
4
 
@@ -13,7 +11,7 @@ export function appendArrayColumn(scope, key) {
13
11
  const path = paths[p];
14
12
  const isIndex = path.match(/^\d+$/);
15
13
  if (Array.isArray(data) && !isIndex && !data[path]) {
16
- data[path] = data.map(item => item[path]);
14
+ data[path] = data.map((item) => item[path]);
17
15
  }
18
16
  data = data?.[path];
19
17
  }