Skip to content

Commit 8e93692

Browse files
committed
add readme
1 parent d068f88 commit 8e93692

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

README.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,99 @@
11
# bytesizer
2-
bytesizer
2+
3+
`bytesizer` is a Go package that provides a simple and intuitive way to work with byte counts. It allows you to easily convert between different units of byte sizes, such as bytes, kilobytes, megabytes, gigabytes, terabytes, and petabytes. Similar to how `time.Duration` works in Go, `bytesizer` helps in representing byte sizes with appropriate types and methods.
4+
5+
## Features
6+
7+
- Conversion between byte units and bytes
8+
- Formatting of byte sizes into human-readable strings
9+
- Parsing of strings into byte sizes with support for different units
10+
- Fetching byte sizes as integer or floating-point numbers for precision work
11+
12+
## Installation
13+
14+
To install `bytesizer`, you can use the following Go command:
15+
16+
```bash
17+
go get github.com/iamlongalong/bytesizer
18+
```
19+
20+
Replace `iamlongalong` with your actual GitHub username where the package is hosted.
21+
22+
## Usage
23+
24+
Below you'll find the methods provided by the `bytesizer` package and some examples of how to use them.
25+
26+
### Constants
27+
28+
`bytesizer` defines the following byte size units:
29+
30+
```go
31+
const (
32+
Byte ByteSize = 1 << (10 * iota)
33+
KB
34+
MB
35+
GB
36+
TB
37+
PB
38+
)
39+
```
40+
41+
### Methods
42+
43+
#### Calc
44+
Calculate the length of a byte slice and return it as a `ByteSize`:
45+
46+
```go
47+
size := bytesizer.Calc([]byte("Hello World!"))
48+
```
49+
50+
#### Format
51+
Format a `ByteSize` value to a string according to a specified unit:
52+
53+
```go
54+
formattedSize := size.Format(bytesizer.KB) // returns string like "1KB"
55+
```
56+
57+
#### String
58+
Convert a `ByteSize` to a string with an automatic unit:
59+
60+
```go
61+
sizeString := size.String() // returns string like "11B"
62+
```
63+
64+
#### Byte, KB, MB, GB, TB, PB
65+
Get the byte size as different units (returns `float64`):
66+
67+
```go
68+
bytes := size.Byte()
69+
kilobytes := size.KB()
70+
// ... and so on for MB, GB, TB, PB
71+
```
72+
73+
#### ByteInt, KBInt, MBInt, GBInt, TBInt, PBInt
74+
Get the byte size as different units (returns `int`):
75+
76+
```go
77+
bytesInt := size.ByteInt()
78+
kilobytesInt := size.KBInt()
79+
// ... and so on for MBInt, GBInt, TBInt, PBInt
80+
```
81+
82+
#### Parse
83+
Parse a string representation of a byte size into a `ByteSize` object:
84+
85+
```go
86+
size, err := bytesizer.Parse("10KB")
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
fmt.Println(size) // Output: 10240 (Bytes equivalent of 10KB)
91+
```
92+
93+
## Contributing
94+
95+
Contributions to `bytesizer` are welcome! Feel free to report issues or submit pull requests on our GitHub repository.
96+
97+
## License
98+
99+
This package is licensed under the MIT License - see the LICENSE.md file for details.

bytesizer.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ var units = []struct {
2525
{Byte, "B"}, {KB, "KB"}, {MB, "MB"}, {GB, "GB"}, {TB, "TB"}, {PB, "PB"},
2626
}
2727

28+
// Calc calc the []byte length, trans to ByteSize.
29+
func Calc(b []byte) ByteSize {
30+
return ByteSize(len(b))
31+
}
32+
33+
// Format method formats the ByteSize value to a string based on the given byte unit.
34+
// It iterates over the predefined units until it matches the given unit,
35+
// then calls formatString to generate the final formatted string.
36+
// If the unit doesn't match any predefined units, it returns the string representation of the ByteSize itself.
2837
func (fs ByteSize) Format(bu ByteSize) string {
2938

3039
unitVal := float64(fs) / float64(bu)
@@ -38,6 +47,7 @@ func (fs ByteSize) Format(bu ByteSize) string {
3847
return fs.String()
3948
}
4049

50+
// String method converts ByteSize to a string with an appropriate unit.
4151
func (fs ByteSize) String() string {
4252
switch {
4353
case fs >= PB:
@@ -55,50 +65,62 @@ func (fs ByteSize) String() string {
5565
return formatString(fs.Byte(), "B", 2)
5666
}
5767

68+
// Byte method returns the ByteSize in bytes as a float64.
5869
func (fs ByteSize) Byte() float64 {
5970
return float64(fs)
6071
}
6172

73+
// KB method returns the ByteSize in kilobytes as a float64.
6274
func (fs ByteSize) KB() float64 {
6375
return float64(fs) / float64(KB)
6476
}
6577

78+
// MB method returns the ByteSize in megabytes as a float64.
6679
func (fs ByteSize) MB() float64 {
6780
return float64(fs) / float64(MB)
6881
}
6982

83+
// GB method returns the ByteSize in gigabytes as a float64.
7084
func (fs ByteSize) GB() float64 {
7185
return float64(fs) / float64(GB)
7286
}
7387

88+
// TB method returns the ByteSize in terabytes as a float64.
7489
func (fs ByteSize) TB() float64 {
7590
return float64(fs) / float64(TB)
7691
}
7792

93+
// PB method returns the ByteSize in petabytes as a float64.
7894
func (fs ByteSize) PB() float64 {
7995
return float64(fs) / float64(PB)
8096
}
8197

98+
// ByteInt method returns the ByteSize in bytes as an integer.
8299
func (fs ByteSize) ByteInt() int {
83100
return int(fs)
84101
}
85102

103+
// KBInt method returns the ByteSize in kilobytes as an integer.
86104
func (fs ByteSize) KBInt() int {
87105
return int(fs / KB)
88106
}
89107

108+
// MBInt method returns the ByteSize in megabytes as an integer.
90109
func (fs ByteSize) MBInt() int {
91110
return int(fs / MB)
92111
}
93112

113+
// GBInt method returns the ByteSize in gigabytes as an integer.
94114
func (fs ByteSize) GBInt() int {
95115
return int(fs / GB)
96116
}
97117

118+
// TBInt method returns the ByteSize in terabytes as an integer.
98119
func (fs ByteSize) TBInt() int {
99120
return int(fs / TB)
100121
}
101122

123+
// PBInt method returns the ByteSize in petabytes as an integer.
102124
func (fs ByteSize) PBInt() int {
103125
return int(fs / TB)
104126
}
@@ -176,6 +198,10 @@ func formatString(v float64, unit string, maxDecimalCount ...int) string {
176198
return fmt.Sprintf(formatString, n, unit)
177199
}
178200

201+
// decimalPlaces counts the decimal places in a float64.
202+
// E.g1: decimalPlaces(1.23) returns 2
203+
// E.g2: decimalPlaces(100.456) returns 3.
204+
// E.g3: decimalPlaces(10.100) returns 1.
179205
func decimalPlaces(f float64) (n int) {
180206
const epsilon = 1e-10
181207
for math.Abs(f-math.Floor(f)) > epsilon {

0 commit comments

Comments
 (0)