From 123c0ed2947fedf1fa2e441c1768a40c91f42b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lajos=20M=C3=A9sz=C3=A1ros?= Date: Thu, 20 May 2021 14:21:01 +0200 Subject: [PATCH] Added A::none(), A::all() and F::complement() --- README.md | 7 +++++++ src/A.php | 14 +++++++++++++- src/F.php | 11 +++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/F.php diff --git a/README.md b/README.md index 587e8b3..18e0bf3 100644 --- a/README.md +++ b/README.md @@ -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** - @@ -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. diff --git a/src/A.php b/src/A.php index e34809a..0495ce1 100644 --- a/src/A.php +++ b/src/A.php @@ -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] @@ -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 { diff --git a/src/F.php b/src/F.php new file mode 100644 index 0000000..d1b73f7 --- /dev/null +++ b/src/F.php @@ -0,0 +1,11 @@ + !$fn(...$args); + } +}