Skip to content

Commit

Permalink
Added A::none(), A::all() and F::complement()
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed May 20, 2021
1 parent 5cecbe8 commit 123c0ed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ Plain numeric arrays are handled best via the methods in A, while associative ar
// $result = true
```

- **none** -
- **all** -

- **includes** -
- **contains** -
- **slice** -
Expand Down Expand Up @@ -529,6 +532,10 @@ Plain numeric arrays are handled best via the methods in A, while associative ar
- **keys** -
- **values** -

### Functions

- **complement** -

## Future plans

I keep adding methods as I come across the need for them, so if you're missing a method you'd use, then 1) PRs are welcome, 2) Issues are welcome.
Expand Down
14 changes: 13 additions & 1 deletion src/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public static function filter(callable $fn, array $data): array

public static function reject(callable $fn, array $data): array
{
return self::filter(fn($value) => !$fn($value), $data);
return self::filter(F::complement($fn), $data);
}

// A::find(x => x.a > 3, [['a' => 8], ['a' => 10]]) -> ['a' => 8]
Expand Down Expand Up @@ -306,6 +306,18 @@ public static function any(callable $fn, array $data): bool
return self::find($fn, $data) !== null;
}

// A::none(x => x % 2 === 1, [0, 2, 4, 6]) -> true
public static function none(callable $fn, array $data): bool
{
return !self::any($fn, $data);
}

// A::all(x => x % 2 === 0, [0, 2, 4, 6]) -> true
public static function all(callable $fn, array $data): bool
{
return !self::any(F::complement($fn), $data);
}

// A::includes('baz', ['foo', 'bar', 'baz']) -> true
public static function includes($value, array $data): bool
{
Expand Down
11 changes: 11 additions & 0 deletions src/F.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Shovel;

class F
{
public static function complement(callable $fn): callable
{
return fn(...$args) => !$fn(...$args);
}
}

0 comments on commit 123c0ed

Please sign in to comment.