Skip to content

Commit

Permalink
Merge pull request #14 from samsonasik/check-string-key
Browse files Browse the repository at this point in the history
[Finder] Fix string key on rows get when preserveKey disabled
  • Loading branch information
samsonasik authored Mar 6, 2024
2 parents 14cc25b + b6e8547 commit 79e3107
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function current;
use function end;
use function is_string;
use function iterator_to_array;
use function key;
use function prev;
Expand Down Expand Up @@ -137,8 +138,14 @@ public static function rows(iterable $data, callable $filter, bool $preserveKey
continue;
}

$rows[$preserveKey ? $key : $newKey] = $datum;
++$newKey;
if (is_string($key)) {
$rowKey = $key;
} else {
$rowKey = $preserveKey ? $key : $newKey;
++$newKey;
}

$rows[$rowKey] = $datum;
}

return $rows;
Expand Down
5 changes: 5 additions & 0 deletions tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ public static function rowsDataProvider(): array
static fn($datum): bool => $datum < 5,
[],
],
[
[6, 7, 'foo' => 8, 9],
static fn($datum): bool => $datum > 7,
['foo' => 8, 9],
],
];
}

Expand Down

0 comments on commit 79e3107

Please sign in to comment.