Skip to content

Fixed potential bug in dropWhile #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 12, 2023
4 changes: 3 additions & 1 deletion src/Array/dropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ function dropWhile(array $array, callable $predicate): array
$count = \count($array);
$length = 0;
$index = \key($array);
while ($length <= $count && $predicate($array[$index], $index, $array)) {
$value = \current($array);
while ($length <= $count && $predicate($value, $index, $array)) {
array_shift($array);
\reset($array);
$length++;
$index = \key($array);
$value = \current($array);
}

return $array;
Expand Down
20 changes: 20 additions & 0 deletions tests/Array/DropWhileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,25 @@ public function testDropWhile()
$this->assertSame([['user' => 'pebbles', 'active' => false]], dropWhile($users, function ($user) {
return $user['active'];
}));

$lines = [
'Processing report:',
'Processed: 1',
'Successful: 1',
'',
'',
];

$lines = dropWhile($lines, static function ($x) {
return trim((string) $x) !== '';
});

self::assertEquals(['', ''], $lines);

$lines = dropWhile($lines, static function ($x) {
return trim((string) $x) === '';
});

self::assertEmpty($lines);
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy