Skip to content

Commit 2960b4d

Browse files
author
Fureev Eugene
committed
feat: add eachValue
1 parent 0dd47fe commit 2960b4d

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher
99
### Added
1010

1111
- Add global method `mapValue` Returns an array containing the results of applying func to the items of the $collection
12+
- Add global method `eachValue` Apply a $fn to all the items of the $collection
1213

1314
## v4.14.0
1415

src/Global/base.php

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ function mapValue(callable $fn, iterable $collection, mixed ...$args): array
3030
}
3131
}
3232

33+
if (!function_exists('eachValue')) {
34+
function eachValue(callable $fn, iterable $collection, mixed ...$args): void
35+
{
36+
foreach ($collection as $key => $value) {
37+
$fn($value, $key, ...$args);
38+
}
39+
}
40+
}
41+
3342
if (!function_exists('when')) {
3443
/**
3544
* Returns a value when a condition is truthy.

tests/Global/EachValueTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Support\Tests\Global;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class EachValueTest extends TestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
public function eachValue(): void
15+
{
16+
$result = [];
17+
$fnColl = static function (string $value) use (&$result) {
18+
$result[] = $value;
19+
};
20+
$expect = ['test', 'app'];
21+
eachValue($fnColl, $expect);
22+
23+
self::assertEquals($expect, $result);
24+
}
25+
}

0 commit comments

Comments
 (0)