Skip to content

Commit

Permalink
add @log directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashot Gharakeshishyan committed Aug 30, 2024
1 parent 5798a8e commit 7261aaf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,42 @@ public static function clamp(int|float $number, int|float $min, int|float $max)
return min(max($number, $min), $max);
}

/**
* Split the given number into pairs of min/max values.
*
* @param int|float $to
* @param int|float $by
* @param int|float $offset
* @return array
*/
public static function pairs(int|float $to, int|float $by, int|float $offset = 1)
{
$output = [];

for ($lower = 0; $lower < $to; $lower += $by) {
$upper = $lower + $by;

if ($upper > $to) {
$upper = $to;
}

$output[] = [$lower + $offset, $upper];
}

return $output;
}

/**
* Remove any trailing zero digits after the decimal point of the given number.
*
* @param int|float $number
* @return int|float
*/
public static function trim(int|float $number)
{
return json_decode(json_encode($number));
}

/**
* Execute the given callback using the given locale.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,22 @@ public function testSummarize()
$this->assertSame('-1Q', Number::abbreviate(-1000000000000000));
$this->assertSame('-1KQ', Number::abbreviate(-1000000000000000000));
}

public function testPairs()
{
$this->assertSame([[1, 10], [11, 20], [21, 25]], Number::pairs(25, 10));
$this->assertSame([[0, 10], [10, 20], [20, 25]], Number::pairs(25, 10, 0));
$this->assertSame([[0, 2.5], [2.5, 5.0], [5.0, 7.5], [7.5, 10.0]], Number::pairs(10, 2.5, 0));
}

public function testTrim()
{
$this->assertSame(12, Number::trim(12));
$this->assertSame(120, Number::trim(120));
$this->assertSame(12, Number::trim(12.0));
$this->assertSame(12.3, Number::trim(12.3));
$this->assertSame(12.3, Number::trim(12.30));
$this->assertSame(12.3456789, Number::trim(12.3456789));
$this->assertSame(12.3456789, Number::trim(12.34567890000));
}
}

0 comments on commit 7261aaf

Please sign in to comment.