Skip to content

Commit

Permalink
Add mapKeys operation
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Oct 8, 2024
1 parent 872cdb5 commit a6447f8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Nexus/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ public function map(\Closure $predicate): self
}, [$this]);
}

/**
* @template UKey
*
* @param (\Closure(TKey): UKey) $predicate
*
* @return self<UKey, T>
*/
public function mapKeys(\Closure $predicate): self
{
return new self(static function (iterable $collection) use ($predicate): iterable {
foreach ($collection as $key => $item) {
yield $predicate($key) => $item;
}
}, [$this]);
}

/**
* @return self<int, T>
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Nexus/Collection/CollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @extends Operation\FilterWithKey<TKey, T>
* @extends Operation\Keys<TKey, T>
* @extends Operation\Map<TKey, T>
* @extends Operation\MapKeys<TKey, T>
* @extends Operation\Values<TKey, T>
*/
interface CollectionInterface extends
Expand All @@ -35,6 +36,7 @@ interface CollectionInterface extends
Operation\FilterWithKey,
Operation\Keys,
Operation\Map,
Operation\MapKeys,
Operation\Values
{
/**
Expand Down
35 changes: 35 additions & 0 deletions src/Nexus/Collection/Operation/MapKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Nexus framework.
*
* (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Nexus\Collection\Operation;

use Nexus\Collection\CollectionInterface;

/**
* @template TKey
* @template T
*/
interface MapKeys
{
/**
* Returns a new collection consisting of items transformed by the
* mapping function `$predicate` that accepts the keys as inputs.
*
* @template UKey
*
* @param (\Closure(TKey): UKey) $predicate
*
* @return CollectionInterface<UKey, T>
*/
public function mapKeys(\Closure $predicate): CollectionInterface;
}
10 changes: 10 additions & 0 deletions tests/Collection/AbstractCollectionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ public function testMap(): void
);
}

public function testMapKeys(): void
{
self::assertSame(
[7 => 5, 6 => 4, 5 => 6],
$this->collection(['bananas' => 5, 'apples' => 4, 'limes' => 6])
->mapKeys(static fn(string $key): int => \strlen($key))
->all(true),
);
}

public function testValues(): void
{
$collection = $this->collection(static function (): \Generator {
Expand Down
1 change: 1 addition & 0 deletions tests/Collection/data/collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@

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

0 comments on commit a6447f8

Please sign in to comment.