diff --git a/README.md b/README.md index 7987e64..ea5bf4c 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 ``` @@ -881,6 +865,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 +892,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/A.php b/src/A.php index f7f3823..637e7a2 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'] @@ -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