Skip to content

Commit

Permalink
Added A::without()
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Jul 20, 2021
1 parent ae69d67 commit 8a8203a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ A::isAssoc(["x" => 10, "y" => 20]); // true
<details>
<summary>A::sum</summary>

#### adds up the numbers in the given array and returns the sum

```php
A::sum([1, 2, 3, 4, 5]); // 15
```

</details>

<details>
Expand Down Expand Up @@ -556,6 +562,25 @@ A::concat([1, 2], 3, [4, 5]); // [1, 2, 3, 4, 5]

</details>

<details>
<summary>A::without</summary>

#### removes items from the second array by values in the first array. if first value is not an array, then it is transformed into one

```php
A::without([1, 3], [1, 2, 3, 4, 5]); // [2, 4, 5]
```

```php
A::without(['a' => 12], [1, 2, 3, 4, ['a' => 12]]); // [1, 2, 3, 4]
```

```php
A::without(['t'], ['t', 'f', 'f', 't', 'f']); // ['f', 'f', 'f']
```

</details>

### String

> Most string operations come with an optional 3rd parameter called $caseSensitivity,
Expand Down
14 changes: 13 additions & 1 deletion src/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function keys(array $data): array
return array_keys($data);
}

// A::values([3, 6, 7]) -> [3, 6, 7]
// A::values([1 => 3, 3 => 6, 4 => 7]) -> [3, 6, 7]
public static function values(array $data): array
{
return array_values($data);
Expand Down Expand Up @@ -359,4 +359,16 @@ public static function zipObj(array $keys, array $values): object {
$_keys = self::filter(fn($key) => array_key_exists($key, $keys) && array_key_exists($key, $values), $_keys);
return self::reduce(fn($result, $key) => O::assoc($keys[$key], $values[$key], $result), new stdClass(), $_keys);
}

// A::without([1, 3], [1, 2, 3, 4, 5]) -> [2, 4, 5]
public static function without($excludedItems, array $values): array {
return self::values(self::reduce(function($values, $excludedItem) {
if (self::includes($excludedItem, $values)) {
$index = self::findIndex(fn($value) => $value === $excludedItem, $values);
unset($values[$index]);
}

return $values;
}, $values, self::ensureArray($excludedItems)));
}
}

0 comments on commit 8a8203a

Please sign in to comment.