Skip to content

Commit

Permalink
fix: Fix Has operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Oct 8, 2020
1 parent 69541f6 commit 8af7817
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
40 changes: 34 additions & 6 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1072,15 +1072,43 @@ public function it_can_groupBy(): void
public function it_can_has(): void
{
$this::fromIterable(range('A', 'C'))
->has(static function ($key, $value) {
return 'A';
})
->has(
static function ($key, $value) {
return 'A';
}
)
->shouldIterateAs([true]);

$this::fromIterable(range('A', 'C'))
->has(static function ($key, $value) {
return 'Z';
})
->has(
static function ($key, $value) {
return 'Z';
}
)
->shouldIterateAs([false]);

$this::fromIterable(['b', 1, 'foo', 'bar'])
->has(
static function ($key, $value) {
return 'foo';
}
)
->shouldIterateAs([2 => true]);

$this::fromIterable(['b', 1, 'foo', 'bar'])
->has(
static function ($key, $value) {
return 'unknown';
}
)
->shouldIterateAs([false]);

$this::empty()
->has(
static function ($key, $value) {
return $value;
}
)
->shouldIterateAs([false]);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Operation/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,19 @@ static function ($value, $key) use ($callback): bool {
return $callback($key, $value) === $value;
};

$dropWhileCallback =
/**
* @param mixed $value
* @psalm-param T $value
*/
static function ($value): bool {
return false === $value;
};

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
$pipe = Pipe::of()(
Map::of()($mapCallback),
DropWhile::of()($dropWhileCallback),
Append::of()(false),
Head::of()
);
Expand Down

0 comments on commit 8af7817

Please sign in to comment.