Skip to content

Commit

Permalink
Add the key to the callback on partition method (#20783)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lloople authored and taylorotwell committed Aug 28, 2017
1 parent aba1bca commit 1a33dc2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ public function partition($callback)
$callback = $this->valueRetriever($callback);

foreach ($this->items as $key => $item) {
$partitions[(int) ! $callback($item)][$key] = $item;
$partitions[(int) ! $callback($item, $key)][$key] = $item;
}

return new static($partitions);
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,19 @@ public function testPartition()
$this->assertEquals([6, 7, 8, 9, 10], $secondPartition->values()->toArray());
}

public function testPartitionCallbackWithKey()
{
$collection = new Collection(['zero', 'one','two', 'three']);

list($even, $odd) = $collection->partition(function ($item, $index) {
return $index % 2 === 0;
});

$this->assertEquals(['zero', 'two'], $even->values()->toArray());

$this->assertEquals(['one', 'three'], $odd->values()->toArray());
}

public function testPartitionByKey()
{
$courses = new Collection([
Expand Down

0 comments on commit 1a33dc2

Please sign in to comment.