fix(Summarize Node): Return null instead of NaN for an average with no numeric values
Summary
Closes #41542
User-visible effect: for a group with no numeric values, a node after Summarize sees average_X as NaN in memory today (so is empty checks miss it and arithmetic downstream yields NaN), even though the saved JSON shows null. After this change it is null, the same as Min and Max.
The Summarize node's Average aggregation only looks at numeric values: it filters the items down to those where the field is a number, then divides the sum by the count. When a group has no numeric values at all (every value is a string such as 'n/a', empty, or missing), the count is 0 and the result is 0 / 0 = NaN.
NaN then gets into the item. It isn't valid JSON, so it is saved and displayed as null. The next node in the same run, though, still receives the in-memory NaN: {{ $json.average_Qty }} is NaN, an IF "is empty" check doesn't match, and the arithmetic downstream gives NaN too. Min and Max already return null in exactly this case (return min ?? null), so Average is the only comparison or numeric aggregation that leaks NaN.
This change returns null from Average when no numeric values remain, which matches Min and Max. Groups that have numeric values are unaffected.
How to test
- Feed a Summarize node items
{Sku: 'A', Qty: 2},{Sku: 'A', Qty: 4},{Sku: 'B', Qty: 'n/a'},{Sku: 'B', Qty: ''}. - Choose Average of
Qtyand split bySku. - Group B comes out as
NaNin memory before this change andnullafter it.
Unit test: should return null, not NaN, for the average of a group with no numeric values in test/unitTests/splitData.test.ts. It fails on master ("average_Qty": NaN) and passes with the change.
I didn't install the full monorepo, so I ran splitData.test.ts against this utils.ts with vitest and a stubbed n8n-workflow (snapshots replaced): all 8 tests pass with the change, and only the new one fails without it. Prettier is clean.
Review / Merge checklist
- PR title and summary are descriptive.
- Tests included.
AI-assistance note: I prepared this change with help from an AI coding assistant, then reviewed and tested it myself.