2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject, INodeExecutionData } from 'n8n-workflow';
|
2023-02-21 05:42:00 -08:00
|
|
|
|
2023-02-23 07:16:05 -08:00
|
|
|
import difference from 'lodash.difference';
|
|
|
|
import get from 'lodash.get';
|
|
|
|
import intersection from 'lodash.intersection';
|
|
|
|
import isEmpty from 'lodash.isempty';
|
|
|
|
import omit from 'lodash.omit';
|
|
|
|
import set from 'lodash.set';
|
|
|
|
import union from 'lodash.union';
|
2023-02-21 05:42:00 -08:00
|
|
|
import { fuzzyCompare } from '../../utils/utilities';
|
2022-10-18 04:10:18 -07:00
|
|
|
|
|
|
|
type PairToMatch = {
|
|
|
|
field1: string;
|
|
|
|
field2: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type EntryMatches = {
|
|
|
|
entry: INodeExecutionData;
|
|
|
|
matches: INodeExecutionData[];
|
|
|
|
};
|
|
|
|
|
2023-01-04 04:37:54 -08:00
|
|
|
type CompareFunction = <T, U>(a: T, b: U) => boolean;
|
|
|
|
|
2023-02-21 05:42:00 -08:00
|
|
|
const processNullishValueFunction = (version: number) => {
|
|
|
|
if (version === 2) {
|
|
|
|
return <T>(value: T) => (value === undefined ? null : value);
|
|
|
|
}
|
|
|
|
return <T>(value: T) => value || null;
|
|
|
|
};
|
|
|
|
|
2022-10-18 04:10:18 -07:00
|
|
|
function compareItems(
|
|
|
|
item1: INodeExecutionData,
|
|
|
|
item2: INodeExecutionData,
|
|
|
|
fieldsToMatch: PairToMatch[],
|
2023-02-21 05:42:00 -08:00
|
|
|
options: IDataObject,
|
2022-11-16 06:37:55 -08:00
|
|
|
skipFields: string[],
|
2023-01-04 04:37:54 -08:00
|
|
|
isEntriesEqual: CompareFunction,
|
2022-10-18 04:10:18 -07:00
|
|
|
) {
|
|
|
|
const keys = {} as IDataObject;
|
|
|
|
fieldsToMatch.forEach((field) => {
|
|
|
|
keys[field.field1] = item1.json[field.field1];
|
|
|
|
});
|
|
|
|
|
|
|
|
const keys1 = Object.keys(item1.json);
|
|
|
|
const keys2 = Object.keys(item2.json);
|
|
|
|
const intersectionKeys = intersection(keys1, keys2);
|
|
|
|
|
|
|
|
const same = intersectionKeys.reduce((acc, key) => {
|
2023-01-04 04:37:54 -08:00
|
|
|
if (isEntriesEqual(item1.json[key], item2.json[key])) {
|
2022-10-18 04:10:18 -07:00
|
|
|
acc[key] = item1.json[key];
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {} as IDataObject);
|
|
|
|
|
|
|
|
const sameKeys = Object.keys(same);
|
|
|
|
const allUniqueKeys = union(keys1, keys2);
|
|
|
|
const differentKeys = difference(allUniqueKeys, sameKeys);
|
|
|
|
|
|
|
|
const different: IDataObject = {};
|
2022-11-16 06:37:55 -08:00
|
|
|
const skipped: IDataObject = {};
|
2022-10-18 04:10:18 -07:00
|
|
|
|
|
|
|
differentKeys.forEach((key) => {
|
2023-02-21 05:42:00 -08:00
|
|
|
const processNullishValue = processNullishValueFunction(options.nodeVersion as number);
|
|
|
|
|
|
|
|
switch (options.resolve) {
|
2022-10-18 04:10:18 -07:00
|
|
|
case 'preferInput1':
|
2023-02-21 05:42:00 -08:00
|
|
|
different[key] = processNullishValue(item1.json[key]);
|
2022-10-18 04:10:18 -07:00
|
|
|
break;
|
|
|
|
case 'preferInput2':
|
2023-02-21 05:42:00 -08:00
|
|
|
different[key] = processNullishValue(item2.json[key]);
|
2022-10-18 04:10:18 -07:00
|
|
|
break;
|
|
|
|
default:
|
2023-02-21 05:42:00 -08:00
|
|
|
const input1 = processNullishValue(item1.json[key]);
|
|
|
|
const input2 = processNullishValue(item2.json[key]);
|
|
|
|
|
|
|
|
let [firstInputName, secondInputName] = ['input1', 'input2'];
|
|
|
|
if (options.nodeVersion === 2) {
|
|
|
|
[firstInputName, secondInputName] = ['inputA', 'inputB'];
|
|
|
|
}
|
|
|
|
|
2022-11-16 06:37:55 -08:00
|
|
|
if (skipFields.includes(key)) {
|
2023-02-21 05:42:00 -08:00
|
|
|
skipped[key] = { [firstInputName]: input1, [secondInputName]: input2 };
|
2022-11-16 06:37:55 -08:00
|
|
|
} else {
|
2023-02-21 05:42:00 -08:00
|
|
|
different[key] = { [firstInputName]: input1, [secondInputName]: input2 };
|
2022-11-16 06:37:55 -08:00
|
|
|
}
|
2022-10-18 04:10:18 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-16 06:37:55 -08:00
|
|
|
return {
|
|
|
|
json: { keys, same, different, ...(!isEmpty(skipped) && { skipped }) },
|
|
|
|
} as INodeExecutionData;
|
2022-10-18 04:10:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function combineItems(
|
|
|
|
item1: INodeExecutionData,
|
|
|
|
item2: INodeExecutionData,
|
|
|
|
prefer: string,
|
|
|
|
except: string,
|
|
|
|
disableDotNotation: boolean,
|
|
|
|
) {
|
|
|
|
let exceptFields: string[];
|
|
|
|
const [entry, match] = prefer === 'input1' ? [item1, item2] : [item2, item1];
|
|
|
|
|
|
|
|
if (except && Array.isArray(except) && except.length) {
|
|
|
|
exceptFields = except;
|
|
|
|
} else {
|
|
|
|
exceptFields = except ? except.split(',').map((field) => field.trim()) : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
exceptFields.forEach((field) => {
|
|
|
|
entry.json[field] = match.json[field];
|
|
|
|
if (disableDotNotation) {
|
|
|
|
entry.json[field] = match.json[field];
|
|
|
|
} else {
|
2023-01-19 04:37:19 -08:00
|
|
|
const value = get(match.json, field) || null;
|
2022-10-18 04:10:18 -07:00
|
|
|
set(entry, `json.${field}`, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findAllMatches(
|
|
|
|
data: INodeExecutionData[],
|
|
|
|
lookup: IDataObject,
|
|
|
|
disableDotNotation: boolean,
|
2023-01-04 04:37:54 -08:00
|
|
|
isEntriesEqual: CompareFunction,
|
2022-10-18 04:10:18 -07:00
|
|
|
) {
|
|
|
|
return data.reduce((acc, entry2, i) => {
|
|
|
|
if (entry2 === undefined) return acc;
|
|
|
|
|
|
|
|
for (const key of Object.keys(lookup)) {
|
|
|
|
const excpectedValue = lookup[key];
|
|
|
|
let entry2FieldValue;
|
|
|
|
|
|
|
|
if (disableDotNotation) {
|
|
|
|
entry2FieldValue = entry2.json[key];
|
|
|
|
} else {
|
|
|
|
entry2FieldValue = get(entry2.json, key);
|
|
|
|
}
|
|
|
|
|
2023-01-04 04:37:54 -08:00
|
|
|
if (!isEntriesEqual(excpectedValue, entry2FieldValue)) {
|
2022-10-18 04:10:18 -07:00
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc.concat({
|
|
|
|
entry: entry2,
|
|
|
|
index: i,
|
|
|
|
});
|
|
|
|
}, [] as IDataObject[]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findFirstMatch(
|
|
|
|
data: INodeExecutionData[],
|
|
|
|
lookup: IDataObject,
|
|
|
|
disableDotNotation: boolean,
|
2023-01-04 04:37:54 -08:00
|
|
|
isEntriesEqual: CompareFunction,
|
2022-10-18 04:10:18 -07:00
|
|
|
) {
|
|
|
|
const index = data.findIndex((entry2) => {
|
|
|
|
if (entry2 === undefined) return false;
|
|
|
|
|
|
|
|
for (const key of Object.keys(lookup)) {
|
|
|
|
const excpectedValue = lookup[key];
|
|
|
|
let entry2FieldValue;
|
|
|
|
|
|
|
|
if (disableDotNotation) {
|
|
|
|
entry2FieldValue = entry2.json[key];
|
|
|
|
} else {
|
|
|
|
entry2FieldValue = get(entry2.json, key);
|
|
|
|
}
|
|
|
|
|
2023-01-04 04:37:54 -08:00
|
|
|
if (!isEntriesEqual(excpectedValue, entry2FieldValue)) {
|
2022-10-18 04:10:18 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (index === -1) return [];
|
|
|
|
|
|
|
|
return [{ entry: data[index], index }];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findMatches(
|
|
|
|
input1: INodeExecutionData[],
|
|
|
|
input2: INodeExecutionData[],
|
|
|
|
fieldsToMatch: PairToMatch[],
|
|
|
|
options: IDataObject,
|
|
|
|
) {
|
|
|
|
const data1 = [...input1];
|
|
|
|
const data2 = [...input2];
|
|
|
|
|
2023-02-21 05:42:00 -08:00
|
|
|
let compareVersion = 1;
|
|
|
|
if (options.nodeVersion === 2) {
|
|
|
|
compareVersion = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isEntriesEqual = fuzzyCompare(options.fuzzyCompare as boolean, compareVersion);
|
2022-10-18 04:10:18 -07:00
|
|
|
const disableDotNotation = (options.disableDotNotation as boolean) || false;
|
|
|
|
const multipleMatches = (options.multipleMatches as string) || 'first';
|
2022-11-16 06:37:55 -08:00
|
|
|
const skipFields = ((options.skipFields as string) || '').split(',').map((field) => field.trim());
|
2022-10-18 04:10:18 -07:00
|
|
|
|
|
|
|
const filteredData = {
|
|
|
|
matched: [] as EntryMatches[],
|
|
|
|
unmatched1: [] as INodeExecutionData[],
|
|
|
|
unmatched2: [] as INodeExecutionData[],
|
|
|
|
};
|
|
|
|
|
|
|
|
const matchedInInput2 = new Set<number>();
|
|
|
|
|
|
|
|
matchesLoop: for (const entry of data1) {
|
|
|
|
const lookup: IDataObject = {};
|
|
|
|
|
|
|
|
fieldsToMatch.forEach((matchCase) => {
|
|
|
|
let valueToCompare;
|
|
|
|
if (disableDotNotation) {
|
2022-12-02 12:54:28 -08:00
|
|
|
valueToCompare = entry.json[matchCase.field1];
|
2022-10-18 04:10:18 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
valueToCompare = get(entry.json, matchCase.field1);
|
2022-10-18 04:10:18 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
lookup[matchCase.field2] = valueToCompare;
|
2022-10-18 04:10:18 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const fieldValue of Object.values(lookup)) {
|
|
|
|
if (fieldValue === undefined) {
|
|
|
|
filteredData.unmatched1.push(entry);
|
|
|
|
continue matchesLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const foundedMatches =
|
|
|
|
multipleMatches === 'all'
|
2023-01-04 04:37:54 -08:00
|
|
|
? findAllMatches(data2, lookup, disableDotNotation, isEntriesEqual)
|
|
|
|
: findFirstMatch(data2, lookup, disableDotNotation, isEntriesEqual);
|
2022-10-18 04:10:18 -07:00
|
|
|
|
|
|
|
const matches = foundedMatches.map((match) => match.entry) as INodeExecutionData[];
|
|
|
|
foundedMatches.map((match) => matchedInInput2.add(match.index as number));
|
|
|
|
|
|
|
|
if (matches.length) {
|
|
|
|
filteredData.matched.push({ entry, matches });
|
|
|
|
} else {
|
|
|
|
filteredData.unmatched1.push(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data2.forEach((entry, i) => {
|
|
|
|
if (!matchedInInput2.has(i)) {
|
|
|
|
filteredData.unmatched2.push(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const same: INodeExecutionData[] = [];
|
|
|
|
const different: INodeExecutionData[] = [];
|
|
|
|
|
|
|
|
filteredData.matched.forEach((entryMatches) => {
|
|
|
|
let entryCopy: INodeExecutionData | undefined;
|
|
|
|
|
|
|
|
entryMatches.matches.forEach((match) => {
|
2022-11-16 06:37:55 -08:00
|
|
|
let entryFromInput1 = entryMatches.entry.json;
|
|
|
|
let entryFromInput2 = match.json;
|
|
|
|
|
|
|
|
if (skipFields.length) {
|
|
|
|
entryFromInput1 = omit(entryFromInput1, skipFields);
|
|
|
|
entryFromInput2 = omit(entryFromInput2, skipFields);
|
|
|
|
}
|
2023-01-04 04:37:54 -08:00
|
|
|
|
|
|
|
let isItemsEqual = true;
|
|
|
|
if (options.fuzzyCompare) {
|
|
|
|
for (const key of Object.keys(entryFromInput1)) {
|
|
|
|
if (!isEntriesEqual(entryFromInput1[key], entryFromInput2[key])) {
|
|
|
|
isItemsEqual = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isItemsEqual = isEntriesEqual(entryFromInput1, entryFromInput2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isItemsEqual) {
|
|
|
|
if (!entryCopy) {
|
|
|
|
if (options.fuzzyCompare && options.resolve === 'preferInput2') {
|
|
|
|
entryCopy = match;
|
|
|
|
} else {
|
|
|
|
entryCopy = entryMatches.entry;
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 04:10:18 -07:00
|
|
|
} else {
|
|
|
|
switch (options.resolve) {
|
|
|
|
case 'preferInput1':
|
|
|
|
different.push(entryMatches.entry);
|
|
|
|
break;
|
|
|
|
case 'preferInput2':
|
|
|
|
different.push(match);
|
|
|
|
break;
|
|
|
|
case 'mix':
|
|
|
|
different.push(
|
|
|
|
combineItems(
|
|
|
|
entryMatches.entry,
|
|
|
|
match,
|
|
|
|
options.preferWhenMix as string,
|
|
|
|
options.exceptWhenMix as string,
|
|
|
|
disableDotNotation,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
different.push(
|
2022-11-16 06:37:55 -08:00
|
|
|
compareItems(
|
|
|
|
entryMatches.entry,
|
|
|
|
match,
|
|
|
|
fieldsToMatch,
|
2023-02-21 05:42:00 -08:00
|
|
|
options,
|
2022-11-16 06:37:55 -08:00
|
|
|
skipFields,
|
2023-01-04 04:37:54 -08:00
|
|
|
isEntriesEqual,
|
2022-11-16 06:37:55 -08:00
|
|
|
),
|
2022-10-18 04:10:18 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!isEmpty(entryCopy)) {
|
|
|
|
same.push(entryCopy);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return [filteredData.unmatched1, same, different, filteredData.unmatched2];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function checkMatchFieldsInput(data: IDataObject[]) {
|
|
|
|
if (data.length === 1 && data[0].field1 === '' && data[0].field2 === '') {
|
|
|
|
throw new Error(
|
|
|
|
'You need to define at least one pair of fields in "Fields to Match" to match on',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (const [index, pair] of data.entries()) {
|
|
|
|
if (pair.field1 === '' || pair.field2 === '') {
|
|
|
|
throw new Error(
|
|
|
|
`You need to define both fields in "Fields to Match" for pair ${index + 1},
|
|
|
|
field 1 = '${pair.field1}'
|
|
|
|
field 2 = '${pair.field2}'`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data as PairToMatch[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function checkInput(
|
|
|
|
input: INodeExecutionData[],
|
|
|
|
fields: string[],
|
|
|
|
disableDotNotation: boolean,
|
|
|
|
inputLabel: string,
|
|
|
|
) {
|
2022-11-16 06:37:55 -08:00
|
|
|
if (input.some((item) => isEmpty(item.json))) {
|
|
|
|
input = input.filter((item) => !isEmpty(item.json));
|
|
|
|
}
|
|
|
|
if (input.length === 0) {
|
|
|
|
return input;
|
|
|
|
}
|
2022-10-18 04:10:18 -07:00
|
|
|
for (const field of fields) {
|
|
|
|
const isPresent = (input || []).some((entry) => {
|
|
|
|
if (disableDotNotation) {
|
|
|
|
return entry.json.hasOwnProperty(field);
|
|
|
|
}
|
|
|
|
return get(entry.json, field, undefined) !== undefined;
|
|
|
|
});
|
|
|
|
if (!isPresent) {
|
|
|
|
throw new Error(`Field '${field}' is not present in any of items in '${inputLabel}'`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return input;
|
|
|
|
}
|