diff --git a/src/Concerns/EnumerableMethods.php b/src/Concerns/EnumerableMethods.php index 01633413..1a123a3d 100644 --- a/src/Concerns/EnumerableMethods.php +++ b/src/Concerns/EnumerableMethods.php @@ -52,6 +52,20 @@ public function filter(callable $filter): static return $cloned; } + /** + * @param callable(TValue): bool $filter + * + * @return static + */ + public function reject(callable $filter): static + { + $cloned = clone $this; + + $cloned->items = $cloned->items->reject($filter); + + return $cloned; + } + /** * @template TFirstDefault * diff --git a/tests/DataCollectionTest.php b/tests/DataCollectionTest.php index 210522f4..9732123b 100644 --- a/tests/DataCollectionTest.php +++ b/tests/DataCollectionTest.php @@ -79,6 +79,17 @@ ->toMatchArray($filtered); }); +test('a collection can be rejected', function () { + $collection = SimpleData::collection(['A', 'B']); + + $filtered = $collection->reject(fn (SimpleData $data) => $data->string === 'B')->toArray(); + + expect([ + ['string' => 'A'], + ]) + ->toMatchArray($filtered); +}); + test('a collection can be transformed', function () { $collection = SimpleData::collection(['A', 'B']);