Skip to content

Commit

Permalink
refactor: Update Compact operation.
Browse files Browse the repository at this point in the history
Compact now remove the following falsy values: 0, false, null, ' '

BREAKING CHANGE: yes
  • Loading branch information
drupol committed Sep 22, 2020
1 parent 7211052 commit 10fd55f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,15 @@ public function it_can_combine(): void

public function it_can_compact(): void
{
$input = ['a', 1 => 'b', null, false, 0, 'c'];
$input = ['a', 1 => 'b', null, false, 0, 'c', ''];

$this::fromIterable($input)
->compact()
->shouldIterateAs(['a', 1 => 'b', 3 => false, 4 => 0, 5 => 'c']);
->shouldIterateAs(['a', 1 => 'b', 5 => 'c']);

$this::fromIterable($input)
->compact(null, 0)
->shouldIterateAs(['a', 1 => 'b', 3 => false, 5 => 'c']);
->shouldIterateAs(['a', 1 => 'b', 3 => false, 5 => 'c', '']);
}

public function it_can_contains(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static function (Iterator $iterator) use ($values): Generator {
};

/** @psalm-var callable(Iterator<TKey, T>):Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallback([] === $values ? [null] : $values));
$filter = Filter::of()($filterCallback([] === $values ? [null, [], 0, false, ''] : $values));

return yield from $filter($iterator);
};
Expand Down

0 comments on commit 10fd55f

Please sign in to comment.