mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-20 11:22:15 +00:00
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:
@@ -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}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user