1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

Compare commits

...

4 Commits

Author SHA1 Message Date
dudaodong f467658481 Merge branch 'rc' into v2 2024-07-31 10:50:29 +08:00
zyfx 5c9d0e396e Update condition.go (#234) 2024-07-31 10:16:03 +08:00
dudaodong 8f74460c1b fix: remove scientific notation in DecimalBytes 2024-07-30 14:17:17 +08:00
dudaodong d5752499bf fix: remove scientific notation in DecimalBytes 2024-07-29 11:42:56 +08:00
3 changed files with 23 additions and 9 deletions
+2 -6
View File
@@ -49,9 +49,7 @@ func Or[T, U any](a T, b U) bool {
// Xor returns true if a or b but not both is truthy. // Xor returns true if a or b but not both is truthy.
// Play: https://go.dev/play/p/gObZrW7ZbG8 // Play: https://go.dev/play/p/gObZrW7ZbG8
func Xor[T, U any](a T, b U) bool { func Xor[T, U any](a T, b U) bool {
valA := Bool(a) return Bool(a) != Bool(b)
valB := Bool(b)
return (valA || valB) && valA != valB
} }
// Nor returns true if neither a nor b is truthy. // Nor returns true if neither a nor b is truthy.
@@ -63,9 +61,7 @@ func Nor[T, U any](a T, b U) bool {
// Xnor returns true if both a and b or neither a nor b are truthy. // Xnor returns true if both a and b or neither a nor b are truthy.
// Play: https://go.dev/play/p/OuDB9g51643 // Play: https://go.dev/play/p/OuDB9g51643
func Xnor[T, U any](a T, b U) bool { func Xnor[T, U any](a T, b U) bool {
valA := Bool(a) return Bool(a) == Bool(b)
valB := Bool(b)
return (valA && valB) || (!valA && !valB)
} }
// Nand returns false if both a and b are truthy. // Nand returns false if both a and b are truthy.
+16 -3
View File
@@ -84,13 +84,26 @@ var (
// The precision parameter specifies the number of digits after the decimal point, which defaults to 4. // The precision parameter specifies the number of digits after the decimal point, which defaults to 4.
// Play: https://go.dev/play/p/FPXs1suwRcs // Play: https://go.dev/play/p/FPXs1suwRcs
func DecimalBytes(size float64, precision ...int) string { func DecimalBytes(size float64, precision ...int) string {
p := 5 pointPosition := 4
if len(precision) > 0 { if len(precision) > 0 {
p = precision[0] + 1 pointPosition = precision[0]
} }
size, unit := calculateByteSize(size, 1000.0, decimalByteUnits) size, unit := calculateByteSize(size, 1000.0, decimalByteUnits)
return fmt.Sprintf("%.*g%s", p, size, unit) format := fmt.Sprintf("%%.%df", pointPosition)
bytes := fmt.Sprintf(format, size)
for i := len(bytes); i > 0; i-- {
s := bytes[i-1]
if s != '0' && s != '.' {
break
}
bytes = bytes[:i-1]
}
return bytes + unit
} }
// BinaryBytes returns a human-readable byte size under binary standard (base 1024) // BinaryBytes returns a human-readable byte size under binary standard (base 1024)
+5
View File
@@ -20,6 +20,11 @@ func TestDecimalBytes(t *testing.T) {
assert.Equal("3.123PB", DecimalBytes(float64(3.123*UnitPB))) assert.Equal("3.123PB", DecimalBytes(float64(3.123*UnitPB)))
assert.Equal("4.123EB", DecimalBytes(float64(4.123*UnitEB))) assert.Equal("4.123EB", DecimalBytes(float64(4.123*UnitEB)))
assert.Equal("1EB", DecimalBytes(float64(1000*UnitPB))) assert.Equal("1EB", DecimalBytes(float64(1000*UnitPB)))
assert.Equal("62MB", DecimalBytes(61812496, 0))
assert.Equal("61.8MB", DecimalBytes(61812496, 1))
assert.Equal("401MB", DecimalBytes(401000000))
assert.Equal("401MB", DecimalBytes(401000000, 0))
assert.Equal("4MB", DecimalBytes(4010000, 0))
} }
func TestBinaryBytes(t *testing.T) { func TestBinaryBytes(t *testing.T) {