Skip to content

Commit

Permalink
bug fix omg
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jul 17, 2024
1 parent d4a2c02 commit 608bc4f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Itera.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ public static function adjust(iterable $input, ?callable $values = null, ?callab
return self::reindex($input, $keys);
}
// Map both values and keys.
// Note: This method does not contain yield, thus is not a generator function itself, but returns generators.
// That's why the `remap` is separated into a private method.
return self::remap($input, $values, $keys);
}

private static function remap(iterable $input, callable $values, callable $keys): iterable
{
foreach ($input as $key => $value) {
yield $keys($value, $key) => $values($value, $key);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/map.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,34 @@ require_once __DIR__ . '/../vendor/autoload.php';
input: ['zero', 'one', 'two', 'three',],
description: 'Should map values and keys based on values and keys',
);

// Actually we may use the same test for `adjust`.
DashTest::assert(
callChain: [
new Call(
method: 'adjust',
keys: fn($v, $k) => 'the key for ' . $v . ' is ' . $k,
),
new Call(
method: 'adjust',
values: fn($v, $k) => 'but the key for ' . $v . ' has since changed to "' . $k . '"',
),
],
assertion: function (iterable $result, ?string $desc) {
Assert::same(
[
'the key for zero is 0' => 'but the key for zero has since changed to "the key for zero is 0"',
'the key for one is 1' => 'but the key for one has since changed to "the key for one is 1"',
'the key for two is 2' => 'but the key for two has since changed to "the key for two is 2"',
'the key for three is 3' => 'but the key for three has since changed to "the key for three is 3"',
],
Itera::toArray($result),
$desc,
);
},
input: ['zero', 'one', 'two', 'three',],
description: 'Should map values and keys based on values and keys',
);
})();

(function () {
Expand Down

0 comments on commit 608bc4f

Please sign in to comment.