@nocobase/evaluators 0.19.0-alpha.7 → 0.19.0-alpha.9

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.
@@ -30,11 +30,10 @@ __export(formulajs_exports, {
30
30
  default: () => formulajs_default
31
31
  });
32
32
  module.exports = __toCommonJS(formulajs_exports);
33
- var import_utils = require("../../utils");
34
33
  var import_formulajs = __toESM(require("../../utils/formulajs"));
35
34
  var formulajs_default = {
36
35
  label: "Formula.js",
37
36
  tooltip: '{{t("Formula.js supports most Microsoft Excel formula functions.")}}',
38
37
  link: "https://formulajs.info/functions/",
39
- evaluate: import_utils.evaluate.bind(import_formulajs.default)
38
+ evaluate: import_formulajs.default
40
39
  };
@@ -30,11 +30,10 @@ __export(mathjs_exports, {
30
30
  default: () => mathjs_default
31
31
  });
32
32
  module.exports = __toCommonJS(mathjs_exports);
33
- var import_utils = require("../../utils");
34
33
  var import_mathjs = __toESM(require("../../utils/mathjs"));
35
34
  var mathjs_default = {
36
35
  label: "Math.js",
37
- 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')}}`,
36
+ 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.')}}`,
38
37
  link: "https://mathjs.org/",
39
- evaluate: import_utils.evaluate.bind(import_mathjs.default)
38
+ evaluate: import_mathjs.default
40
39
  };
@@ -0,0 +1,6 @@
1
+ declare const _default: {
2
+ label: string;
3
+ tooltip: string;
4
+ evaluate: any;
5
+ };
6
+ export default _default;
@@ -0,0 +1,38 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var string_exports = {};
29
+ __export(string_exports, {
30
+ default: () => string_default
31
+ });
32
+ module.exports = __toCommonJS(string_exports);
33
+ var import_string = __toESM(require("../../utils/string"));
34
+ var string_default = {
35
+ label: `{{t('String template')}}`,
36
+ tooltip: `{{t('Simple string replacement, can be used to interpolate variables in a string.')}}`,
37
+ evaluate: import_string.default
38
+ };
@@ -36,9 +36,11 @@ module.exports = __toCommonJS(client_exports);
36
36
  var import_client = require("@nocobase/utils/client");
37
37
  var import_mathjs = __toESM(require("./engines/mathjs"));
38
38
  var import_formulajs = __toESM(require("./engines/formulajs"));
39
+ var import_string = __toESM(require("./engines/string"));
39
40
  const evaluators = new import_client.Registry();
40
41
  evaluators.register("math.js", import_mathjs.default);
41
42
  evaluators.register("formula.js", import_formulajs.default);
