added some helper functions

This commit is contained in:
Suyono 2025-05-18 08:45:00 +10:00
parent ffe10b11a3
commit b7f6d544ec
2 changed files with 53 additions and 0 deletions

View File

@ -16,6 +16,15 @@ package sizes
limitations under the License. limitations under the License.
*/ */
import (
"errors"
"math"
)
var (
ErrUnfit = errors.New("cannot fit into integer")
)
func (b BiByteUnit) AsByte() ByteUnit { func (b BiByteUnit) AsByte() ByteUnit {
return ByteUnit(b) return ByteUnit(b)
} }
@ -28,6 +37,27 @@ func (b BiByteUnit) AsBiByte() BiByteUnit {
return b return b
} }
func (b BiByteUnit) Int() (int, error) {
if int64(b) > math.MaxInt || int64(b) < math.MinInt {
return 0, ErrUnfit
}
return int(b), nil
}
func (b BiByteUnit) MustInt() int {
var (
result int
err error
)
if result, err = b.Int(); err != nil {
panic(err)
}
return result
}
func (b BiByteUnit) Kibi() float64 { func (b BiByteUnit) Kibi() float64 {
return float64(b / KibiByte) return float64(b / KibiByte)
} }

View File

@ -16,6 +16,8 @@ package sizes
limitations under the License. limitations under the License.
*/ */
import "math"
func (b ByteUnit) AsByte() ByteUnit { func (b ByteUnit) AsByte() ByteUnit {
return b return b
} }
@ -28,6 +30,27 @@ func (b ByteUnit) AsBiByte() BiByteUnit {
return BiByteUnit(b) return BiByteUnit(b)
} }
func (b ByteUnit) Int() (int, error) {
if int64(b) > math.MaxInt || int64(b) < math.MinInt {
return 0, ErrUnfit
}
return int(b), nil
}
func (b ByteUnit) MustInt() int {
var (
result int
err error
)
if result, err = b.Int(); err != nil {
panic(err)
}
return result
}
func (b ByteUnit) Kilo() float64 { func (b ByteUnit) Kilo() float64 {
return float64(b / KiloByte) return float64(b / KiloByte)
} }