Skip to content

Commit 6a5add4

Browse files
authored
Merge pull request #6005 from JosephSilber/take-while-take-until
[7.x] Document `takeWhile` and `takeUntil`
2 parents 4747f5f + 7e37a08 commit 6a5add4

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

collections.md

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ For the remainder of this documentation, we'll discuss each method available on
164164
[split](#method-split)
165165
[sum](#method-sum)
166166
[take](#method-take)
167+
[takeUntil](#method-takeuntil)
168+
[takeWhile](#method-takewhile)
167169
[tap](#method-tap)
168170
[times](#method-times)
169171
[toArray](#method-toarray)
@@ -175,7 +177,6 @@ For the remainder of this documentation, we'll discuss each method available on
175177
[unless](#method-unless)
176178
[unlessEmpty](#method-unlessempty)
177179
[unlessNotEmpty](#method-unlessnotempty)
178-
[until](#method-until)
179180
[unwrap](#method-unwrap)
180181
[values](#method-values)
181182
[when](#method-when)
@@ -1959,6 +1960,50 @@ You may also pass a negative integer to take the specified amount of items from
19591960

19601961
// [4, 5]
19611962

1963+
<a name="method-takeuntil"></a>
1964+
#### `takeUntil()` {#collection-method}
1965+
1966+
The `takeUntil` method returns items in the collection until the given callback returns `true`:
1967+
1968+
$collection = collect([1, 2, 3, 4]);
1969+
1970+
$subset = $collection->takeUntil(function ($item) {
1971+
return $item >= 3;
1972+
});
1973+
1974+
$subset->all();
1975+
1976+
// [1, 2]
1977+
1978+
You may also pass a simple value to the `takeUntil` method to get the items until the given value is found:
1979+
1980+
$collection = collect([1, 2, 3, 4]);
1981+
1982+
$subset = $collection->until(3);
1983+
1984+
$subset->all();
1985+
1986+
// [1, 2]
1987+
1988+
> {note} If the given value is not found or the callback never returns `true`, the `takeUntil` method will return all items in the collection.
1989+
1990+
<a name="method-takewhile"></a>
1991+
#### `takeWhile()` {#collection-method}
1992+
1993+
The `takeWhile` method returns items in the collection until the given callback returns `false`:
1994+
1995+
$collection = collect([1, 2, 3, 4]);
1996+
1997+
$subset = $collection->takeWhile(function ($item) {
1998+
return $item < 3;
1999+
});
2000+
2001+
$subset->all();
2002+
2003+
// [1, 2]
2004+
2005+
> {note} If the callback never returns `false`, the `takeWhile` method will return all items in the collection.
2006+
19622007
<a name="method-tap"></a>
19632008
#### `tap()` {#collection-method}
19642009

@@ -2151,33 +2196,6 @@ Alias for the [`whenNotEmpty`](#method-whennotempty) method.
21512196

21522197
Alias for the [`whenEmpty`](#method-whenempty) method.
21532198

2154-
<a name="method-until"></a>
2155-
#### `until()` {#collection-method}
2156-
2157-
The `until` method returns items in the collection until the given value is found:
2158-
2159-
$collection = collect([1, 2, 3, 4]);
2160-
2161-
$subset = $collection->until(3);
2162-
2163-
$subset->all();
2164-
2165-
// [1, 2]
2166-
2167-
You may also pass a callback to the `until` method to perform your own logic. The callback should return `true` when the `until` method should stop.
2168-
2169-
$collection = collect([1, 2, 3, 4]);
2170-
2171-
$subset = $collection->until(function ($item) {
2172-
return $item >= 3;
2173-
});
2174-
2175-
$subset->all();
2176-
2177-
// [1, 2]
2178-
2179-
If the given value is not found or callback does not return `true`, the `until` method will return all items in the collection.
2180-
21812199
<a name="method-unwrap"></a>
21822200
#### `unwrap()` {#collection-method}
21832201

@@ -2580,7 +2598,7 @@ The `zip` method merges together the values of the given array with the values o
25802598
<a name="higher-order-messages"></a>
25812599
## Higher Order Messages
25822600

2583-
Collections also provide support for "higher order messages", which are short-cuts for performing common actions on collections. The collection methods that provide higher order messages are: [`average`](#method-average), [`avg`](#method-avg), [`contains`](#method-contains), [`each`](#method-each), [`every`](#method-every), [`filter`](#method-filter), [`first`](#method-first), [`flatMap`](#method-flatmap), [`groupBy`](#method-groupby), [`keyBy`](#method-keyby), [`map`](#method-map), [`max`](#method-max), [`min`](#method-min), [`partition`](#method-partition), [`reject`](#method-reject), [`some`](#method-some), [`sortBy`](#method-sortby), [`sortByDesc`](#method-sortbydesc), [`sum`](#method-sum), [`unique`](#method-unique), and [`until`](#method-until).
2601+
Collections also provide support for "higher order messages", which are short-cuts for performing common actions on collections. The collection methods that provide higher order messages are: [`average`](#method-average), [`avg`](#method-avg), [`contains`](#method-contains), [`each`](#method-each), [`every`](#method-every), [`filter`](#method-filter), [`first`](#method-first), [`flatMap`](#method-flatmap), [`groupBy`](#method-groupby), [`keyBy`](#method-keyby), [`map`](#method-map), [`max`](#method-max), [`min`](#method-min), [`partition`](#method-partition), [`reject`](#method-reject), [`some`](#method-some), [`sortBy`](#method-sortby), [`sortByDesc`](#method-sortbydesc), [`sum`](#method-sum) [`takeUntil`](#method-takeuntil), [`takeWhile`](#method-takewhile) and [`unique`](#method-unique).
25842602

25852603
Each higher order message can be accessed as a dynamic property on a collection instance. For instance, let's use the `each` higher order message to call a method on each object within a collection:
25862604

0 commit comments

Comments
 (0)