From aca70d2394f0d9be6090c9b49ae4468d2e354ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Thu, 7 Dec 2023 09:42:40 +0800 Subject: [PATCH] feat(bytes): Added size (#34) --- bytes/size.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++ bytes/size_test.go | 66 +++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 bytes/size.go create mode 100644 bytes/size_test.go diff --git a/bytes/size.go b/bytes/size.go new file mode 100644 index 00000000..d1f0fe16 --- /dev/null +++ b/bytes/size.go @@ -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() +} diff --git a/bytes/size_test.go b/bytes/size_test.go new file mode 100644 index 00000000..c610d218 --- /dev/null +++ b/bytes/size_test.go @@ -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)) +}