(
func: any,
formData = {},
parentPath: string | []
)
| 65 | }; |
| 66 | |
| 67 | export 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 | |
| 101 | export function getRealDataPath(path) { |
| 102 | if (typeof path !== 'string') { |
no test coverage detected