Skip to content

Commit

Permalink
refactor: Update Falsy, Nullsy and Truthy operations.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: yes
  • Loading branch information
drupol committed Oct 7, 2020
1 parent 42934a9 commit 07fbce4
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 55 deletions.
34 changes: 21 additions & 13 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,19 +457,19 @@ public function it_can_contains(): void
{
$this::fromIterable(range('A', 'C'))
->contains('A')
->shouldReturn(true);
->shouldIterateAs([true]);

$this::fromIterable(range('A', 'C'))
->contains('unknown')
->shouldReturn(false);
->shouldIterateAs([false]);

$this::fromIterable(range('A', 'C'))
->contains('C', 'A')
->shouldReturn(true);
->shouldIterateAs([true]);

$this::fromIterable(range('A', 'C'))
->contains('C', 'unknown', 'A')
->shouldReturn(false);
->shouldIterateAs([false]);
}

public function it_can_convert_use_a_string_as_parameter(): void
Expand Down Expand Up @@ -686,15 +686,15 @@ public function it_can_falsy(): void
{
$this::fromIterable([false, false, false])
->falsy()
->shouldReturn(true);
->shouldIterateAs([true]);

$this::fromIterable([false, true, false])
->falsy()
->shouldReturn(false);
->shouldIterateAs([1 => false]);

$this::fromIterable([0, [], ''])
->falsy()
->shouldReturn(true);
->shouldIterateAs([true]);
}

public function it_can_filter(): void
Expand Down Expand Up @@ -1439,11 +1439,19 @@ public function it_can_nullsy(): void
{
$this::fromIterable([null, null, null])
->nullsy()
->shouldReturn(true);
->shouldIterateAs([true]);

$this::fromIterable([null, 0, null])
->nullsy()
->shouldReturn(false);
->shouldIterateAs([true]);

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

$this::fromIterable([null, [], 0, false, '', 'foo'])
->nullsy()
->shouldIterateAs([5 => false]);
}

public function it_can_pack(): void
Expand Down Expand Up @@ -2142,19 +2150,19 @@ public function it_can_truthy(): void
{
$this::fromIterable([true, true, true])
->truthy()
->shouldReturn(true);
->shouldIterateAs([true]);

$this::fromIterable([true, false, true])
->truthy()
->shouldReturn(false);
->shouldIterateAs([1 => false]);

$this::fromIterable([1, 2, 3])
->truthy()
->shouldReturn(true);
->shouldIterateAs([true]);

$this::fromIterable([1, 2, 3, 0])
->truthy()
->shouldReturn(false);
->shouldIterateAs([3 => false]);
}

public function it_can_unfold(): void
Expand Down
12 changes: 6 additions & 6 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ public function explode(...$explodes): CollectionInterface
return $this->pipe(Explode::of()(...$explodes));
}

public function falsy(): bool
public function falsy(): CollectionInterface
{
return $this->pipe(Falsy::of())->getIterator()->current();
return $this->pipe(Falsy::of());
}

public function filter(callable ...$callbacks): CollectionInterface
Expand Down Expand Up @@ -629,9 +629,9 @@ public function nth(int $step, int $offset = 0): CollectionInterface
return $this->pipe(Nth::of()($step)($offset));
}

public function nullsy(): bool
public function nullsy(): CollectionInterface
{
return $this->pipe(Nullsy::of())->getIterator()->current();
return $this->pipe(Nullsy::of());
}

public function pack(): CollectionInterface
Expand Down Expand Up @@ -787,9 +787,9 @@ public function transpose(): CollectionInterface
return $this->pipe(Transpose::of());
}

public function truthy(): bool
public function truthy(): CollectionInterface
{
return $this->pipe(Truthy::of())->getIterator()->current();
return $this->pipe(Truthy::of());
}

public static function unfold(callable $callback, ...$parameters): CollectionInterface
Expand Down
3 changes: 3 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
* @template-extends Duplicateable<TKey, T>
* @template-extends Everyable<TKey, T>
* @template-extends Explodeable<TKey, T>
* @template-extends Falsyable<int, bool>
* @template-extends Filterable<TKey, T>
* @template-extends Firstable<TKey, T>
* @template-extends Flattenable<TKey, T>
Expand Down Expand Up @@ -163,6 +164,7 @@
* @template-extends Mergeable<TKey, T>
* @template-extends Normalizeable<TKey, T>
* @template-extends Nthable<TKey, T>
* @template-extends Nullsyable<int, bool>
* @template-extends Packable<TKey, T>
* @template-extends Padable<TKey, T>
* @template-extends Pairable<TKey, T>
Expand Down Expand Up @@ -190,6 +192,7 @@
* @template-extends Tailsable<TKey, T>
* @template-extends TakeWhileable<TKey, T>
* @template-extends Transposeable<TKey, T>
* @template-extends Truthyable<int, bool>
* @template-extends Unlinesable<TKey, T>
* @template-extends Unpackable<TKey, T>
* @template-extends Unpairable<TKey, T>
Expand Down
12 changes: 11 additions & 1 deletion src/Contract/Operation/Falsyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Falsyable
{
public function falsy(): bool;
/**
* @psalm-return \loophp\collection\Contract\Collection<int, bool>
*/
public function falsy(): Collection;
}
12 changes: 11 additions & 1 deletion src/Contract/Operation/Nullsyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Nullsyable
{
public function nullsy(): bool;
/**
* @psalm-return \loophp\collection\Contract\Collection<int, bool>
*/
public function nullsy(): Collection;
}
12 changes: 11 additions & 1 deletion src/Contract/Operation/Truthyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Truthyable
{
public function truthy(): bool;
/**
* @psalm-return \loophp\collection\Contract\Collection<int, bool>
*/
public function truthy(): Collection;
}
35 changes: 24 additions & 11 deletions src/Operation/Falsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,33 @@ final class Falsy extends AbstractOperation
*/
public function __invoke(): Closure
{
return
$mapCallback =
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, bool> $iterator
* @param mixed $value
* @psalm-param T $value
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $value) {
if (false !== (bool) $value) {
return yield false;
}
}
static function ($value): bool {
return !(bool) $value;
};

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

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

// Point free style.
return $pipe;
}
}
38 changes: 27 additions & 11 deletions src/Operation/Nullsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function in_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand All @@ -20,19 +22,33 @@ final class Nullsy extends AbstractOperation
*/
public function __invoke(): Closure
{
return
$mapCallback =
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-return Generator<int, bool>
* @param mixed $value
* @psalm-param T $value
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $value) {
if (null !== $value) {
return yield false;
}
}

return yield true;
static function ($value): bool {
return in_array($value, [null, [], 0, false, ''], true);
};

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

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

// Point free style.
return $pipe;
}
}
35 changes: 24 additions & 11 deletions src/Operation/Truthy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,33 @@ final class Truthy extends AbstractOperation
*/
public function __invoke(): Closure
{
return
$mapCallback =
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, bool> $iterator
* @param mixed $value
* @psalm-param T $value
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $value) {
if (false === (bool) $value) {
return yield false;
}
}
static function ($value): bool {
return (bool) $value;
};

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

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

// Point free style.
return $pipe;
}
}

0 comments on commit 07fbce4

Please sign in to comment.