Skip to content

Commit

Permalink
Merge pull request #34 from worksolutions/dev
Browse files Browse the repository at this point in the history
Some enchasaments for next minor version
  • Loading branch information
antonLytkin18 authored Oct 11, 2021
2 parents 1afdc5e + e00263f commit 93f20a7
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Collections library for php language",
"minimum-stability": "dev",
"license": "MIT",
"version": "1.0.8",
"version": "1.0.9",
"authors": [
{
"name": "Maxim Sokolovsky",
Expand Down
19 changes: 19 additions & 0 deletions src/WS/Utils/Collections/DummyStreamDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,23 @@ public function always(): Stream
{
return $this->decoratedStream;
}

/**
* @inheritDoc
*/
public function toArray(): array
{
return $this
->getCollection()
->toArray()
;
}

/**
* @inheritDoc
*/
public function getSet(): Set
{
return new HashSet($this->toArray());
}
}
5 changes: 3 additions & 2 deletions src/WS/Utils/Collections/Functions/Predicates.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ public static function equal($value): Closure
/**
* Returns <Fn($el: mixed): bool> passed unique element at once
*/
public static function lockDuplicated(): Closure
public static function lockDuplicated(?callable $caster = null): Closure
{
$set = new HashSet();
return static function ($el) use ($set): bool {
return static function ($el) use ($set, $caster): bool {
$el = isset($caster) ? $caster($el) : $el;
$res = !$set->contains($el);
$set->add($el);
return $res;
Expand Down
21 changes: 15 additions & 6 deletions src/WS/Utils/Collections/Functions/Reorganizers.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,25 @@ public static function chunk(int $size): Closure

/**
* Returns Closure <Fn($c: Collection): Collection> that collapses a collection of arrays into a single, flat collection
* @param int $depth Depth of collapses. The 0 value is without
* @return Closure
*/
public static function collapse(): Closure
public static function collapse(int $depth = 0): Closure
{
return static function (Collection $collection): Collection {
$flatIterable = static function (iterable $collection) use (& $flatIterable): array {
if ($depth === 0) {
$depth = null;
}
return static function (Collection $collection) use ($depth): Collection {
$flatIterable = static function (iterable $collection, $depth) use (& $flatIterable): array {
$goToDepth = $depth > 0 || $depth === null;
if ($depth !== null) {
$depth--;
}

$res = [];
foreach ($collection as $item) {
if (is_iterable($item)) {
$toPush = $flatIterable($item);
if (is_iterable($item) && $goToDepth) {
$toPush = $flatIterable($item, $depth);
array_unshift($toPush, $res);
array_push(...$toPush);
$res = array_shift($toPush);
Expand All @@ -132,7 +141,7 @@ public static function collapse(): Closure
return $res;
};

return self::collectionConstructor($flatIterable($collection));
return self::collectionConstructor($flatIterable($collection, $depth));
};
}
}
19 changes: 19 additions & 0 deletions src/WS/Utils/Collections/SerialStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,23 @@ public function always(): Stream
{
return $this;
}

/**
* @inheritDoc
*/
public function toArray(): array
{
return $this
->getCollection()
->toArray()
;
}

/**
* @inheritDoc
*/
public function getSet(): Set
{
return new HashSet($this->toArray());
}
}
12 changes: 12 additions & 0 deletions src/WS/Utils/Collections/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,16 @@ public function getCollection(): Collection;
* @return Stream
*/
public function always(): Stream;

/**
* Returns array as collection represent
* @return array
*/
public function toArray(): array;

/**
* Creates a set structure
* @return Set
*/
public function getSet(): Set;
}
48 changes: 48 additions & 0 deletions tests/WS/Utils/Collections/ReorganizersFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ public function collapsing(): void
$this->assertThat($collapsedCollection, CollectionIsEqual::to([1, 2, 3, 4, 5, 6]));
}

/**
* @test
*/
public function singleDepthCollapsing(): void
{
$collection = self::toCollection([1, 2], [3, 4], [5, 6, [7, 8]]);

$collapsedCollection = $collection
->stream()
->reorganize(Reorganizers::collapse(1))
->getCollection()
;
$this->assertThat($collapsedCollection, CollectionIsEqual::to([1, 2, 3, 4, 5, 6, [7, 8]]));
}

/**
* @test
*/
public function numericDepthCollapsing(): void
{
$collection = self::toCollection([1, 2], [3, 4], [5, 6, [7, 8, [9, 10]]]);

$collapsedCollection = $collection
->stream()
->reorganize(Reorganizers::collapse(3))
->getCollection()
;
$this->assertThat($collapsedCollection, CollectionIsEqual::to([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
}

/**
* @test
*/
Expand All @@ -67,4 +97,22 @@ public function filterDistinctElements(): void

self::assertThat($uniqCollection, CollectionIsEqual::to([$o1, $o2, $o3]));
}

/**
* @test
*/
public function filterDistinctCastedValues(): void
{
$caster = static function (int $number) {
return $number % 2;
};

$result = CollectionFactory::numbers(0, 10)
->stream()
->filter(Predicates::lockDuplicated($caster))
->toArray()
;

$this->assertCount(2, $result);
}
}
14 changes: 14 additions & 0 deletions tests/WS/Utils/Collections/SerialStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,18 @@ public function copyOfCollectionWhenStreaming(): void

$this->assertNotSame($dest, $source);
}

/**
* @test
*/
public function gettingSetStructure(): void
{
$source = CollectionFactory::generate(3);
$this->assertThat($source, $this->isInstanceOf(ListSequence::class));
$set = $source
->stream()
->getSet()
;
$this->assertThat($set, $this->isInstanceOf(Set::class));
}
}

0 comments on commit 93f20a7

Please sign in to comment.