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
28 changes: 28 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,34 @@ public function chunk($size)
return new static($chunks);
}

/**
* Chunk the collection into chunks with a callback.
*
* @param callable $callback
* @return static
*/
public function chunkWhile(callable $callback)
{
$chunks = [];

$chunk = [];

foreach ($this->items as $current) {
if (isset($previous) && ! $callback($previous, $current)) {
$chunks[] = new static($chunk);
$chunk = [];
}

$chunk[] = $current;
$previous = $current;
}


$chunks[] = new static($chunk);

return new static($chunks);
}

/**
* Sort through each item with a callback.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Collections/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,14 @@ public function split($numberOfGroups);
*/
public function chunk($size);

/**
* Chunk the collection into chunks with a callback.
*
* @param callable $callback
* @return static
*/
public function chunkWhile(callable $callback);

/**
* Sort through each item with a callback.
*
Expand Down
30 changes: 30 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,36 @@ public function chunk($size)
});
}

/**
* Chunk the collection into chunks with a callback.
*
* @param callable $callback
* @return static
*/
public function chunkWhile(callable $callback)
{
return new static(function () use ($callback) {
$iterator = $this->getIterator();

$chunk = [];

while ($iterator->valid()) {
if (isset($previous) && ! $callback($previous, $iterator->current())) {
yield new static($chunk);

$chunk = [];
}

$chunk[] = $iterator->current();
$previous = $iterator->current();

$iterator->next();
}

yield new static($chunk);
});
}

/**
* Sort through each item with a callback.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,42 @@ public function testChunkWhenGivenLessThanZero($collection)
);
}

/**
* @dataProvider collectionClassProvider
*/
public function testChunkWhileOnEqualElements($collection)
{
$data = (new $collection(['A', 'A', 'B', 'B', 'C', 'C', 'C']))
->chunkWhile(function ($previous, $current) {
return $previous === $current;
});

$this->assertInstanceOf($collection, $data);
$this->assertInstanceOf($collection, $data->first());
$this->assertEquals(['A', 'A'], $data->first()->toArray());
$this->assertEquals(['B', 'B'], $data->get(1)->toArray());
$this->assertEquals(['C', 'C', 'C'], $data->last()->toArray());
}

/**
* @dataProvider collectionClassProvider
*/
public function testChunkWhileOnContiguouslyIncreasingIntegers($collection)
{
$data = (new $collection([1, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21]))
->chunkWhile(function ($previous, $current) {
return $previous + 1 == $current;
});

$this->assertInstanceOf($collection, $data);
$this->assertInstanceOf($collection, $data->first());
$this->assertEquals([1], $data->first()->toArray());
$this->assertEquals([4], $data->get(1)->toArray());
$this->assertEquals([9, 10, 11, 12], $data->get(2)->toArray());
$this->assertEquals([15, 16], $data->get(3)->toArray());
$this->assertEquals([19, 20, 21], $data->last()->toArray());
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down