Skip to content

Commit

Permalink
Remove prefixType to avoid unnecessary breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer authored and aldas committed Apr 10, 2022
1 parent f6ddce3 commit 64116ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
30 changes: 5 additions & 25 deletions bytes/bytes.go
Expand Up @@ -10,16 +10,6 @@ import (
type (
// Bytes struct
Bytes struct{}

// PrefixType is the type of the unit prefix (binary/decimal)
PrefixType byte
)

const (
// IEC 60027
PrefixTypeBinary PrefixType = iota
// SI international system of units
PrefixTypeDecimal
)

// binary units (IEC 60027)
Expand Down Expand Up @@ -54,19 +44,9 @@ func New() *Bytes {
return &Bytes{}
}

// Format formats bytes integer to human readable string according to the given prefix type.
// If prefixType is not passed, binary prefix is used.
func (b *Bytes) Format(value int64, prefixType ...PrefixType) string {

if len(prefixType) > 0 {
switch prefixType[0] {
case PrefixTypeBinary:
return b.FormatBinary(value)
case PrefixTypeDecimal:
return b.FormatDecimal(value)
}
}

// Format formats bytes integer to human readable string according to IEC 60027.
// For example, 31323 bytes will return 30.59KB.
func (b *Bytes) Format(value int64) string {
return b.FormatBinary(value)
}

Expand Down Expand Up @@ -216,8 +196,8 @@ func (*Bytes) ParseDecimal(value string) (i int64, err error) {
}

// Format wraps global Bytes's Format function.
func Format(value int64, prefixType ...PrefixType) string {
return global.Format(value, prefixType...)
func Format(value int64) string {
return global.Format(value)
}

// FormatBinary wraps global Bytes's FormatBinary function.
Expand Down
16 changes: 8 additions & 8 deletions bytes/bytes_test.go
Expand Up @@ -40,27 +40,27 @@ func TestBytesFormat(t *testing.T) {
assert.Equal(t, "8.00EiB", b)

// KB
b = Format(31323, PrefixTypeDecimal)
b = FormatDecimal(31323)
assert.Equal(t, "31.32KB", b)

// MB
b = Format(13231323, PrefixTypeDecimal)
b = FormatDecimal(13231323)
assert.Equal(t, "13.23MB", b)

// GB
b = Format(7323232398, PrefixTypeDecimal)
b = FormatDecimal(7323232398)
assert.Equal(t, "7.32GB", b)

// TB
b = Format(7323232398434, PrefixTypeDecimal)
b = FormatDecimal(7323232398434)
assert.Equal(t, "7.32TB", b)

// PB
b = Format(9923232398434432, PrefixTypeDecimal)
b = FormatDecimal(9923232398434432)
assert.Equal(t, "9.92PB", b)

// EB
b = Format(math.MaxInt64, PrefixTypeDecimal)
b = FormatDecimal(math.MaxInt64)
assert.Equal(t, "9.22EB", b)
}

Expand Down Expand Up @@ -96,12 +96,12 @@ func TestFloats(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, int64(12250), valueDec)

strDec2 := Format(valueDec, PrefixTypeDecimal)
strDec2 := FormatDecimal(valueDec)
assert.Equal(t, strDec, strDec2)

// To string decimal:
valDec := int64(13230000)
strDec = Format(valDec, PrefixTypeDecimal)
strDec = FormatDecimal(valDec)
assert.Equal(t, "13.23MB", strDec)

valDec2, err := Parse(strDec)
Expand Down

0 comments on commit 64116ba

Please sign in to comment.