mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(Summarize Node): Fix spaces in Fields to Split By values converted to underscores (#15020)
This commit is contained in:
@@ -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`] = `
|
||||
[
|
||||
{
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -225,22 +225,22 @@ export function aggregateAndSplitData({
|
||||
|
||||
const groupedItems = new Map<unknown, IDataObject[]>();
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user