Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Allow collection partition by key and add preserving keys option #16644

Merged
merged 3 commits into from
Dec 6, 2016
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
18 changes: 10 additions & 8 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,20 +726,22 @@ public function forPage($page, $perPage)
}

/**
* Partition the collection into two array using the given callback.
* Partition the collection into two arrays using the given callback or key.
*
* @param callable $callback
* @return array
* @param callable|string $callback
* @return static
*/
public function partition(callable $callback)
public function partition($callback)
{
$partitions = [new static(), new static()];
$partitions = [new static, new static];

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

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

return $partitions;
return new static($partitions);
}

/**
Expand Down
32 changes: 29 additions & 3 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1640,16 +1640,42 @@ public function testSplitEmptyCollection()
);
}

public function testPartition()
public function testPartitionWithClosure()
{
$collection = new Collection(range(1, 10));

list($firstPartition, $secondPartition) = $collection->partition(function ($i) {
return $i <= 5;
});

$this->assertEquals([1, 2, 3, 4, 5], $firstPartition->toArray());
$this->assertEquals([6, 7, 8, 9, 10], $secondPartition->toArray());
$this->assertEquals([1, 2, 3, 4, 5], $firstPartition->values()->toArray());
$this->assertEquals([6, 7, 8, 9, 10], $secondPartition->values()->toArray());
}

public function testPartitionByKey()
{
$courses = new Collection([
['free' => true, 'title' => 'Basic'], ['free' => false, 'title' => 'Premium'],
]);

list($free, $premium) = $courses->partition('free');

$this->assertSame([['free' => true, 'title' => 'Basic']], $free->values()->toArray());

$this->assertSame([['free' => false, 'title' => 'Premium']], $premium->values()->toArray());
}

public function testPartitionPreservesKeys()
{
$courses = new Collection([
'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true],
]);

list($free, $premium) = $courses->partition('free');

$this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray());

$this->assertSame(['b' => ['free' => false]], $premium->toArray());
}

public function testPartitionEmptyCollection()
Expand Down