fix: Fix mapping paths when appending to empty expression (#5591)

* fix: Fix mapping when appending to empty expression

* fix: refactor logic out

* test: add tests

* test: add tests

* fix: fix bug where value does not get updated when mapping

* test: add test for bug

* test: add test for bug
This commit is contained in:
Mutasem Aldmour
2023-03-02 15:02:29 +03:00
committed by GitHub
parent 31cc8de829
commit 1f7b478920
9 changed files with 294 additions and 33 deletions

View File

@@ -1,3 +1,5 @@
import { INodeProperties, isResourceLocatorValue, NodeParameterValueType } from 'n8n-workflow';
export function generatePath(root: string, path: Array<string | number>): string {
return path.reduce((accu: string, part: string | number) => {
if (typeof part === 'number') {
@@ -29,3 +31,39 @@ export function getMappedExpression({
return `{{ ${generatePath(root, path)} }}`;
}
export function getMappedResult(
parameter: INodeProperties,
newParamValue: string,
prevParamValue: NodeParameterValueType,
): string {
const useDataPath = !!parameter.requiresDataPath && newParamValue.startsWith('{{ $json'); // ignore when mapping from grand-parent-node
const prevValue =
parameter.type === 'resourceLocator' && isResourceLocatorValue(prevParamValue)
? prevParamValue.value
: prevParamValue;
if (useDataPath) {
const newValue = newParamValue
.replace('{{ $json', '')
.replace(new RegExp('^\\.'), '')
.replace(new RegExp('}}$'), '')
.trim();
if (prevValue && parameter.requiresDataPath === 'multiple') {
if (typeof prevValue === 'string' && prevValue.trim() === '=') {
return newValue;
} else {
return `${prevValue}, ${newValue}`;
}
} else {
return newValue;
}
} else if (typeof prevValue === 'string' && prevValue.startsWith('=') && prevValue.length > 1) {
return `${prevValue} ${newParamValue}`;
} else if (prevValue && ['string', 'json'].includes(parameter.type)) {
return prevValue === '=' ? `=${newParamValue}` : `=${prevValue} ${newParamValue}`;
}
return `=${newParamValue}`;
}