Skip to content

Commit

Permalink
feat: Add Unwords operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 21, 2020
1 parent ab9fde2 commit b6aef32
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,35 @@ Signature: ``Collection::unwindow();``
->drop(1)
->normalize(); // [10, 11, 12, 13, 14, 15, 16, 17, 18]
unwords
~~~~~~~

Create a string from words.

Interface: `Unwordsable`_

Signature: ``Collection::unwords();``

.. code-block:: php
$words = [
'The',
'quick',
'brow',
'fox',
'jumps',
'over',
'the',
'lazy',
"dog.\n\nThis",
'is',
'another',
'sentence.',
];
Collection::fromIterable($words)
->unwords();
unwrap
~~~~~~

Expand Down Expand Up @@ -1921,6 +1950,7 @@ Signature: ``Collection::zip(iterable ...$iterables);``
.. _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
.. _Unwindowable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unwindowable.php
.. _Unwordsable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unwordsable.php
.. _Unwrapable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unwrapable.php
.. _Unzipable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unzipable.php
.. _Windowable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Windowable.php
Expand Down
28 changes: 28 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,34 @@ public function it_can_unwindow(): void
->shouldIterateAs(range('a', 'z'));
}

public function it_can_unwords(): void
{
$string = <<<'EOF'
The quick brow fox jumps over the lazy dog.
This is another sentence.
EOF;

$words = [
'The',
'quick',
'brow',
'fox',
'jumps',
'over',
'the',
'lazy',
"dog.\n\nThis",
'is',
'another',
'sentence.',
];

$this::fromIterable($words)
->unwords()
->shouldIterateAs([11 => $string]);
}

public function it_can_unwrap()
{
$this::fromIterable([['a' => 'A'], ['b' => 'B'], ['c' => 'C']])
Expand Down
6 changes: 6 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
use loophp\collection\Operation\Unpair;
use loophp\collection\Operation\Until;
use loophp\collection\Operation\Unwindow;
use loophp\collection\Operation\Unwords;
use loophp\collection\Operation\Unwrap;
use loophp\collection\Operation\Unzip;
use loophp\collection\Operation\Window;
Expand Down Expand Up @@ -787,6 +788,11 @@ public function unwindow(): CollectionInterface
return $this->run(Unwindow::of());
}

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

public function unwrap(): CollectionInterface
{
return $this->run(Unwrap::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 @@ -92,6 +92,7 @@
use loophp\collection\Contract\Operation\Unpairable;
use loophp\collection\Contract\Operation\Untilable;
use loophp\collection\Contract\Operation\Unwindowable;
use loophp\collection\Contract\Operation\Unwordsable;
use loophp\collection\Contract\Operation\Unwrapable;
use loophp\collection\Contract\Operation\Unzipable;
use loophp\collection\Contract\Operation\Windowable;
Expand Down Expand Up @@ -182,6 +183,7 @@
* @template-extends Unpairable<TKey, T>
* @template-extends Untilable<TKey, T>
* @template-extends Unwindowable<TKey, T>
* @template-extends Unwordsable<TKey, T>
* @template-extends Unwrapable<TKey, T>
* @template-extends Unzipable<TKey, T>
* @template-extends Windowable<TKey, T>
Expand Down Expand Up @@ -278,6 +280,7 @@ interface Collection extends
Unpairable,
Untilable,
Unwindowable,
Unwordsable,
Unwrapable,
Unzipable,
Windowable,
Expand Down
20 changes: 20 additions & 0 deletions src/Contract/Operation/Unwordsable.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 Unwordsable
{
/**
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function unwords(): Collection;
}
37 changes: 37 additions & 0 deletions src/Operation/Unwords.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 Unwords 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()(' ');

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

0 comments on commit b6aef32

Please sign in to comment.