From 608bc4fca5ae1bf9cc9dbbbf9830c576d2ad63b0 Mon Sep 17 00:00:00 2001 From: Andrej Rypo Date: Wed, 17 Jul 2024 20:56:48 +0200 Subject: [PATCH] bug fix omg --- src/Itera.php | 7 +++++++ tests/map.phpt | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Itera.php b/src/Itera.php index 5fc1a79..82a9530 100644 --- a/src/Itera.php +++ b/src/Itera.php @@ -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); } diff --git a/tests/map.phpt b/tests/map.phpt index 127ab42..ced1615 100644 --- a/tests/map.phpt +++ b/tests/map.phpt @@ -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 () {