Skip to content

Commit

Permalink
Merge pull request #32494 from JosephSilber/fix/lazy-collection-until
Browse files Browse the repository at this point in the history
[7.x] Make LazyCollection@until actually be lazy
  • Loading branch information
taylorotwell authored Apr 22, 2020
2 parents bead456 + 4565657 commit a4326d8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,17 @@ public function union($items)
return new static($this->items + $this->getArrayableItems($items));
}

/**
* Take items in the collection until the given condition is met.
*
* @param mixed $key
* @return static
*/
public function until($value)
{
return new static($this->lazy()->until($value)->all());
}

/**
* Create a new collection consisting of every n-th element.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,27 @@ public function union($items)
return $this->passthru('union', func_get_args());
}

/**
* Take items in the collection until the given condition is met.
*
* @param mixed $key
* @return static
*/
public function until($value)
{
$callback = $this->useAsCallable($value) ? $value : $this->equality($value);

return new static(function () use ($callback) {
foreach ($this as $key => $item) {
if ($callback($item, $key)) {
break;
}

yield $key => $item;
}
});
}

/**
* Create a new collection consisting of every n-th element.
*
Expand Down
38 changes: 13 additions & 25 deletions src/Illuminate/Support/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,31 +722,6 @@ public function uniqueStrict($key = null)
return $this->unique($key, true);
}

/**
* Take items in the collection until condition is met.
*
* @param mixed $key
* @return static
*/
public function until($value)
{
$passed = [];

$callback = $this->useAsCallable($value) ? $value : function ($item) use ($value) {
return $item === $value;
};

foreach ($this as $key => $item) {
if ($callback($item, $key)) {
break;
}

$passed[$key] = $item;
}

return new static($passed);
}

/**
* Collect the values into a collection.
*
Expand Down Expand Up @@ -969,4 +944,17 @@ protected function valueRetriever($value)
return data_get($item, $value);
};
}

/**
* Make a function to check an item's equality.
*
* @param \Closure|mixed $value
* @return \Closure
*/
protected function equality($value)
{
return function ($item) use ($value) {
return $item === $value;
};
}
}
17 changes: 17 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,23 @@ public function testUnlessNotEmptyIsLazy()
});
}

public function testUntilIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
$collection->until(INF);
});

$this->assertEnumerates(10, function ($collection) {
$collection->until(10)->all();
});

$this->assertEnumerates(10, function ($collection) {
$collection->until(function ($item) {
return $item === 10;
})->all();
});
}

public function testUnwrapEnumeratesOne()
{
$this->assertEnumeratesOnce(function ($collection) {
Expand Down

0 comments on commit a4326d8

Please sign in to comment.