From 154153d86f59552cffe33a1a746cbaf28fc8886f Mon Sep 17 00:00:00 2001 From: Shireen Missi <94372015+ShireenMissi@users.noreply.github.com> Date: Wed, 30 Apr 2025 16:24:31 +0100 Subject: [PATCH] fix(Summarize Node): Fix spaces in Fields to Split By values converted to underscores (#15020) --- .../__snapshots__/splitData.test.ts.snap | 118 ++++++++++++++++++ .../test/unitTests/splitData.test.ts | 89 +++++++++++++ .../nodes/Transform/Summarize/utils.ts | 14 +-- 3 files changed, 214 insertions(+), 7 deletions(-) diff --git a/packages/nodes-base/nodes/Transform/Summarize/test/unitTests/__snapshots__/splitData.test.ts.snap b/packages/nodes-base/nodes/Transform/Summarize/test/unitTests/__snapshots__/splitData.test.ts.snap index 1e6411fefb..068ab30d1b 100644 --- a/packages/nodes-base/nodes/Transform/Summarize/test/unitTests/__snapshots__/splitData.test.ts.snap +++ b/packages/nodes-base/nodes/Transform/Summarize/test/unitTests/__snapshots__/splitData.test.ts.snap @@ -1,5 +1,123 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Test Summarize Node, aggregateAndSplitData should handle split field values containing spaces when convertKeysToString is not set: split-field-with-spaces-array 1`] = ` +[ + { + "pairedItems": [ + 0, + 2, + ], + "returnData": { + "Product": "Widget A", + "appended_Warehouse": [ + "WH1", + "WH3", + ], + }, + }, + { + "pairedItems": [ + 1, + ], + "returnData": { + "Product": "Widget B", + "appended_Warehouse": [ + "WH2", + ], + }, + }, +] +`; + +exports[`Test Summarize Node, aggregateAndSplitData should handle split field values containing spaces when convertKeysToString is not set: split-field-with-spaces-result 1`] = ` +{ + "fieldName": "Product", + "splits": Map { + "Widget A" => { + "pairedItems": [ + 0, + 2, + ], + "returnData": { + "appended_Warehouse": [ + "WH1", + "WH3", + ], + }, + }, + "Widget B" => { + "pairedItems": [ + 1, + ], + "returnData": { + "appended_Warehouse": [ + "WH2", + ], + }, + }, + }, +} +`; + +exports[`Test Summarize Node, aggregateAndSplitData should handle split field values containing spaces when convertKeysToString is true: split-field-with-spaces-array 1`] = ` +[ + { + "pairedItems": [ + 0, + 2, + ], + "returnData": { + "Product": "Widget A", + "appended_Warehouse": [ + "WH1", + "WH3", + ], + }, + }, + { + "pairedItems": [ + 1, + ], + "returnData": { + "Product": "Widget B", + "appended_Warehouse": [ + "WH2", + ], + }, + }, +] +`; + +exports[`Test Summarize Node, aggregateAndSplitData should handle split field values containing spaces when convertKeysToString is true: split-field-with-spaces-result 1`] = ` +{ + "fieldName": "Product", + "splits": Map { + "Widget A" => { + "pairedItems": [ + 0, + 2, + ], + "returnData": { + "appended_Warehouse": [ + "WH1", + "WH3", + ], + }, + }, + "Widget B" => { + "pairedItems": [ + 1, + ], + "returnData": { + "appended_Warehouse": [ + "WH2", + ], + }, + }, + }, +} +`; + exports[`Test Summarize Node, aggregateAndSplitData should not convert numbers to strings: array 1`] = ` [ { diff --git a/packages/nodes-base/nodes/Transform/Summarize/test/unitTests/splitData.test.ts b/packages/nodes-base/nodes/Transform/Summarize/test/unitTests/splitData.test.ts index 296bbe29f8..78c9189ab4 100644 --- a/packages/nodes-base/nodes/Transform/Summarize/test/unitTests/splitData.test.ts +++ b/packages/nodes-base/nodes/Transform/Summarize/test/unitTests/splitData.test.ts @@ -88,6 +88,95 @@ describe('Test Summarize Node, aggregateAndSplitData', () => { expect(flattenAggregationResultToArray(result)).toMatchSnapshot('array'); }); + test('should handle split field values containing spaces when convertKeysToString is not set', () => { + const data = [ + { + Product: 'Widget A', + Warehouse: 'WH1', + Qty: '5', + _itemIndex: 0, + }, + { + Product: 'Widget B', + Warehouse: 'WH2', + Qty: '3', + _itemIndex: 1, + }, + { + Product: 'Widget A', + Warehouse: 'WH3', + Qty: '2', + _itemIndex: 2, + }, + ]; + + const aggregations: Aggregations = [ + { + aggregation: 'append', + field: 'Warehouse', + includeEmpty: true, + }, + ]; + + const result = aggregateAndSplitData({ + splitKeys: ['Product'], + inputItems: data, + fieldsToSummarize: aggregations, + options: { continueIfFieldNotFound: true }, + getValue: fieldValueGetter(), + }); + + expect(result).toMatchSnapshot('split-field-with-spaces-result'); + expect(flattenAggregationResultToArray(result)).toMatchSnapshot( + 'split-field-with-spaces-array', + ); + }); + + test('should handle split field values containing spaces when convertKeysToString is true', () => { + const data = [ + { + Product: 'Widget A', + Warehouse: 'WH1', + Qty: '5', + _itemIndex: 0, + }, + { + Product: 'Widget B', + Warehouse: 'WH2', + Qty: '3', + _itemIndex: 1, + }, + { + Product: 'Widget A', + Warehouse: 'WH3', + Qty: '2', + _itemIndex: 2, + }, + ]; + + const aggregations: Aggregations = [ + { + aggregation: 'append', + field: 'Warehouse', + includeEmpty: true, + }, + ]; + + const result = aggregateAndSplitData({ + splitKeys: ['Product'], + inputItems: data, + fieldsToSummarize: aggregations, + options: { continueIfFieldNotFound: true }, + getValue: fieldValueGetter(), + convertKeysToString: true, + }); + + expect(result).toMatchSnapshot('split-field-with-spaces-result'); + expect(flattenAggregationResultToArray(result)).toMatchSnapshot( + 'split-field-with-spaces-array', + ); + }); + describe('with skipEmptySplitFields=true', () => { test('should skip empty split fields', () => { const data = [ diff --git a/packages/nodes-base/nodes/Transform/Summarize/utils.ts b/packages/nodes-base/nodes/Transform/Summarize/utils.ts index 9a28d240a6..84231e1f90 100644 --- a/packages/nodes-base/nodes/Transform/Summarize/utils.ts +++ b/packages/nodes-base/nodes/Transform/Summarize/utils.ts @@ -225,22 +225,22 @@ export function aggregateAndSplitData({ const groupedItems = new Map(); for (const item of inputItems) { - let key = getValue(item, firstSplitKey); + let splitValue = getValue(item, firstSplitKey); - if (key && typeof key === 'object') { - key = JSON.stringify(key); + if (splitValue && typeof splitValue === 'object') { + splitValue = JSON.stringify(splitValue); } if (convertKeysToString) { - key = normalizeFieldName(String(key)); + splitValue = String(splitValue); } - if (options.skipEmptySplitFields && typeof key !== 'number' && !key) { + if (options.skipEmptySplitFields && typeof splitValue !== 'number' && !splitValue) { continue; } - const group = groupedItems.get(key) ?? []; - groupedItems.set(key, group.concat([item])); + const group = groupedItems.get(splitValue) ?? []; + groupedItems.set(splitValue, group.concat([item])); } const splits = new Map(