diff --git a/CHANGELOG.md b/CHANGELOG.md index 74ea770..0476e56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,3 +72,9 @@ ##6.1.0 - Filter can be called without arguments and it will remove falsy values + +##6.2.0 +- sizeIsExactly function added +- sizeIsGreaterThan function added +- sizeIsLessThan function added +- sizeIsBetween function added diff --git a/src/CollectionTrait.php b/src/CollectionTrait.php index 2ead745..af13619 100644 --- a/src/CollectionTrait.php +++ b/src/CollectionTrait.php @@ -758,6 +758,54 @@ public function intersect(...$collections) return intersect($this->getItems(), ...$collections); } + + + /** + * Checks whether this collection has exactly $size items. + * + * @param int $size + * @return bool + */ + public function sizeIs($size) + { + return sizeIs($this->getItems(), $size); + } + + /** + * Checks whether this collection has less than $size items. + * + * @param int $size + * @return bool + */ + public function sizeIsLessThan($size) + { + return sizeIsLessThan($this->getItems(), $size); + } + + /** + * Checks whether this collection has more than $size items. + * + * @param int $size + * @return bool + */ + public function sizeIsGreaterThan($size) + { + return sizeIsGreaterThan($this->getItems(), $size); + } + + /** + * Checks whether this collection has between $fromSize to $toSize items. $toSize can be + * smaller than $fromSize. + * + * @param int $fromSize + * @param int $toSize + * @return bool + */ + public function sizeIsBetween($fromSize, $toSize) + { + return sizeIsBetween($this->getItems(), $fromSize, $toSize); + } + /** * @return array|\Traversable */ diff --git a/src/collection_functions.php b/src/collection_functions.php index dda7665..b1796a2 100644 --- a/src/collection_functions.php +++ b/src/collection_functions.php @@ -1421,3 +1421,98 @@ function extract($collection, $keyPath) return new Collection($generatorFactory); } + +/** + * Checks whether $collection has exactly $size items. + * + * @param array|Traversable $collection + * @param int $size + * @return bool + */ +function sizeIs($collection, $size) +{ + $itemsTempCount = 0; + + foreach ($collection as $key => $value) { + $itemsTempCount++; + + if ($itemsTempCount > $size) { + return false; + } + } + + return $itemsTempCount == $size; +} + +/** + * Checks whether $collection has less than $size items. + * + * @param array|Traversable $collection + * @param int $size + * @return bool + */ +function sizeIsLessThan($collection, $size) +{ + $itemsTempCount = 0; + + foreach ($collection as $key => $value) { + $itemsTempCount++; + + if ($itemsTempCount > $size) { + return false; + } + } + + return $itemsTempCount < $size; +} + +/** + * Checks whether $collection has more than $size items. + * + * @param array|Traversable $collection + * @param int $size + * @return bool + */ +function sizeIsGreaterThan($collection, $size) +{ + $itemsTempCount = 0; + + foreach ($collection as $key => $value) { + $itemsTempCount++; + + if ($itemsTempCount > $size) { + return true; + } + } + + return $itemsTempCount > $size; +} + +/** + * Checks whether $collection has between $fromSize to $toSize items. $toSize can be + * smaller than $fromSize. + * + * @param array|Traversable $collection + * @param int $fromSize + * @param int $toSize + * @return bool + */ +function sizeIsBetween($collection, $fromSize, $toSize) +{ + if ($fromSize > $toSize) { + $tmp = $toSize; + $toSize = $fromSize; + $fromSize = $tmp; + } + + $itemsTempCount = 0; + foreach ($collection as $key => $value) { + $itemsTempCount++; + + if ($itemsTempCount > $toSize) { + return false; + } + } + + return $fromSize < $itemsTempCount && $itemsTempCount < $toSize; +} diff --git a/tests/spec/CollectionSpec.php b/tests/spec/CollectionSpec.php index 12c081a..a563d29 100644 --- a/tests/spec/CollectionSpec.php +++ b/tests/spec/CollectionSpec.php @@ -1107,7 +1107,7 @@ function it_can_use_the_utility_methods() ->values() ->toArray() ->shouldReturn([1, 2, 3]); - + $this ->map('\DusanKasan\Knapsack\compare') ->toArray() @@ -1139,5 +1139,38 @@ function it_can_use_the_utility_methods() ->reduce('\DusanKasan\Knapsack\concatenate', '') ->shouldReturn('103122'); } + + function it_can_check_if_size_is_exactly_n() + { + $this->beConstructedWith([1, 2]); + $this->sizeIs(2)->shouldReturn(true); + $this->sizeIs(3)->shouldReturn(false); + $this->sizeIs(0)->shouldReturn(false); + } + + function it_can_check_if_size_is_less_than_n() + { + $this->beConstructedWith([1, 2]); + $this->sizeIsLessThan(0)->shouldReturn(false); + $this->sizeIsLessThan(2)->shouldReturn(false); + $this->sizeIsLessThan(3)->shouldReturn(true); + } + + function it_can_check_if_size_is_greater_than_n() + { + $this->beConstructedWith([1, 2]); + $this->sizeIsGreaterThan(2)->shouldReturn(false); + $this->sizeIsGreaterThan(1)->shouldReturn(true); + $this->sizeIsGreaterThan(0)->shouldReturn(true); + } + + function it_can_check_if_size_is_between_n_and_m() + { + $this->beConstructedWith([1, 2]); + $this->sizeIsBetween(1, 3)->shouldReturn(true); + $this->sizeIsBetween(3, 4)->shouldReturn(false); + $this->sizeIsBetween(0, 0)->shouldReturn(false); + $this->sizeIsBetween(3, 1)->shouldReturn(true); + } }