Skip to content

Commit eddb1a3

Browse files
committed
Bugfix for negative file sizes.
1 parent a906b53 commit eddb1a3

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
## 3.3.1 - 2022-05-13
6+
7+
- Bugfix for negative file sizes.
8+
59
## 3.3.0 - 2022-02-22
610

711
- Added `SecondsToTime` string macro.

src/String/HumanFilesize.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public function humanFilesize(): callable
1515
* Formats the $value into a human readable filesize.
1616
*/
1717
return function ($value, $precision = 1): string {
18+
$isNegative = $value < 0;
19+
20+
$value = abs($value);
21+
1822
if ($value >= 1000000000000) {
1923
$value = round($value / (1024 * 1024 * 1024 * 1024), $precision);
2024
$unit = 'TB';
@@ -32,7 +36,7 @@ public function humanFilesize(): callable
3236
return number_format($value) . ' ' . $unit;
3337
}
3438

35-
return number_format($value, $precision) . ' ' . $unit;
39+
return ($isNegative ? '-' : '') . number_format($value, $precision) . ' ' . $unit;
3640
};
3741
}
3842
}

tests/String/HumanFilesizeTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function it_can_format_file_size()
2525
$this->assertEquals('44.6 KB', Str::humanFilesize(45678));
2626
$this->assertEquals('446.1 KB', Str::humanFilesize(456789));
2727
$this->assertEquals('3.3 MB', Str::humanFilesize(3456789));
28+
$this->assertEquals('-3.3 MB', Str::humanFilesize(-3456789));
2829
$this->assertEquals('1.8 GB', Str::humanFilesize(1932735283.2));
2930
$this->assertEquals('112,283.3 TB', Str::humanFilesize(123456789123456789));
3031
}

0 commit comments

Comments
 (0)