diff --git a/src/builtin.jq b/src/builtin.jq index 7d22f97111..a6cdabe58a 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -164,7 +164,7 @@ def all(condition): all(.[]; condition); def any(condition): any(.[]; condition); def all: all(.[]; .); def any: any(.[]; .); -def last(g): reduce . as $_ (.; g); +def last(g): reduce . as $_ (.; g|[.]) | .[]?; def nth($n; g): if $n < 0 then error("nth doesn't support negative indices") else last(limit($n + 1; g)) end; def first: .[0]; def last: .[-1]; diff --git a/tests/jq.test b/tests/jq.test index 16d5c0ccf9..00e8744405 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -333,6 +333,14 @@ first(1,error("foo")) null 1 +[first(empty)] +null +[] + +[last(empty)] +null +[] + # # Check that various builtins evalute all arguments where appropriate, # doing cartesian products where appropriate.