Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👔 up(fmt,math): move commonly util func to pkg basefn
- func DataSize, HowLongAgo
- Loading branch information
Showing
6 changed files
with
140 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package basefn | ||
|
||
import "fmt" | ||
|
||
// DataSize format bytes number friendly. | ||
// | ||
// Usage: | ||
// | ||
// file, err := os.Open(path) | ||
// fl, err := file.Stat() | ||
// fmtSize := DataSize(fl.Size()) | ||
func DataSize(size uint64) string { | ||
switch { | ||
case size < 1024: | ||
return fmt.Sprintf("%dB", size) | ||
case size < 1024*1024: | ||
return fmt.Sprintf("%.2fK", float64(size)/1024) | ||
case size < 1024*1024*1024: | ||
return fmt.Sprintf("%.2fM", float64(size)/1024/1024) | ||
default: | ||
return fmt.Sprintf("%.2fG", float64(size)/1024/1024/1024) | ||
} | ||
} | ||
|
||
var timeFormats = [][]int{ | ||
{0}, | ||
{1}, | ||
{2, 1}, | ||
{60}, | ||
{120, 60}, | ||
{3600}, | ||
{7200, 3600}, | ||
{86400}, | ||
{172800, 86400}, | ||
} | ||
|
||
var timeMessages = []string{ | ||
"< 1 sec", "1 sec", "secs", "1 min", "mins", "1 hr", "hrs", "1 day", "days", | ||
} | ||
|
||
// HowLongAgo format a seconds, get how lang ago | ||
func HowLongAgo(sec int64) string { | ||
intVal := int(sec) | ||
length := len(timeFormats) | ||
|
||
for i, item := range timeFormats { | ||
if intVal >= item[0] { | ||
ni := i + 1 | ||
match := false | ||
|
||
if ni < length { // next exists | ||
next := timeFormats[ni] | ||
if intVal < next[0] { // current <= intVal < next | ||
match = true | ||
} | ||
} else if ni == length { // current is last | ||
match = true | ||
} | ||
|
||
if match { // match success | ||
if len(item) == 1 { | ||
return timeMessages[i] | ||
} | ||
|
||
// len is 2 | ||
return fmt.Sprintf("%d %s", intVal/item[1], timeMessages[i]) | ||
} | ||
} | ||
} | ||
|
||
return "unknown" // He should never happen | ||
} |
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,43 @@ | ||
package basefn_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/basefn" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestDataSize(t *testing.T) { | ||
tests := []struct { | ||
args uint64 | ||
want string | ||
}{ | ||
{346, "346B"}, | ||
{3467, "3.39K"}, | ||
{346778, "338.65K"}, | ||
{12346778, "11.77M"}, | ||
{1200346778, "1.12G"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
assert.Eq(t, tt.want, basefn.DataSize(tt.args)) | ||
} | ||
} | ||
|
||
func TestHowLongAgo(t *testing.T) { | ||
tests := []struct { | ||
args int64 | ||
want string | ||
}{ | ||
{-36, "unknown"}, | ||
{36, "36 secs"}, | ||
{346, "5 mins"}, | ||
{3467, "57 mins"}, | ||
{346778, "4 days"}, | ||
{1200346778, "13892 days"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
assert.Eq(t, tt.want, basefn.HowLongAgo(tt.args)) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,17 @@ | ||
// Package fmtutil provide some format util functions. | ||
package fmtutil | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/gookit/goutil/strutil" | ||
) | ||
|
||
// StringOrJSON encode pretty JSON data to json bytes. | ||
func StringOrJSON(v any) ([]byte, error) { | ||
s, err := strutil.StringOrErr(v) | ||
if err != nil { | ||
return json.MarshalIndent(v, "", " ") | ||
} | ||
return []byte(s), nil | ||
} |
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 |
---|---|---|
@@ -1,52 +1,10 @@ | ||
package fmtutil | ||
|
||
import "fmt" | ||
|
||
var timeFormats = [][]int{ | ||
{0}, | ||
{1}, | ||
{2, 1}, | ||
{60}, | ||
{120, 60}, | ||
{3600}, | ||
{7200, 3600}, | ||
{86400}, | ||
{172800, 86400}, | ||
} | ||
|
||
var timeMessages = []string{ | ||
"< 1 sec", "1 sec", "secs", "1 min", "mins", "1 hr", "hrs", "1 day", "days", | ||
} | ||
import ( | ||
"github.com/gookit/goutil/basefn" | ||
) | ||
|
||
// HowLongAgo format a seconds, get how lang ago | ||
func HowLongAgo(sec int64) string { | ||
intVal := int(sec) | ||
length := len(timeFormats) | ||
|
||
for i, item := range timeFormats { | ||
if intVal >= item[0] { | ||
ni := i + 1 | ||
match := false | ||
|
||
if ni < length { // next exists | ||
next := timeFormats[ni] | ||
if intVal < next[0] { // current <= intVal < next | ||
match = true | ||
} | ||
} else if ni == length { // current is last | ||
match = true | ||
} | ||
|
||
if match { // match success | ||
if len(item) == 1 { | ||
return timeMessages[i] | ||
} | ||
|
||
// len is 2 | ||
return fmt.Sprintf("%d %s", intVal/item[1], timeMessages[i]) | ||
} | ||
} | ||
} | ||
|
||
return "unknown" // He should never happen | ||
return basefn.HowLongAgo(sec) | ||
} |
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