From 903deb40dddbbb53ebb277f5f5f38494ca2f6863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lajos=20M=C3=A9sz=C3=A1ros?= Date: Wed, 14 Apr 2021 17:47:50 +0200 Subject: [PATCH] Added A::findIndex, O::keys and O::values --- README.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/A.php | 17 +++++++++++- src/O.php | 20 ++++++++++++++ 3 files changed, 114 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6c0da2f..6e73c40 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ Every method is abide to the following rules ( or at least they should. if they - **null return values on error** - when an error happens and the underlying php function returns false (eg. end or strpos), then it's being normalized to null - **camelCase naming** +Plain numeric arrays are handled best via the methods in A, while associative arrays and objects are handled via the methods in O. + ## API ### Array @@ -99,8 +101,79 @@ Every method is abide to the following rules ( or at least they should. if they - **head** - - **first** - - **filter** - -- **find** - -- **any** - + +- **find** - calls the given function on the elements of an array and returns the value for the first match. if there's no match, it will return `null` + + ```php + $data = [ + ["a" => 8], + ["a" => 10], + ["a" => 12] + ]; + + $result = A::find(function($x) { + return $x["a"] === 10; + }, $data); + + // $result = ["a" => 10] + ``` + + ```php + $data = [ + ["a" => 8], + ["a" => 10], + ["a" => 12] + ]; + + $result = A::find(function($x) { + return $x["a"] === -4; + }, $data); + + // $result = null + ``` + +- **findIndex** - calls the given function on the elements of an array and returns the key for the first match. if there's no match it will return -1 + + ```php + $data = [ + ["a" => 8], + ["a" => 10], + ["a" => 12] + ]; + + $result = A::findIndex(function($x) { + return $x["a"] === 10; + }, $data); + + // $result = 1 + ``` + + ```php + $data = [ + ["a" => 8], + ["a" => 10], + ["a" => 12] + ]; + + $result = A::findIndex(function($x) { + return $x["a"] === -4; + }, $data); + + // $result = -1 + ``` + +- **any** - calls the given predicate function on the elements in the given array and returns true if for at least one of them the predicate returns true + + ```php + $data = [2, 3, 5, 6, 7, 9, 10]; + + $result = A::any(function($x) { + return $x % 5 === 0; + }, $data); + + // $result = true + ``` + - **includes** - - **slice** - - **join** - @@ -230,6 +303,9 @@ Every method is abide to the following rules ( or at least they should. if they O::has('y', $data); // false ``` +- **keys** - +- **values** - + ## 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 1741a33..a088e53 100644 --- a/src/A.php +++ b/src/A.php @@ -266,7 +266,22 @@ public static function find(callable $fn, array $data) return self::first($results); } - // A::find(x => x.a === 10, [['a' => 8], ['a' => 10]]) -> true + // A::findIndex(x => x.a === 10, [['a' => 8], ['a' => 10]]) -> 1 + public static function findIndex(callable $fn, array $data): int { + if (self::isEmpty($data)) { + return -1; + } + + foreach ($data as $key => $value) { + if ($fn($value)) { + return $key; + } + } + + return -1; + } + + // A::any(x => x.a === 10, [['a' => 8], ['a' => 10]]) -> true public static function any(callable $fn, array $data): bool { return self::find($fn, $data) !== null; diff --git a/src/O.php b/src/O.php index 0ed0e1f..cf432bb 100644 --- a/src/O.php +++ b/src/O.php @@ -60,4 +60,24 @@ public static function dissoc(string $key, object $data): object { public static function has(string $key, $data): bool { return array_key_exists($key, $data); } + + public static function keys($data): array { + if (self::isObject($data)) { + return A::keys(get_object_vars($data)); + } + if (A::isArray($data) && A::isAssoc($data)) { + return A::keys($data); + } + return []; + } + + public static function values($data): array { + if (self::isObject($data)) { + return A::values(get_object_vars($data)); + } + if (A::isArray($data) && A::isAssoc($data)) { + return A::values($data); + } + return []; + } }