n8n/packages/editor-ui/src/utils/mappingUtils.ts
Mutasem Aldmour 6092f6c41e
feat(editor): Add mapping support for data paths (#5191)
* feat: add data path flag

* chore: update types

* feat: use path for data

* feat: add support for multiple values

* fix: handle if not prev node

* fix: update node

* fix: handle multi part path

* feat: add support for multiple vals for field

* feat: add support for table transforms

* feat: use dot notation

* feat: fix bug where brackets removed

* fix: handle dots, fix unit tests

* test: update snapshot

* test: fix tests

* test: add test for edge case
2023-01-30 14:42:08 +03:00

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)} }}`;
}