Skip to content

Commit

Permalink
Added A::findIndex, O::keys and O::values
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Apr 14, 2021
1 parent 255c8d2 commit 903deb4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
80 changes: 78 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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** -
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion src/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/O.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
}

0 comments on commit 903deb4

Please sign in to comment.