Files
FastGPT/packages/plugins/src/mathExprVal/index.ts
Archer e99c91aaa6 Perf system plugin and worker (#2126)
* perf: worker pool

* perf: worker register

* perf: worker controller

* perf: system plugin worker

* perf: system plugin worker

* perf: worker

* perf: worker

* worker timeout

* perf: copy icon
2024-07-23 11:23:42 +08:00

40 lines
744 B
TypeScript

import { Parser } from 'expr-eval';
type Props = {
expr: string;
};
// Response type same as HTTP outputs
type Response = Promise<{
result: string;
}>;
const replaceSpecialChar = (expr: string) => {
// replace ** to ^
let result = expr.replace(/\*\*/g, '^');
return result;
};
const main = async ({ expr }: Props): Response => {
if (typeof expr !== 'string') {
return {
result: `${expr} is not a string`
};
}
try {
const parser = new Parser();
const exprParser = parser.parse(replaceSpecialChar(expr));
return {
result: exprParser.evaluate()
};
} catch (error) {
return {
result: `${expr} is not a valid math expression. Error: ${error}`
};
}
};
export default main;