n8n/packages/editor-ui/src/components/AssignmentCollection/utils.ts
2024-11-18 12:41:59 +01:00

60 lines
1.7 KiB
TypeScript

import { isObject } from 'lodash-es';
import type { AssignmentValue, IDataObject } from 'n8n-workflow';
import { resolveParameter } from '@/composables/useWorkflowHelpers';
import { v4 as uuid } from 'uuid';
import { DateTime } from 'luxon';
export function inferAssignmentType(value: unknown): string {
if (typeof value === 'boolean') return 'boolean';
if (typeof value === 'number') return 'number';
if (typeof value === 'string' || DateTime.isDateTime(value) || value instanceof Date)
return 'string';
if (Array.isArray(value)) return 'array';
if (isObject(value)) return 'object';
return 'string';
}
export function typeFromExpression(expression: string): string {
try {
const resolved = resolveParameter(`=${expression}`);
return inferAssignmentType(resolved);
} catch (error) {
return 'string';
}
}
export function inputDataToAssignments(input: IDataObject): AssignmentValue[] {
const assignments: AssignmentValue[] = [];
function processValue(value: IDataObject, path: Array<string | number> = []) {
if (Array.isArray(value)) {
value.forEach((element, index) => {
processValue(element, [...path, index]);
});
} else if (isObject(value)) {
for (const [key, objectValue] of Object.entries(value)) {
processValue(objectValue as IDataObject, [...path, key]);
}
} else {
const stringPath = path.reduce((fullPath: string, part) => {
if (typeof part === 'number') {
return `${fullPath}[${part}]`;
}
return `${fullPath}.${part}`;
}, '$json');
const expression = `={{ ${stringPath} }}`;
assignments.push({
id: uuid(),
name: stringPath.replace('$json.', ''),
value: expression,
type: inferAssignmentType(value),
});
}
}
processValue(input);
return assignments;
}