Skip to content

Commit

Permalink
Merge branch 'master' of github.com:meszaros-lajos-gyorgy/shovel
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Jul 27, 2021
2 parents 83d04e3 + 1ae6704 commit d95b1c7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
57 changes: 32 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
```
Expand Down Expand Up @@ -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!**

</details>

<details>
Expand All @@ -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!**

</details>

<details>
Expand Down
12 changes: 7 additions & 5 deletions src/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit d95b1c7

Please sign in to comment.