From 2beaf7b3e185754a5ce3699d473a2891a3dfda67 Mon Sep 17 00:00:00 2001 From: Vytautas Smilingis Date: Thu, 19 Oct 2023 12:22:07 +0200 Subject: [PATCH] Added `reject()` method --- src/Concerns/EnumerableMethods.php | 14 ++++++++++++++ tests/DataCollectionTest.php | 11 +++++++++++ 2 files changed, 25 insertions(+) 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']);