-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package bytes | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
type Bytes int64 | ||
|
||
const ( | ||
KB Bytes = 1 << (10 * (iota + 1)) | ||
MB | ||
GB | ||
TB | ||
PB | ||
EB | ||
) | ||
|
||
func (b Bytes) Unit() string { | ||
switch { | ||
case b >= EB: | ||
return "EB" | ||
case b >= PB: | ||
return "PB" | ||
case b >= TB: | ||
return "TB" | ||
case b >= GB: | ||
return "GB" | ||
case b >= MB: | ||
return "MB" | ||
case b >= KB: | ||
return "KB" | ||
default: | ||
return "B" | ||
} | ||
} | ||
|
||
func (b Bytes) Format() float64 { | ||
switch { | ||
case b >= EB: | ||
return b.EB() | ||
case b >= PB: | ||
return b.PB() | ||
case b >= TB: | ||
return b.TB() | ||
case b >= GB: | ||
return b.GB() | ||
case b >= MB: | ||
return b.MB() | ||
case b >= KB: | ||
return b.KB() | ||
default: | ||
return b.Bytes() | ||
} | ||
} | ||
|
||
func (b Bytes) Bytes() float64 { | ||
return float64(b) | ||
} | ||
|
||
func (b Bytes) KB() float64 { | ||
return float64(b) / float64(KB) | ||
} | ||
|
||
func (b Bytes) MB() float64 { | ||
return float64(b) / float64(MB) | ||
} | ||
|
||
func (b Bytes) GB() float64 { | ||
return float64(b) / float64(GB) | ||
} | ||
|
||
func (b Bytes) TB() float64 { | ||
return float64(b) / float64(TB) | ||
} | ||
|
||
func (b Bytes) PB() float64 { | ||
return float64(b) / float64(PB) | ||
} | ||
|
||
func (b Bytes) EB() float64 { | ||
return float64(b) / float64(EB) | ||
} | ||
|
||
func (b Bytes) HumanizeValue(lens ...int) string { | ||
var l = 2 | ||
if len(lens) > 0 { | ||
l = lens[0] | ||
} | ||
|
||
return fmt.Sprintf("%."+strconv.Itoa(l)+"f", b.Format()) | ||
} | ||
|
||
func (b Bytes) Humanize(lens ...int) string { | ||
return fmt.Sprintf("%s", b.HumanizeValue(lens...)) + b.Unit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package bytes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBytes_Humanize(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
b Bytes | ||
want string | ||
}{ | ||
{ | ||
name: "test B", | ||
b: Bytes(1000), | ||
want: "1000.00B", | ||
}, | ||
{ | ||
name: "test KB", | ||
b: Bytes(1024), | ||
want: "1.00KB", | ||
}, | ||
{ | ||
name: "test MB", | ||
b: Bytes(1024 * 1024), | ||
want: "1.00MB", | ||
}, | ||
{ | ||
name: "test GB", | ||
b: Bytes(1024 * 1024 * 1024), | ||
want: "1.00GB", | ||
}, | ||
{ | ||
name: "test TB", | ||
b: Bytes(1024 * 1024 * 1024 * 1024), | ||
want: "1.00TB", | ||
}, | ||
{ | ||
name: "test PB", | ||
b: Bytes(1024 * 1024 * 1024 * 1024 * 1024), | ||
want: "1.00PB", | ||
}, | ||
{ | ||
name: "test EB", | ||
b: Bytes(1024 * 1024 * 1024 * 1024 * 1024 * 1024), | ||
want: "1.00EB", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.want, tt.b.Humanize()) | ||
assert.Equal(t, tt.want, tt.b.Humanize(2)) | ||
}) | ||
} | ||
} | ||
|
||
func TestBytes_HumanizeLen(t *testing.T) { | ||
assert.Equal(t, "1.00KB", Bytes(1024).Humanize()) | ||
assert.Equal(t, "1KB", Bytes(1024).Humanize(0)) | ||
assert.Equal(t, "1.0KB", Bytes(1024).Humanize(1)) | ||
assert.Equal(t, "1.00KB", Bytes(1024).Humanize(2)) | ||
assert.Equal(t, "1.000KB", Bytes(1024).Humanize(3)) | ||
} |