Skip to content

Commit a6447f8

Browse files
committed
Add mapKeys operation
1 parent 872cdb5 commit a6447f8

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

src/Nexus/Collection/Collection.php

+16
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ public function map(\Closure $predicate): self
158158
}, [$this]);
159159
}
160160

161+
/**
162+
* @template UKey
163+
*
164+
* @param (\Closure(TKey): UKey) $predicate
165+
*
166+
* @return self<UKey, T>
167+
*/
168+
public function mapKeys(\Closure $predicate): self
169+
{
170+
return new self(static function (iterable $collection) use ($predicate): iterable {
171+
foreach ($collection as $key => $item) {
172+
yield $predicate($key) => $item;
173+
}
174+
}, [$this]);
175+
}
176+
161177
/**
162178
* @return self<int, T>
163179
*/

src/Nexus/Collection/CollectionInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @extends Operation\FilterWithKey<TKey, T>
2525
* @extends Operation\Keys<TKey, T>
2626
* @extends Operation\Map<TKey, T>
27+
* @extends Operation\MapKeys<TKey, T>
2728
* @extends Operation\Values<TKey, T>
2829
*/
2930
interface CollectionInterface extends
@@ -35,6 +36,7 @@ interface CollectionInterface extends
3536
Operation\FilterWithKey,
3637
Operation\Keys,
3738
Operation\Map,
39+
Operation\MapKeys,
3840
Operation\Values
3941
{
4042
/**
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Nexus framework.
7+
*
8+
* (c) John Paul E. Balandan, CPA <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Nexus\Collection\Operation;
15+
16+
use Nexus\Collection\CollectionInterface;
17+
18+
/**
19+
* @template TKey
20+
* @template T
21+
*/
22+
interface MapKeys
23+
{
24+
/**
25+
* Returns a new collection consisting of items transformed by the
26+
* mapping function `$predicate` that accepts the keys as inputs.
27+
*
28+
* @template UKey
29+
*
30+
* @param (\Closure(TKey): UKey) $predicate
31+
*
32+
* @return CollectionInterface<UKey, T>
33+
*/
34+
public function mapKeys(\Closure $predicate): CollectionInterface;
35+
}

tests/Collection/AbstractCollectionTestCase.php

+10
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ public function testMap(): void
109109
);
110110
}
111111

112+
public function testMapKeys(): void
113+
{
114+
self::assertSame(
115+
[7 => 5, 6 => 4, 5 => 6],
116+
$this->collection(['bananas' => 5, 'apples' => 4, 'limes' => 6])
117+
->mapKeys(static fn(string $key): int => \strlen($key))
118+
->all(true),
119+
);
120+
}
121+
112122
public function testValues(): void
113123
{
114124
$collection = $this->collection(static function (): \Generator {

tests/Collection/data/collection.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@
4545

4646
assertType('Nexus\Collection\Collection<int, int>', Collection::wrap([10, 11])->map(static fn(int $item): int => $item ** 2));
4747
assertType('Nexus\Collection\Collection<int, string>', Collection::wrap([1])->map(static fn(int $item): string => $item > 1 ? 'Yes' : 'No'));
48+
assertType('Nexus\Collection\Collection<int, int>', Collection::wrap(['apples' => 2])->mapKeys(static fn(string $key): int => \strlen($key)));

0 commit comments

Comments
 (0)