Skip to content

Commit

Permalink
feat: collection max and min (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana authored Jan 3, 2024
1 parent 83cd344 commit 1d1a30a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/System/Collection/AbstractCollectionImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,26 @@ public function avg(): int
return $this->sum() / $this->count();
}

/**
* Find higest value.
*
* @param string|int|null $key
*/
public function max($key = null): int
{
return max(array_column($this->collection, $key));
}

/**
* Find lowest value.
*
* @param string|int|null $key
*/
public function min($key = null): int
{
return min(array_column($this->collection, $key));
}

// array able

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/Collection/CollectionImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,36 @@ public function itCanGetLasts()

$this->assertEquals([80, 90], $coll->lasts(2));
}

/** @test */
public function itCanGetHigest()
{
$coll = new CollectionImmutable([10, 20, 30, 40, 50, 60, 70, 80, 90]);

$this->assertEquals(90, $coll->max());

$coll = new CollectionImmutable([
['rank' => 10],
['rank' => 50],
['rank' => 90],
]);

$this->assertEquals(90, $coll->max('rank'));
}

/** @test */
public function itCanGetLowestValue()
{
$coll = new CollectionImmutable([10, 20, 30, 40, 50, 60, 70, 80, 90]);

$this->assertEquals(10, $coll->min());

$coll = new CollectionImmutable([
['rank' => 10],
['rank' => 50],
['rank' => 90],
]);

$this->assertEquals(10, $coll->min('rank'));
}
}

0 comments on commit 1d1a30a

Please sign in to comment.