43
+ evaluators.register("string", import_string.default);
42
44
  function getOptions() {
43
45
  return Array.from(evaluators.getEntities()).reduce(
44
46
  (result, [value, options]) => result.concat({ value, ...options }),
@@ -35,13 +35,14 @@ __export(server_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(server_exports);
37
37
  var import_utils = require("@nocobase/utils");
38
- var import_utils2 = require("../utils");
39
38
  var import_mathjs = __toESM(require("../utils/mathjs"));
40
39
  var import_formulajs = __toESM(require("../utils/formulajs"));
40
+ var import_string = __toESM(require("../utils/string"));
41
41
  var import_utils3 = require("../utils");
42
42
  const evaluators = new import_utils.Registry();
43
- evaluators.register("math.js", import_utils2.evaluate.bind(import_mathjs.default));
44
- evaluators.register("formula.js", import_utils2.evaluate.bind(import_formulajs.default));
43
+ evaluators.register("math.js", import_mathjs.default);
44
+ evaluators.register("formula.js", import_formulajs.default);
45
+ evaluators.register("string", import_string.default);
45
46
  var server_default = evaluators;
46
47
  // Annotate the CommonJS export names for ESM import in node:
47
48
  0 && (module.exports = {
@@ -1 +1,2 @@
1
- export default function (expression: string, scope?: {}): any;
1
+ declare const _default: any;
2
+ export default _default;
@@ -4,7 +4,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
7
  var __export = (target, all) => {
9
8
  for (var name in all)
10
9
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -32,9 +31,10 @@ __export(formulajs_exports, {
32
31
  });
33
32
  module.exports = __toCommonJS(formulajs_exports);
34
33
  var functions = __toESM(require("@formulajs/formulajs"));
34
+ var import__ = require(".");
35
35
  const fnNames = Object.keys(functions).filter((key) => key !== "default");
36
36
  const fns = fnNames.map((key) => functions[key]);
37
- function formulajs_default(expression, scope = {}) {
37
+ var formulajs_default = import__.evaluate.bind(function(expression, scope = {}) {
38
38
  const fn = new Function(...fnNames, ...Object.keys(scope), `return ${expression}`);
39
39
  const result = fn(...fns, ...Object.values(scope));
40
40
  if (typeof result === "number") {
@@ -44,5 +44,4 @@ function formulajs_default(expression, scope = {}) {
44
44
  return functions.ROUND(result, 9);
45
45
  }
46
46
  return result;
47
- }
48
- __name(formulajs_default, "default");
47
+ }, {});
@@ -3,4 +3,8 @@ export type Scope = {
3
3
  };
4
4
  export type Evaluator = (expression: string, scope?: Scope) => any;
5
5
  export declare function appendArrayColumn(scope: any, key: any): void;
6
- export declare function evaluate(this: Evaluator, expression: string, scope?: Scope): any;
6
+ interface EvaluatorOptions {
7
+ replaceValue?: boolean;
8
+ }
9
+ export declare function evaluate(this: Evaluator, options: EvaluatorOptions, expression: string, scope?: Scope): any;
10
+ export {};
@@ -36,21 +36,24 @@ function appendArrayColumn(scope, key) {
36
36
  }
37
37
  }
38
38
  __name(appendArrayColumn, "appendArrayColumn");
39
- function evaluate(expression, scope = {}) {
39
+ function evaluate(options = {}, expression, scope = {}) {
40
40
  const context = (0, import_lodash.cloneDeep)(scope);
41
41
  const newContext = {};
42
- const exp = expression.trim().replace(/{{\s*([^{}]+)\s*}}/g, (_, v) => {
42
+ const keyMap = {};
43
+ let index = 0;
44
+ const exp = expression.trim().replace(/{{\s*([\w$.-]+)\s*}}/g, (_, v) => {
43
45
  appendArrayColumn(context, v);
44
- let item = (0, import_lodash.get)(context, v);
46
+ let item = (0, import_lodash.get)(context, v) ?? null;
45
47
  if (typeof item === "function") {
46
48
  item = item();
47
49
  }
48
- const randomKey = `$$${Math.random().toString(36).slice(2, 10).padEnd(8, "0")}`;
49
- if (item == null) {
50
- return "null";
50
+ let key = keyMap[v];
51
+ if (!key) {
52
+ key = `$$${index++}`;
53
+ keyMap[v] = key;
54
+ newContext[key] = item;
51
55
  }
52
- newContext[randomKey] = item;
53
- return ` ${randomKey} `;
56
+ return options.replaceValue ? `${item == null || typeof item === "number" && (Number.isNaN(item) || !Number.isFinite(item)) ? "" : item}` : key;
54
57
  });
55
58
  return this(exp, newContext);
56
59
  }
@@ -1 +1,2 @@
1
- export default function (expression: string, scope?: {}): any;
1
+ declare const _default: any;
2
+ export default _default;
@@ -4,7 +4,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
7
  var __export = (target, all) => {
9
8
  for (var name in all)
10
9
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -32,14 +31,17 @@ __export(mathjs_exports, {
32
31
  });
33
32
  module.exports = __toCommonJS(mathjs_exports);
34
33
  var math = __toESM(require("mathjs"));
35
- function mathjs_default(expression, scope = {}) {
36
- const result = math.evaluate(expression, scope);
37
- if (typeof result === "number") {
38
- if (Number.isNaN(result) || !Number.isFinite(result)) {
39
- return null;
34
+ var import__ = require(".");
35
+ var mathjs_default = import__.evaluate.bind(
36
+ function(expression, scope = {}) {
37
+ const result = math.evaluate(expression, scope);
38
+ if (typeof result === "number") {
39
+ if (Number.isNaN(result) || !Number.isFinite(result)) {
40
+ return null;
41
+ }
42
+ return math.round(result, 9);
40
43
  }
41
- return math.round(result, 9);
42
- }
43
- return result;
44
- }
45
- __name(mathjs_default, "default");
44
+ return result;
45
+ },
46
+ { replaceKey: true }
47
+ );
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,29 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var string_exports = {};
19
+ __export(string_exports, {
20
+ default: () => string_default
21
+ });
22
+ module.exports = __toCommonJS(string_exports);
23
+ var import__ = require(".");
24
+ var string_default = import__.evaluate.bind(
25
+ function(expression, scope = {}) {
26
+ return expression;
27
+ },
28
+ { replaceValue: true }
29
+ );
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/evaluators",
3
- "version": "0.19.0-alpha.7",
3
+ "version": "0.19.0-alpha.9",
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.19.0-alpha.7",
10
+ "@nocobase/utils": "0.19.0-alpha.9",
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": "cbbfd4de14b8f73417e613af6927d2fbbdbd2b41"
18
+ "gitHead": "975f9c58a1995df6a8d7a4d191a0dc4a9769ebc5"
19
19
  }