(input: unknown, outputs: Record<string, Record<string, unknown>>)
| 17 | } |
| 18 | |
| 19 | export function parseInput(input: unknown, outputs: Record<string, Record<string, unknown>>): unknown { |
| 20 | const variableRegex = /{{\s*([^}]+)\s*}}/ |
| 21 | |
| 22 | if (!input) { |
| 23 | return input |
| 24 | } |
| 25 | if (typeof input === 'string') { |
| 26 | const variables = [...input.matchAll(new RegExp(variableRegex, 'g'))] |
| 27 | |
| 28 | // if the entire input is just one variable, then just return its value |
| 29 | if (variables.length === 1 && input.trim() === variables[0][0]) { |
| 30 | return calculateExpression(variables[0][1].trim(), outputs) |
| 31 | } |
| 32 | |
| 33 | // replace all variables by its value |
| 34 | return variables.reduce((prev, variableMatch) => { |
| 35 | return prev.replace(variableMatch[0], stringifyInput(calculateExpression(variableMatch[1].trim(), outputs))) |
| 36 | }, input) |
| 37 | } |
| 38 | if (Array.isArray(input)) { |
| 39 | return input.map((x) => parseInput(x, outputs)) |
| 40 | } |
| 41 | if (input && typeof input === 'object') { |
| 42 | // Don't send empty objects, external integrations might fail validation because of them |
| 43 | if (isEmptyObj(input)) { |
| 44 | return |
| 45 | } |
| 46 | return Object.entries(input).reduce((prev: Record<string, unknown>, [key, value]) => { |
| 47 | prev[key] = parseInput(value, outputs) |
| 48 | return prev |
| 49 | }, {}) |
| 50 | } |
| 51 | return input |
| 52 | } |
| 53 | |
| 54 | // Your custom handler for undefined functions |
| 55 | const customFunctionHandler = (name, args) => { |
no test coverage detected