From d2bfba5c4368e79df83e7708c2f4470af895b72d Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Thu, 16 Jul 2020 04:46:37 +0900 Subject: [PATCH] Mappers should try to preserve keys Fixes #84 Tests that keys are preserved in fact --- src/Principal.php | 12 +++++++----- tests/CastTest.php | 14 ++++++++++++++ tests/MapTest.php | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 tests/MapTest.php diff --git a/src/Principal.php b/src/Principal.php index c5c87af..6f257bb 100644 --- a/src/Principal.php +++ b/src/Principal.php @@ -87,16 +87,18 @@ protected function map(callable $func) private static function apply(iterable $previous, callable $func): iterable { - foreach ($previous as $value) { + foreach ($previous as $key => $value) { $result = $func($value); + + // For generators we use keys they provide if ($result instanceof \Generator) { yield from $result; continue; } - // Case of a plain old mapping function - yield $result; + // In case of a plain old mapping function we use the original key + yield $key => $result; } } @@ -131,8 +133,8 @@ protected function cast(callable $func) private static function applyOnce(iterable $previous, callable $func): iterable { - foreach ($previous as $value) { - yield $func($value); + foreach ($previous as $key => $value) { + yield $key => $func($value); } } diff --git a/tests/CastTest.php b/tests/CastTest.php index c6a77b4..284ef95 100644 --- a/tests/CastTest.php +++ b/tests/CastTest.php @@ -88,4 +88,18 @@ public function testCastUnpack(): void $this->assertSame([0, 2, 6], $pipeline->toArray()); } + + public function testCastPreservesKeys(): void + { + $this->assertSame([ + 'a' => 2, + 'b' => 6, + ], map(function () { + yield 'a' => 1; + yield 'b' => 2; + yield 'b' => 3; + })->cast(function (int $a) { + return $a * 2; + })->toArray(true)); + } } diff --git a/tests/MapTest.php b/tests/MapTest.php new file mode 100644 index 0000000..50dc9a3 --- /dev/null +++ b/tests/MapTest.php @@ -0,0 +1,46 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types=1); + +namespace Tests\Pipeline; + +use PHPUnit\Framework\TestCase; +use function Pipeline\map; + +/** + * @covers \Pipeline\Principal + * @covers \Pipeline\Standard + * + * @internal + */ +final class MapTest extends TestCase +{ + public function testMapPreservesKeys(): void + { + $this->assertSame([ + 'a' => 2, + 'b' => 6, + ], map(function () { + yield 'a' => 1; + yield 'b' => 2; + yield 'b' => 3; + })->map(function (int $a) { + return $a * 2; + })->toArray(true)); + } +}