File tree Expand file tree Collapse file tree 4 files changed +62
-25
lines changed Expand file tree Collapse file tree 4 files changed +62
-25
lines changed Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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 *
@@ -969,4 +944,17 @@ protected function valueRetriever($value)
969944 return data_get ($ item , $ value );
970945 };
971946 }
947+
948+ /**
949+ * Make a function to check an item's equality.
950+ *
951+ * @param \Closure|mixed $value
952+ * @return \Closure
953+ */
954+ protected function equality ($ value )
955+ {
956+ return function ($ item ) use ($ value ) {
957+ return $ item === $ value ;
958+ };
959+ }
972960}
Original file line number Diff line number Diff line change @@ -1100,6 +1100,23 @@ public function testUnlessNotEmptyIsLazy()
11001100 });
11011101 }
11021102
1103+ public function testUntilIsLazy ()
1104+ {
1105+ $ this ->assertDoesNotEnumerate (function ($ collection ) {
1106+ $ collection ->until (INF );
1107+ });
1108+
1109+ $ this ->assertEnumerates (10 , function ($ collection ) {
1110+ $ collection ->until (10 )->all ();
1111+ });
1112+
1113+ $ this ->assertEnumerates (10 , function ($ collection ) {
1114+ $ collection ->until (function ($ item ) {
1115+ return $ item === 10 ;
1116+ })->all ();
1117+ });
1118+ }
1119+
11031120 public function testUnwrapEnumeratesOne ()
11041121 {
11051122 $ this ->assertEnumeratesOnce (function ($ collection ) {
You can’t perform that action at this time.
0 commit comments