-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
871fde9
commit 4ef6628
Showing
9 changed files
with
212 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ type Object struct { | |
Links []string | ||
MetadataURL string | ||
Reader io.Reader | ||
Bytes int64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Pluralize(s string, n int64) string { | ||
if n == 1 { | ||
return s | ||
} | ||
return s + "s" | ||
} | ||
|
||
func FormatBytes(bytes int64) string { | ||
const ( | ||
kb = 1024 | ||
mb = 1024 * kb | ||
gb = 1024 * mb | ||
) | ||
switch { | ||
case bytes >= gb: | ||
// Note: Using this pattern instead of printf with %.1f to avoid rounding up which can lead | ||
// to weird results like 1024.0 MiB instead of 1.0 GiB. | ||
n, rem := bytes/gb, bytes%gb | ||
return fmt.Sprintf("%d.%d GiB", n, rem*10/gb) | ||
case bytes >= mb: | ||
n, rem := bytes/mb, bytes%mb | ||
return fmt.Sprintf("%d.%d MiB", n, rem*10/mb) | ||
case bytes >= kb: | ||
n, rem := bytes/kb, bytes%kb | ||
return fmt.Sprintf("%d.%d KiB", n, rem*10/kb) | ||
default: | ||
return fmt.Sprintf("%d %s", bytes, Pluralize("byte", bytes)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package utils_test | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/chriskuehl/fluffy/server/utils" | ||
) | ||
|
||
func TestPluralize(t *testing.T) { | ||
tests := map[int64]string{ | ||
0: "things", | ||
1: "thing", | ||
2: "things", | ||
} | ||
for count, want := range tests { | ||
t.Run(strconv.FormatInt(count, 10), func(t *testing.T) { | ||
if got := utils.Pluralize("thing", count); got != want { | ||
t.Errorf("got Pluralize(%q, %d) = %v, want %v", "thing", count, got, want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFormatBytes(t *testing.T) { | ||
tests := map[int64]string{ | ||
0: "0 bytes", | ||
1: "1 byte", | ||
1023: "1023 bytes", | ||
1024: "1.0 KiB", | ||
1024 * 1024: "1.0 MiB", | ||
1024*1024 - 1: "1023.9 KiB", | ||
1024*1024*1024 - 1: "1023.9 MiB", | ||
1024 * 1024 * 1024: "1.0 GiB", | ||
3*1024*1024*1024 + 717*1024*1024: "3.7 GiB", | ||
} | ||
for bytes, want := range tests { | ||
t.Run(strconv.FormatInt(bytes, 10), func(t *testing.T) { | ||
if got := utils.FormatBytes(bytes); got != want { | ||
t.Errorf("got FormatBytes(%d) = %v, want %v", bytes, got, want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters