Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Changed sort(), sort_by(), max(), min(), max_by() and min_by() to order strings by code point.
* Fixed max_by() and min_by() to error on mixed-type keys instead of returning arbitrary elements.
* Fixed max() returning null or erroring when the first array element is falsy, e.g. max([0, 1]).
* Fixed sum() and join() to return 0 and an empty string respectively for empty arrays.
* Fixed 0.0 to be truthy in filters and logical operators, like every other number.
* Fixed the compiled runtime to apply JMESPath truthiness to || and &&.
* Fixed @(foo), foo[-] and oversized index literals to throw syntax errors.
Expand Down
4 changes: 2 additions & 2 deletions src/FnDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private function fn_join(array $args)
$fn = function ($a, $b, $i) use ($args) {
return $i ? ($a . $args[0] . $b) : $b;
};
return $this->reduce('join:0', $args[1], ['string'], $fn);
return $args[1] ? $this->reduce('join:0', $args[1], ['string'], $fn) : '';
}

private function fn_keys(array $args)
Expand Down Expand Up @@ -203,7 +203,7 @@ private function fn_sum(array $args)
$fn = function ($a, $b) {
return Utils::add($a, $b);
};
return $this->reduce('sum:0', $args[0], ['number'], $fn);
return $args[0] ? $this->reduce('sum:0', $args[0], ['number'], $fn) : 0;
}

private function fn_sort(array $args)
Expand Down
16 changes: 16 additions & 0 deletions tests/FnDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ public function testContainsUsesJsonSemantics(): void
$this->assertFalse($fn('contains', ['foobar', 123]));
}

public function testSumOfEmptyArrayIsZero(): void
{
$fn = new FnDispatcher();

$this->assertSame(0, $fn('sum', [[]]));
$this->assertSame(3, $fn('sum', [[1, 2]]));
}

public function testJoinOfEmptyArrayIsEmptyString(): void
{
$fn = new FnDispatcher();

$this->assertSame('', $fn('join', ['|', []]));
$this->assertSame('a|b', $fn('join', ['|', ['a', 'b']]));
}

public function testMaxAndMinCompareStringsByCodePoint(): void
{
$fn = new FnDispatcher();
Expand Down
Loading