From 022cb142d703c06efdbe0ae5a7b3ec47cc623d0e Mon Sep 17 00:00:00 2001 From: Yarchik Date: Wed, 24 Jun 2026 01:44:35 +0100 Subject: [PATCH] fix: return null for avg() of empty array avg() over an empty array returned NaN (0/0) instead of null. The JMESPath spec states an empty array produces a return value of null. Guard the empty case before the sum loop and add the compliance case avg(empty_list) = null. --- jmespath.js | 3 +++ test/compliance/functions.json | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/jmespath.js b/jmespath.js index 0a36691..deaf4a2 100644 --- a/jmespath.js +++ b/jmespath.js @@ -1358,6 +1358,9 @@ _functionAvg: function(resolvedArgs) { var sum = 0; var inputArray = resolvedArgs[0]; + if (inputArray.length === 0) { + return null; + } for (var i = 0; i < inputArray.length; i++) { sum += inputArray[i]; } diff --git a/test/compliance/functions.json b/test/compliance/functions.json index 8b8db36..a749dda 100644 --- a/test/compliance/functions.json +++ b/test/compliance/functions.json @@ -83,6 +83,10 @@ "expression": "avg(strings)", "error": "invalid-type" }, + { + "expression": "avg(empty_list)", + "result": null + }, { "expression": "ceil(`1.2`)", "result": 2