From d58eec47c07d92bb9c592eecd70f3d8dfb75e219 Mon Sep 17 00:00:00 2001 From: yaisog Date: Wed, 29 Oct 2025 12:57:39 +0100 Subject: [PATCH] Modify output of some math operators for empty inputs (#9337) * Initial commit * Adapt test to new behavior --- core/modules/filters/math.js | 4 ++++ editions/test/tiddlers/tests/test-prefixes-filter.js | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/modules/filters/math.js b/core/modules/filters/math.js index 4c2a9168a6..bdf2117eb7 100644 --- a/core/modules/filters/math.js +++ b/core/modules/filters/math.js @@ -217,6 +217,10 @@ function makeNumericReducingOperator(fnCalc,initialValue,fnFinal) { source(function(tiddler,title) { result.push($tw.utils.parseNumber(title)); }); + // We return an empty array if there are no input titles + if(result.length === 0) { + return []; + } var value = result.reduce(function(accumulator,currentValue) { return fnCalc(accumulator,currentValue); },initialValue); diff --git a/editions/test/tiddlers/tests/test-prefixes-filter.js b/editions/test/tiddlers/tests/test-prefixes-filter.js index 7b1e5b479f..68be6fe542 100644 --- a/editions/test/tiddlers/tests/test-prefixes-filter.js +++ b/editions/test/tiddlers/tests/test-prefixes-filter.js @@ -372,13 +372,13 @@ describe("'reduce' and 'intersection' filter prefix tests", function() { it("should handle the variance operator", function() { expect(parseFloat(wiki.filterTiddlers("[tag[shopping]get[price]variance[]]").join(","))).toBeCloseTo(2.92); expect(parseFloat(wiki.filterTiddlers("[tag[food]get[price]variance[]]").join(","))).toBeCloseTo(3.367); - expect(wiki.filterTiddlers(" +[variance[]]").toString()).toBe("NaN"); + expect(wiki.filterTiddlers(" +[variance[]]").toString()).toBe(""); }); it("should handle the standard-deviation operator", function() { expect(parseFloat(wiki.filterTiddlers("[tag[shopping]get[price]standard-deviation[]]").join(","))).toBeCloseTo(1.71); expect(parseFloat(wiki.filterTiddlers("[tag[food]get[price]standard-deviation[]]").join(","))).toBeCloseTo(1.835); - expect(wiki.filterTiddlers(" +[standard-deviation[]]").toString()).toBe("NaN"); + expect(wiki.filterTiddlers(" +[standard-deviation[]]").toString()).toBe(""); }); it("should handle the :intersection prefix", function() {