Skip to content

Commit

Permalink
Bugfix for negative file sizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed May 13, 2022
1 parent a906b53 commit eddb1a3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-mixins` will be documented in this file.

## 3.3.1 - 2022-05-13

- Bugfix for negative file sizes.

## 3.3.0 - 2022-02-22

- Added `SecondsToTime` string macro.
Expand Down
6 changes: 5 additions & 1 deletion src/String/HumanFilesize.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public function humanFilesize(): callable
* Formats the $value into a human readable filesize.
*/
return function ($value, $precision = 1): string {
$isNegative = $value < 0;

$value = abs($value);

if ($value >= 1000000000000) {
$value = round($value / (1024 * 1024 * 1024 * 1024), $precision);
$unit = 'TB';
Expand All @@ -32,7 +36,7 @@ public function humanFilesize(): callable
return number_format($value) . ' ' . $unit;
}

return number_format($value, $precision) . ' ' . $unit;
return ($isNegative ? '-' : '') . number_format($value, $precision) . ' ' . $unit;
};
}
}
1 change: 1 addition & 0 deletions tests/String/HumanFilesizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function it_can_format_file_size()
$this->assertEquals('44.6 KB', Str::humanFilesize(45678));
$this->assertEquals('446.1 KB', Str::humanFilesize(456789));
$this->assertEquals('3.3 MB', Str::humanFilesize(3456789));
$this->assertEquals('-3.3 MB', Str::humanFilesize(-3456789));
$this->assertEquals('1.8 GB', Str::humanFilesize(1932735283.2));
$this->assertEquals('112,283.3 TB', Str::humanFilesize(123456789123456789));
}
Expand Down

0 comments on commit eddb1a3

Please sign in to comment.