Skip to content

Commit

Permalink
feat(Item Lists Node): Split out items work on objects as well as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored Apr 27, 2023
1 parent 0c9ce3a commit c65ac03
Show file tree
Hide file tree
Showing 2 changed files with 462 additions and 66 deletions.
137 changes: 71 additions & 66 deletions packages/nodes-base/nodes/ItemLists/V2/ItemListsV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export class ItemListsV2 implements INodeType {
{
name: 'Split Out Items',
value: 'splitOutItems',
description: 'Turn a list inside item(s) into separate items',
description:
"Turn a list or values of object's properties inside item(s) into separate items",
action: 'Split Out Items',
},
{
Expand Down Expand Up @@ -844,81 +845,85 @@ return 0;`,
}
}

if (!Array.isArray(arrayToSplit)) {
if (typeof arrayToSplit !== 'object' || arrayToSplit === null) {
throw new NodeOperationError(
this.getNode(),
`The provided field '${fieldToSplitOut}' is not an array`,
`The provided field '${fieldToSplitOut}' is not an array or object`,
{ itemIndex: i },
);
} else {
for (const element of arrayToSplit) {
let newItem = {};

if (include === 'selectedOtherFields') {
const fieldsToInclude = (
this.getNodeParameter('fieldsToInclude.fields', i, []) as [{ fieldName: string }]
).map((field) => field.fieldName);

if (!fieldsToInclude.length) {
throw new NodeOperationError(this.getNode(), 'No fields specified', {
description: 'Please add a field to include',
});
}
}

newItem = {
...fieldsToInclude.reduce((prev, field) => {
if (field === fieldToSplitOut) {
return prev;
}
let value;
if (!disableDotNotation) {
value = get(items[i].json, field);
} else {
value = items[i].json[field];
}
prev = { ...prev, [field]: value };
return prev;
}, {}),
};
} else if (include === 'allOtherFields') {
const keys = Object.keys(items[i].json);

newItem = {
...keys.reduce((prev, field) => {
let value;
if (!disableDotNotation) {
value = get(items[i].json, field);
} else {
value = items[i].json[field];
}
prev = { ...prev, [field]: value };
return prev;
}, {}),
};
if (!Array.isArray(arrayToSplit)) {
arrayToSplit = Object.values(arrayToSplit);
}

unset(newItem, fieldToSplitOut);
}
for (const element of arrayToSplit) {
let newItem = {};

if (
typeof element === 'object' &&
include === 'noOtherFields' &&
destinationFieldName === ''
) {
newItem = { ...newItem, ...element };
} else {
newItem = {
...newItem,
[destinationFieldName || fieldToSplitOut]: element,
};
if (include === 'selectedOtherFields') {
const fieldsToInclude = (
this.getNodeParameter('fieldsToInclude.fields', i, []) as [{ fieldName: string }]
).map((field) => field.fieldName);

if (!fieldsToInclude.length) {
throw new NodeOperationError(this.getNode(), 'No fields specified', {
description: 'Please add a field to include',
});
}

returnData.push({
json: newItem,
pairedItem: {
item: i,
},
});
newItem = {
...fieldsToInclude.reduce((prev, field) => {
if (field === fieldToSplitOut) {
return prev;
}
let value;
if (!disableDotNotation) {
value = get(items[i].json, field);
} else {
value = items[i].json[field];
}
prev = { ...prev, [field]: value };
return prev;
}, {}),
};
} else if (include === 'allOtherFields') {
const keys = Object.keys(items[i].json);

newItem = {
...keys.reduce((prev, field) => {
let value;
if (!disableDotNotation) {
value = get(items[i].json, field);
} else {
value = items[i].json[field];
}
prev = { ...prev, [field]: value };
return prev;
}, {}),
};

unset(newItem, fieldToSplitOut);
}

if (
typeof element === 'object' &&
include === 'noOtherFields' &&
destinationFieldName === ''
) {
newItem = { ...newItem, ...element };
} else {
newItem = {
...newItem,
[destinationFieldName || fieldToSplitOut]: element,
};
}

returnData.push({
json: newItem,
pairedItem: {
item: i,
},
});
}
}

Expand Down
Loading

0 comments on commit c65ac03

Please sign in to comment.