Skip to content

Commit

Permalink
Added reject() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Plytas committed Oct 19, 2023
1 parent f47d3f0 commit 2beaf7b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Concerns/EnumerableMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
11 changes: 11 additions & 0 deletions tests/DataCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down

0 comments on commit 2beaf7b

Please sign in to comment.