mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
32 lines
731 B
TypeScript
32 lines
731 B
TypeScript
|
export function generatePath(root: string, path: Array<string | number>): string {
|
||
|
return path.reduce((accu: string, part: string | number) => {
|
||
|
if (typeof part === 'number') {
|
||
|
return `${accu}[${part}]`;
|
||
|
}
|
||
|
|
||
|
if (part.includes(' ') || part.includes('.')) {
|
||
|
return `${accu}["${part}"]`;
|
||
|
}
|
||
|
|
||
|
return `${accu}.${part}`;
|
||
|
}, root);
|
||
|
}
|
||
|
|
||
|
export function getMappedExpression({
|
||
|
nodeName,
|
||
|
distanceFromActive,
|
||
|
path,
|
||
|
}: {
|
||
|
nodeName: string;
|
||
|
distanceFromActive: number;
|
||
|
path: Array<string | number> | string;
|
||
|
}) {
|
||
|
const root = distanceFromActive === 1 ? '$json' : generatePath('$node', [nodeName, 'json']);
|
||
|
|
||
|
if (typeof path === 'string') {
|
||
|
return `{{ ${root}${path} }}`;
|
||
|
}
|
||
|
|
||
|
return `{{ ${generatePath(root, path)} }}`;
|
||
|
}
|