Skip to content

Commit 847c716

Browse files
committed
Add Collection.reject function
1 parent cb6557e commit 847c716

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/Collection/reject.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2017
10+
*/
11+
12+
namespace _;
13+
14+
use function _\internal\baseIteratee;
15+
16+
/**
17+
* The opposite of `filter` this method returns the elements of `collection`
18+
* that `predicate` does **not** return truthy for.
19+
*
20+
* @category Collection
21+
*
22+
* @param iterable $collection The collection to iterate over.
23+
* @param callable $predicate The function invoked per iteration.
24+
*
25+
* @return array the new filtered array.
26+
*
27+
* @example
28+
* <code>
29+
* $users = [
30+
* ['user' => 'barney', 'active' => true],
31+
* ['user' => 'fred', 'active' => false]
32+
* ]
33+
*
34+
* reject($users, 'active')
35+
* // => objects for ['fred']
36+
* </code>
37+
*/
38+
function reject(iterable $collection, $predicate = null): array
39+
{
40+
return filter($collection, negate(baseIteratee($predicate)));
41+
}

tests/Collection/RejectTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2017
10+
*/
11+
12+
use PHPUnit\Framework\TestCase;
13+
use function _\reject;
14+
15+
class RejectTest extends TestCase
16+
{
17+
public function testReject()
18+
{
19+
$users = [
20+
['user' => 'barney', 'active' => true],
21+
['user' => 'fred', 'active' => false],
22+
];
23+
24+
$this->assertSame([['user' => 'fred', 'active' => false]], reject($users, 'active'));
25+
}
26+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy