From 7004381112a5a1a91523895b889f06c72fb2d649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Lajos?= Date: Sun, 25 Jul 2021 02:21:03 +0200 Subject: [PATCH 1/3] Fixed mixed up A::append() and A::prepend() --- src/A.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/A.php b/src/A.php index f7f3823..d8c62ce 100644 --- a/src/A.php +++ b/src/A.php @@ -108,13 +108,13 @@ public static function ensureArray($data): array // A::append([4, 5, 6], [1, 2]) -> [1, 2, 4, 5, 6] public static function append($value, array $data): array { - return self::concat($data, $value); + return self::concat($value, $data); } // A::prepend([4, 5, 6], [1, 2]) -> [4, 5, 6, 1, 2] public static function prepend($value, array $data): array { - return self::concat($value, $data); + return self::concat($data, $value); } // A::pluck('color', [['color' => 'red', ...], ['color' => 'green', ...]]) -> ['red', 'green'] From 48147745f5fe4378b3fb3175078f9009f71872bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Lajos?= Date: Sun, 25 Jul 2021 02:26:52 +0200 Subject: [PATCH 2/3] Added associative array support for O::assoc and O::dissoc --- README.md | 23 +++++++++++++++++++++++ src/O.php | 16 ++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f00443a..a95807d 100644 --- a/README.md +++ b/README.md @@ -881,6 +881,17 @@ $point2d->y = 20; $point3d = O::assoc("z", 30, $point2d); // {"x": 10, "y": 20, "z": 30} ``` +```php +$point2d = [ + "x" => 10, + "y" => 20 +]; + +$point3d = O::assoc("z", 30, $point2d); // ["x" => 10, "y" => 20, "z" => 30] +``` + +**Does not work on arrays with numeric keys!** +
@@ -897,6 +908,18 @@ $point3d->z = 30; $point2d = O::dissoc("z", 30, $point3d); // {"x": 10, "y": 20} ``` +```php +$point3d = [ + "x" => 10, + "y" => 20, + "z" => 30 +]; + +$point2d = O::dissoc("z", 30, $point3d); // ["x" => 10, "y" => 30] +``` + +**Does not work on arrays with numeric keys!** +
diff --git a/src/O.php b/src/O.php index 8315510..611aedc 100644 --- a/src/O.php +++ b/src/O.php @@ -45,14 +45,22 @@ function ($result, $key) use ($data) { } // O::assoc('foo', 'bar', {}) -> {foo: 'bar'} - public static function assoc(string $key, $value, object $data): object { - $data->{$key} = $value; + public static function assoc(string $key, $value, $data): object { + if (O::isObject($data)) { + $data->{$key} = $value; + } elseif(A::isArray($data) && A::isAssoc($data)) { + $data[$key] = $value; + } return $data; } // O::dissoc('foo', {foo: 'bar', fizz: 'buzz'}) -> {fizz: 'buzz'} - public static function dissoc(string $key, object $data): object { - unset($data->{$key}); + public static function dissoc(string $key, $data): object { + if (O::isObject($data)) { + unset($data->{$key}); + } elseif(A::isArray($data) && A::isAssoc($data)) { + unset($data[$key]); + } return $data; } From 1ae6704a6f644767f6165059ee7fad57dc4c59ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Lajos?= Date: Sun, 25 Jul 2021 17:07:02 +0200 Subject: [PATCH 3/3] Fixed incorrect index calculation for A::findLastIndex() --- README.md | 34 +++++++++------------------------- src/A.php | 8 +++++--- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index a95807d..32138e6 100644 --- a/README.md +++ b/README.md @@ -436,25 +436,17 @@ $result = A::findLast(fn($x) => $x["a"] === -4, $data); #### 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 `null` ```php -$data = [ - ["a" => 8], - ["a" => 10], - ["a" => 12] -]; +$data = [1, 1, 1, 0, 0, 0, 0, 0]; -$result = A::findIndex(fn($x) => $x["a"] === 10, $data); +$result = A::findIndex(fn($x) => $x === 0, $data); -// $result = 1 +// $result = 3 ``` ```php -$data = [ - ["a" => 8], - ["a" => 10], - ["a" => 12] -]; +$data = [1, 1, 1, 0, 0, 0, 0, 0]; -$result = A::findIndex(fn($x) => $x["a"] === -4, $data); +$result = A::findIndex(fn($x) => $x === 2, $data); // $result = null ``` @@ -467,25 +459,17 @@ $result = A::findIndex(fn($x) => $x["a"] === -4, $data); #### calls the given function on the elements of an array and returns the key for the last match. if there's no match it will return `null` ```php -$data = [ - ["a" => 8], - ["a" => 10], - ["a" => 12] -]; +$data = [1, 1, 1, 0, 0, 0, 0, 0]; -$result = A::findLastIndex(fn($x) => $x["a"] > 3, $data); +$result = A::findLastIndex(fn($x) => $x === 1, $data); // $result = 2 ``` ```php -$data = [ - ["a" => 8], - ["a" => 10], - ["a" => 12] -]; +$data = [1, 1, 1, 0, 0, 0, 0, 0]; -$result = A::findLastIndex(fn($x) => $x["a"] > 500, $data); +$result = A::findLastIndex(fn($x) => $x === 2, $data); // $result = null ``` diff --git a/src/A.php b/src/A.php index d8c62ce..637e7a2 100644 --- a/src/A.php +++ b/src/A.php @@ -267,7 +267,7 @@ public static function findLast(callable $fn, array $data) { return self::find($fn, self::reverse($data)); } - // A::findIndex(x => x.a === 10, [['a' => 8], ['a' => 10]]) -> 1 + // A::findIndex(x => x === 1, [1, 1, 1, 0, 0, 0, 0, 0]) -> 3 public static function findIndex(callable $fn, array $data): ?int { if (self::isEmpty($data)) { return null; @@ -282,10 +282,12 @@ public static function findIndex(callable $fn, array $data): ?int { return null; } - // A::findLastIndex(x => x.a > 3, [['a' => 8], ['a' => 10]]) -> 1 + // A::findLastIndex(x => x === 1, [1, 1, 1, 0, 0, 0, 0, 0]) -> 2 public static function findLastIndex(callable $fn, array $data): ?int { - return self::findIndex($fn, self::reverse($data)); + $size = A::length($data); + $idx = self::findIndex($fn, self::reverse($data)); + return $idx === null ? null : $size - $idx - 1; } // A::any(x => x.a === 10, [['a' => 8], ['a' => 10]]) -> true