exprify 1.0.4 → 1.0.7
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/HISTORY.md +49 -0
- package/README.md +109 -182
- package/SECURITY.md +18 -0
- package/bin/cli.mjs +234 -0
- package/dist/exprify.cjs.cjs +3558 -1220
- package/dist/exprify.cjs.cjs.map +1 -1
- package/dist/exprify.esm.js +3558 -1220
- package/dist/exprify.esm.js.map +1 -1
- package/dist/exprify.js +3560 -1222
- package/dist/exprify.js.map +1 -1
- package/dist/exprify.min.js +2 -2
- package/dist/exprify.min.js.map +1 -1
- package/package.json +44 -17
- package/src/core/context.js +35 -27
- package/src/core/exprify.js +880 -0
- package/src/function/executor.js +29 -20
- package/src/function/internal.js +1150 -153
- package/src/function/registry.js +23 -16
- package/src/index.js +1 -1
- package/src/math/bignumber.js +31 -0
- package/src/math/fraction.js +112 -0
- package/src/math/operations.js +38 -24
- package/src/parser/astBuild.js +276 -214
- package/src/parser/evaluator.js +431 -171
- package/src/parser/tokenizer.js +179 -146
- package/src/utils/decimal.js +264 -0
- package/src/utils/globalUnits.js +43 -35
- package/src/utils/matrix.js +14 -14
- package/src/utils/store.js +69 -47
- package/src/variables/store.js +18 -15
- package/src/core/Exprify.js +0 -369
package/src/function/executor.js
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {{ get: (arg0: any) => any; has: (arg0: any) => any; }} fnRegistry
|
|
3
|
+
*/
|
|
1
4
|
export function createFunctionExecutor(fnRegistry, options = {}) {
|
|
2
5
|
if (!fnRegistry) {
|
|
3
|
-
throw new Error(
|
|
6
|
+
throw new Error('Function registry is required');
|
|
4
7
|
}
|
|
5
8
|
|
|
6
9
|
const config = {
|
|
7
|
-
strict: options.strict ?? true
|
|
10
|
+
strict: options.strict ?? true,
|
|
8
11
|
};
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
/**
|
|
14
|
+
* @param {any} name
|
|
15
|
+
* @param {any} args
|
|
16
|
+
* @param {any} _context
|
|
17
|
+
*/
|
|
18
|
+
function execute(name, args = [], _context) {
|
|
13
19
|
const fn = fnRegistry.get(name);
|
|
14
20
|
|
|
15
|
-
|
|
21
|
+
// not found
|
|
16
22
|
if (!fn) {
|
|
17
23
|
if (config.strict) {
|
|
18
24
|
throw new Error(`Unknown function: ${name}`);
|
|
@@ -20,45 +26,48 @@ export function createFunctionExecutor(fnRegistry, options = {}) {
|
|
|
20
26
|
return undefined;
|
|
21
27
|
}
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
// validate args
|
|
24
30
|
if (!Array.isArray(args)) {
|
|
25
31
|
throw new Error(`Arguments for "${name}" must be an array`);
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
// execute
|
|
29
35
|
try {
|
|
30
36
|
return fn(...args);
|
|
31
37
|
} catch (err) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
);
|
|
38
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
39
|
+
throw new Error(`Error in function "${name}": ${msg}`, { cause: err });
|
|
35
40
|
}
|
|
36
41
|
}
|
|
37
42
|
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
/**
|
|
44
|
+
* @param {any} name
|
|
45
|
+
* @param {any} args
|
|
46
|
+
* @param {any} context
|
|
47
|
+
*/
|
|
40
48
|
function safeExecute(name, args = [], context) {
|
|
41
49
|
try {
|
|
42
50
|
return execute(name, args, context);
|
|
43
51
|
} catch (err) {
|
|
52
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
44
53
|
return {
|
|
45
54
|
error: true,
|
|
46
|
-
message:
|
|
55
|
+
message: msg,
|
|
47
56
|
};
|
|
48
57
|
}
|
|
49
58
|
}
|
|
50
59
|
|
|
51
|
-
|
|
52
|
-
|
|
60
|
+
/**
|
|
61
|
+
* @param {any} name
|
|
62
|
+
*/
|
|
53
63
|
function exists(name) {
|
|
54
64
|
return fnRegistry.has(name);
|
|
55
65
|
}
|
|
56
66
|
|
|
57
|
-
|
|
58
|
-
|
|
67
|
+
// API
|
|
59
68
|
return {
|
|
60
69
|
execute,
|
|
61
70
|
safeExecute,
|
|
62
|
-
exists
|
|
71
|
+
exists,
|
|
63
72
|
};
|
|
64
|
-
}
|
|
73
|
+
}
|