Skip to content

Commit

Permalink
fix(Item Lists Node): Don't check same type in remove duplicates oper…
Browse files Browse the repository at this point in the history
…ation (#7678)

Github issue / Community forum post (link here to close automatically):

---------

Co-authored-by: Michael Kret <michael.k@radency.com>
  • Loading branch information
maspio and michael-radency committed Nov 21, 2023
1 parent 366cd67 commit 4f30764
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/ItemLists/ItemLists.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ItemLists extends VersionedNodeType {
group: ['input'],
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Helper for working with lists of items and transforming arrays',
defaultVersion: 3,
defaultVersion: 3.1,
};

const nodeVersions: IVersionedNodeType['nodeVersions'] = {
Expand All @@ -23,6 +23,7 @@ export class ItemLists extends VersionedNodeType {
2.1: new ItemListsV2(baseDescription),
2.2: new ItemListsV2(baseDescription),
3: new ItemListsV3(baseDescription),
3.1: new ItemListsV3(baseDescription),
};

super(nodeVersions, baseDescription);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isEqual from 'lodash/isEqual';
import lt from 'lodash/lt';
import pick from 'lodash/pick';

import { compareItems, flattenKeys, prepareFieldsArray } from '../../helpers/utils';
import { compareItems, flattenKeys, prepareFieldsArray, typeToNumber } from '../../helpers/utils';
import { disableDotNotationBoolean } from '../common.descriptions';
import { updateDisplayOptions } from '@utils/utilities';

Expand Down Expand Up @@ -105,6 +105,7 @@ export async function execute(
false,
) as boolean;
const removeOtherFields = this.getNodeParameter('options.removeOtherFields', 0, false) as boolean;
const nodeVersion = this.getNode().typeVersion;

let keys = disableDotNotation
? Object.keys(items[0].json)
Expand Down Expand Up @@ -163,24 +164,28 @@ export async function execute(
pairedItem: { item: index },
}) as INodeExecutionData,
);

//sort items using the compare keys
newItems.sort((a, b) => {
let result = 0;

for (const key of keys) {
let equal;
if (!disableDotNotation) {
equal = isEqual(get(a.json, key), get(b.json, key));
} else {
equal = isEqual(a.json[key], b.json[key]);
const a_value = disableDotNotation ? a.json[key] : get(a.json, key);
const b_value = disableDotNotation ? b.json[key] : get(b.json, key);

if (nodeVersion >= 3.1) {
const a_value_tnum = typeToNumber(a_value);
const b_value_tnum = typeToNumber(b_value);
if (a_value_tnum !== b_value_tnum) {
result = a_value_tnum - b_value_tnum;
break;
}
}

const equal = isEqual(a_value, b_value);

if (!equal) {
let lessThan;
if (!disableDotNotation) {
lessThan = lt(get(a.json, key), get(b.json, key));
} else {
lessThan = lt(a.json[key], b.json[key]);
}
const lessThan = lt(a_value, b_value);
result = lessThan ? -1 : 1;
break;
}
Expand Down Expand Up @@ -210,7 +215,7 @@ export async function execute(
`'${key}' field is missing from some input items`,
);
}
if (type !== undefined && value !== undefined && type !== typeof value) {
if (nodeVersion < 3.1 && type !== undefined && value !== undefined && type !== typeof value) {
throw new NodeOperationError(this.getNode(), `'${key}' isn't always the same type`, {
description: 'The type of this field varies between items',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const versionDescription: INodeTypeDescription = {
group: ['input'],
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Helper for working with lists of items and transforming arrays',
version: 3,
version: [3, 3.1],
defaults: {
name: 'Item Lists',
},
Expand Down
20 changes: 20 additions & 0 deletions packages/nodes-base/nodes/ItemLists/V3/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
IBinaryData,
INode,
INodeExecutionData,
GenericValue,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';

Expand Down Expand Up @@ -147,3 +148,22 @@ export function addBinariesToItem(

return newItem;
}

export function typeToNumber(value: GenericValue): number {
if (typeof value === 'object') {
if (Array.isArray(value)) return 9;
if (value === null) return 10;
if (value instanceof Date) return 11;
}
const types = {
_string: 1,
_number: 2,
_bigint: 3,
_boolean: 4,
_symbol: 5,
_undefined: 6,
_object: 7,
_function: 8,
};
return types[`_${typeof value}`];
}

0 comments on commit 4f30764

Please sign in to comment.