MCPcopy
hub / github.com/alibaba/x-render / parseExpression

Function parseExpression

packages/form-render/src/models/expression.ts:67–99  ·  view source on GitHub ↗
(
  func: any,
  formData = {},
  parentPath: string | []
)

Source from the content-addressed store, hash-verified

65};
66
67export const parseExpression = (
68 func: any,
69 formData = {},
70 parentPath: string | []
71) => {
72 const parentData = get(formData, parentPath) || {};
73
74 if (typeof func === 'string') {
75 const funcBody = func
76 .replace(/^{\s*{/g, '')
77 .replace(/}\s*}$/g, '')
78 .trim();
79 let isHandleData =
80 funcBody?.startsWith('formData') || funcBody?.startsWith('rootValue');
81
82 let funcBodyStr = isHandleData ? parseFunc(funcBody) : funcBody;
83
84 const funcStr = `
85 return ${funcBodyStr
86 .replace(/formData/g, JSON.stringify(formData))
87 .replace(/rootValue/g, JSON.stringify(parentData))}
88 `;
89 try {
90 const result = Function(funcStr)();
91 return result;
92 } catch (error) {
93 console.log(error, funcStr, parentPath);
94 return null; // 如果计算有错误,return null 最合适
95 }
96 }
97
98 return func;
99}
100
101export function getRealDataPath(path) {
102 if (typeof path !== 'string') {

Callers 3

getListEveryResultFunction · 0.90
recursionArrayFunction · 0.70
parseAllExpressionFunction · 0.70

Calls 1

parseFuncFunction · 0.85

Tested by

no test coverage detected