Skip to content

Commit

Permalink
feat: Add Unlines operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 21, 2020
1 parent 24cd4a5 commit ab9fde2
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,27 @@ truthy

Interface: `Truthyable`_

unlines
~~~~~~~

Create a string from lines.

Interface: `Unlinesable`_

Signature: ``Collection::unlines();``

.. code-block:: php
$lines = [
'The quick brow fox jumps over the lazy dog.',
'',
'This is another sentence.',
];
Collection::fromIterable($lines)
->unlines()
->current();
unpack
~~~~~~

Expand Down Expand Up @@ -1895,6 +1916,7 @@ Signature: ``Collection::zip(iterable ...$iterables);``
.. _TakeWhileable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/TakeWhileable.php
.. _Transposeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Transposeable.php
.. _Truthyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Truthyable.php
.. _Unlinesable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unlinesable.php
.. _Unpackable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unpackagle.php
.. _Unpairable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unpairable.php
.. _Untilable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Untilable.php
Expand Down
21 changes: 21 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,27 @@ public function it_can_unfold(): void
]);
}

public function it_can_unlines(): void
{
$lines = [
'The quick brow fox jumps over the lazy dog.',
'',
'This is another sentence.',
];

$string = <<<'EOF'
The quick brow fox jumps over the lazy dog.
This is another sentence.
EOF;

$this::fromIterable($lines)
->unlines()
->shouldIterateAs([
2 => $string,
]);
}

public function it_can_unpack(): void
{
$input = [
Expand Down
6 changes: 6 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
use loophp\collection\Operation\Transpose;
use loophp\collection\Operation\Truthy;
use loophp\collection\Operation\Unfold;
use loophp\collection\Operation\Unlines;
use loophp\collection\Operation\Unpack;
use loophp\collection\Operation\Unpair;
use loophp\collection\Operation\Until;
Expand Down Expand Up @@ -761,6 +762,11 @@ public static function unfold(callable $callback, ...$parameters): CollectionInt
return (new self())->run(Unfold::of()(...$parameters)($callback));
}

public function unlines(): CollectionInterface
{
return $this->run(Unlines::of());
}

public function unpack(): CollectionInterface
{
return $this->run(Unpack::of());
Expand Down
3 changes: 3 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
use loophp\collection\Contract\Operation\Transposeable;
use loophp\collection\Contract\Operation\Truthyable;
use loophp\collection\Contract\Operation\Unfoldable;
use loophp\collection\Contract\Operation\Unlinesable;
use loophp\collection\Contract\Operation\Unpackable;
use loophp\collection\Contract\Operation\Unpairable;
use loophp\collection\Contract\Operation\Untilable;
Expand Down Expand Up @@ -176,6 +177,7 @@
* @template-extends Tailable<TKey, T>
* @template-extends TakeWhileable<TKey, T>
* @template-extends Transposeable<TKey, T>
* @template-extends Unlinesable<TKey, T>
* @template-extends Unpackable<TKey, T>
* @template-extends Unpairable<TKey, T>
* @template-extends Untilable<TKey, T>
Expand Down Expand Up @@ -271,6 +273,7 @@ interface Collection extends
Transposeable,
Truthyable,
Unfoldable,
Unlinesable,
Unpackable,
Unpairable,
Untilable,
Expand Down
20 changes: 20 additions & 0 deletions src/Contract/Operation/Unlinesable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Unlinesable
{
/**
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function unlines(): Collection;
}
37 changes: 37 additions & 0 deletions src/Operation/Unlines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Operation;

use Closure;
use Generator;
use Iterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
*/
final class Unlines extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T|string>): Generator<TKey, T|string, mixed, void>
*/
public function __invoke(): Closure
{
/**
* @psalm-param Iterator<TKey, T|string> $iterator
*
* @psalm-return Generator<TKey, string>
*/
return static function (Iterator $iterator): Generator {
/** @psalm-var callable(Iterator<TKey, T|string>): Generator<TKey, string> $implode */
$implode = Implode::of()("\n");

return $implode($iterator);
};
}
}

0 comments on commit ab9fde2

Please sign in to comment.