Skip to content

Commit 4565657

Browse files
committed
Make LazyCollection@until actually be lazy
1 parent 0808c65 commit 4565657

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

src/Illuminate/Support/Collection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,17 @@ public function union($items)
736736
return new static($this->items + $this->getArrayableItems($items));
737737
}
738738

739+
/**
740+
* Take items in the collection until the given condition is met.
741+
*
742+
* @param mixed $key
743+
* @return static
744+
*/
745+
public function until($value)
746+
{
747+
return new static($this->lazy()->until($value)->all());
748+
}
749+
739750
/**
740751
* Create a new collection consisting of every n-th element.
741752
*

src/Illuminate/Support/LazyCollection.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,27 @@ public function union($items)
737737
return $this->passthru('union', func_get_args());
738738
}
739739

740+
/**
741+
* Take items in the collection until the given condition is met.
742+
*
743+
* @param mixed $key
744+
* @return static
745+
*/
746+
public function until($value)
747+
{
748+
$callback = $this->useAsCallable($value) ? $value : $this->equality($value);
749+
750+
return new static(function () use ($callback) {
751+
foreach ($this as $key => $item) {
752+
if ($callback($item, $key)) {
753+
break;
754+
}
755+
756+
yield $key => $item;
757+
}
758+
});
759+
}
760+
740761
/**
741762
* Create a new collection consisting of every n-th element.
742763
*

src/Illuminate/Support/Traits/EnumeratesValues.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -722,31 +722,6 @@ public function uniqueStrict($key = null)
722722
return $this->unique($key, true);
723723
}
724724

725-
/**
726-
* Take items in the collection until condition is met.
727-
*
728-
* @param mixed $key
729-
* @return static
730-
*/
731-
public function until($value)
732-
{
733-
$passed = [];
734-
735-
$callback = $this->useAsCallable($value) ? $value : function ($item) use ($value) {
736-
return $item === $value;
737-
};
738-
739-
foreach ($this as $key => $item) {
740-
if ($callback($item, $key)) {
741-
break;
742-
}
743-
744-
$passed[$key] = $item;
745-
}
746-
747-
return new static($passed);
748-
}
749-
750725
/**
751726
* Collect the values into a collection.
752727
*

0 commit comments

Comments
 (0)