Skip to content

Commit

Permalink
fix nth where step <= offset (#41645)
Browse files Browse the repository at this point in the history
Co-authored-by: Misha van Tol <misha.vantol@synetic.nl>
  • Loading branch information
mishavantol and Misha van Tol committed Mar 25, 2022
1 parent 447f620 commit 98aa74a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Collections/Collection.php
Expand Up @@ -806,8 +806,8 @@ public function nth($step, $offset = 0)

$position = 0;

foreach ($this->items as $item) {
if ($position % $step === $offset) {
foreach ($this->slice($offset)->items as $item) {
if ($position % $step === 0) {
$new[] = $item;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Collections/LazyCollection.php
Expand Up @@ -796,8 +796,8 @@ public function nth($step, $offset = 0)
return new static(function () use ($step, $offset) {
$position = 0;

foreach ($this as $item) {
if ($position % $step === $offset) {
foreach ($this->slice($offset) as $item) {
if ($position % $step === 0) {
yield $item;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Support/SupportCollectionTest.php
Expand Up @@ -2956,6 +2956,8 @@ public function testNth($collection)
$this->assertEquals(['b', 'f'], $data->nth(4, 1)->all());
$this->assertEquals(['c'], $data->nth(4, 2)->all());
$this->assertEquals(['d'], $data->nth(4, 3)->all());
$this->assertEquals(['c', 'e'], $data->nth(2, 2)->all());
$this->assertEquals(['c', 'd', 'e', 'f'], $data->nth(1, 2)->all());
}

/**
Expand Down

0 comments on commit 98aa74a

Please sign in to comment.