Skip to content

Commit 64116ba

Browse files
muXxeraldas
authored andcommitted
Remove prefixType to avoid unnecessary breaking change
1 parent f6ddce3 commit 64116ba

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

bytes/bytes.go

+5-25
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ import (
1010
type (
1111
// Bytes struct
1212
Bytes struct{}
13-
14-
// PrefixType is the type of the unit prefix (binary/decimal)
15-
PrefixType byte
16-
)
17-
18-
const (
19-
// IEC 60027
20-
PrefixTypeBinary PrefixType = iota
21-
// SI international system of units
22-
PrefixTypeDecimal
2313
)
2414

2515
// binary units (IEC 60027)
@@ -54,19 +44,9 @@ func New() *Bytes {
5444
return &Bytes{}
5545
}
5646

57-
// Format formats bytes integer to human readable string according to the given prefix type.
58-
// If prefixType is not passed, binary prefix is used.
59-
func (b *Bytes) Format(value int64, prefixType ...PrefixType) string {
60-
61-
if len(prefixType) > 0 {
62-
switch prefixType[0] {
63-
case PrefixTypeBinary:
64-
return b.FormatBinary(value)
65-
case PrefixTypeDecimal:
66-
return b.FormatDecimal(value)
67-
}
68-
}
69-
47+
// Format formats bytes integer to human readable string according to IEC 60027.
48+
// For example, 31323 bytes will return 30.59KB.
49+
func (b *Bytes) Format(value int64) string {
7050
return b.FormatBinary(value)
7151
}
7252

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

218198
// Format wraps global Bytes's Format function.
219-
func Format(value int64, prefixType ...PrefixType) string {
220-
return global.Format(value, prefixType...)
199+
func Format(value int64) string {
200+
return global.Format(value)
221201
}
222202

223203
// FormatBinary wraps global Bytes's FormatBinary function.

bytes/bytes_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,27 @@ func TestBytesFormat(t *testing.T) {
4040
assert.Equal(t, "8.00EiB", b)
4141

4242
// KB
43-
b = Format(31323, PrefixTypeDecimal)
43+
b = FormatDecimal(31323)
4444
assert.Equal(t, "31.32KB", b)
4545

4646
// MB
47-
b = Format(13231323, PrefixTypeDecimal)
47+
b = FormatDecimal(13231323)
4848
assert.Equal(t, "13.23MB", b)
4949

5050
// GB
51-
b = Format(7323232398, PrefixTypeDecimal)
51+
b = FormatDecimal(7323232398)
5252
assert.Equal(t, "7.32GB", b)
5353

5454
// TB
55-
b = Format(7323232398434, PrefixTypeDecimal)
55+
b = FormatDecimal(7323232398434)
5656
assert.Equal(t, "7.32TB", b)
5757

5858
// PB
59-
b = Format(9923232398434432, PrefixTypeDecimal)
59+
b = FormatDecimal(9923232398434432)
6060
assert.Equal(t, "9.92PB", b)
6161

6262
// EB
63-
b = Format(math.MaxInt64, PrefixTypeDecimal)
63+
b = FormatDecimal(math.MaxInt64)
6464
assert.Equal(t, "9.22EB", b)
6565
}
6666

@@ -96,12 +96,12 @@ func TestFloats(t *testing.T) {
9696
assert.NoError(t, err)
9797
assert.Equal(t, int64(12250), valueDec)
9898

99-
strDec2 := Format(valueDec, PrefixTypeDecimal)
99+
strDec2 := FormatDecimal(valueDec)
100100
assert.Equal(t, strDec, strDec2)
101101

102102
// To string decimal:
103103
valDec := int64(13230000)
104-
strDec = Format(valDec, PrefixTypeDecimal)
104+
strDec = FormatDecimal(valDec)
105105
assert.Equal(t, "13.23MB", strDec)
106106

107107
valDec2, err := Parse(strDec)

0 commit comments

Comments
 (0)