Skip to content

Commit

Permalink
feat(bytes): Added size (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Dec 7, 2023
1 parent ebe94ee commit aca70d2
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
96 changes: 96 additions & 0 deletions bytes/size.go
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()
}
66 changes: 66 additions & 0 deletions bytes/size_test.go
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))
}

0 comments on commit aca70d2

Please sign in to comment.