Skip to content

Commit

Permalink
Update filter operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 22, 2020
1 parent b65cffe commit 8374728
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 5 additions & 2 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,11 @@ static function ($value) {
return mb_strlen($value) - 1 === mb_strpos($value, 'o');
}
)
->normalize()
->shouldIterateAs(['allo']);
->shouldIterateAs([2 => 'allo']);

$this::fromIterable([true, false, 0, '', null])
->filter()
->shouldIterateAs([true]);
}

public function it_can_flatten(): void
Expand Down
8 changes: 7 additions & 1 deletion src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ final class Filter extends AbstractOperation implements Operation
{
public function __construct(callable ...$callbacks)
{
$this->storage['callbacks'] = $callbacks;
$defaultCallback = static function ($item): bool {
return true === (bool) $item;
};

$this->storage['callbacks'] = [] === $callbacks ?
[$defaultCallback] :
$callbacks;
}

public function __invoke(): Closure
Expand Down

0 comments on commit 8374728

Please sign in to comment.