@nocobase/evaluators 0.19.0-alpha.8 → 0.20.0-alpha.1
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.
- package/lib/client/engines/formulajs.js +1 -2
- package/lib/client/engines/mathjs.js +2 -3
- package/lib/client/engines/string.d.ts +6 -0
- package/lib/client/engines/string.js +38 -0
- package/lib/client/index.js +2 -0
- package/lib/server/index.js +4 -3
- package/lib/utils/formulajs.d.ts +2 -1
- package/lib/utils/formulajs.js +3 -4
- package/lib/utils/index.d.ts +5 -1
- package/lib/utils/index.js +11 -8
- package/lib/utils/mathjs.d.ts +2 -1
- package/lib/utils/mathjs.js +13 -11
- package/lib/utils/string.d.ts +2 -0
- package/lib/utils/string.js +29 -0
- package/package.json +3 -3
|
@@ -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:
|
|
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:
|
|
38
|
+
evaluate: import_mathjs.default
|
|
40
39
|
};
|
|
@@ -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
|
+
};
|
package/lib/client/index.js
CHANGED
|
@@ -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 }),
|
package/lib/server/index.js
CHANGED
|
@@ -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",
|
|
44
|
-
evaluators.register("formula.js",
|
|
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 = {
|
package/lib/utils/formulajs.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
declare const _default: any;
|
|
2
|
+
export default _default;
|
package/lib/utils/formulajs.js
CHANGED
|
@@ -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
|
-
|
|
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
|
+
}, {});
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -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
|
-
|
|
6
|
+
interface EvaluatorOptions {
|
|
7
|
+
replaceValue?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function evaluate(this: Evaluator, options: EvaluatorOptions, expression: string, scope?: Scope): any;
|
|
10
|
+
export {};
|
package/lib/utils/index.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
49
|
-
if (
|
|
50
|
-
|
|
50
|
+
let key = keyMap[v];
|
|
51
|
+
if (!key) {
|
|
52
|
+
key = `$$${index++}`;
|
|
53
|
+
keyMap[v] = key;
|
|
54
|
+
newContext[key] = item;
|
|
51
55
|
}
|
|
52
|
-
|
|
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
|
}
|
package/lib/utils/mathjs.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
declare const _default: any;
|
|
2
|
+
export default _default;
|
package/lib/utils/mathjs.js
CHANGED
|
@@ -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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
__name(mathjs_default, "default");
|
|
44
|
+
return result;
|
|
45
|
+
},
|
|
46
|
+
{ replaceKey: true }
|
|
47
|
+
);
|
|
@@ -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.
|
|
3
|
+
"version": "0.20.0-alpha.1",
|
|
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.
|
|
10
|
+
"@nocobase/utils": "0.20.0-alpha.1",
|
|
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": "
|
|
18
|
+
"gitHead": "516c0b1470d77ff499f533ae6b57e4c084450ff5"
|
|
19
19
|
}
|