4.9 KiB
Condition
Package condition contains some functions for conditional judgment. eg. And, Or, TernaryOperator... The implementation of this package refers to the implementation of carlmjohnson's truthy package, you may find more useful information in truthy, thanks carlmjohnson.
Source:
Usage:
import (
"github.com/duke-git/lancet/v2/condition"
)
Index
Documentation
Bool
Returns the truthy value of anything.
If the value's type has a Bool() bool method, the method is called and returned.
If the type has an IsZero() bool method, the opposite value is returned.
Slices and maps are truthy if they have a length greater than zero.
All other types are truthy if they are not their zero value.
Signature:
func Bool[T any](value T) bool
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
// bool
fmt.Println(condition.Bool(false)) // false
fmt.Println(condition.Bool(true)) // true
// integer
fmt.Println(condition.Bool(0)) // false
fmt.Println(condition.Bool(1)) // true
// float
fmt.Println(condition.Bool(0.0)) // false
fmt.Println(condition.Bool(0.1)) // true
// string
fmt.Println(condition.Bool("")) // false
fmt.Println(condition.Bool(" ")) // true
fmt.Println(condition.Bool("0")) // true
// slice
var nums [2]int
fmt.Println(condition.Bool(nums)) // false
nums = [2]int{0, 1}
fmt.Println(condition.Bool(nums)) // true
// map
fmt.Println(condition.Bool(map[string]string{})) // false
fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true
// struct
fmt.Println(condition.Bool(struct{}{})) // false
fmt.Println(condition.Bool(time.Now())) // true
}
And
Returns true if both a and b are truthy.
Signature:
func And[T, U any](a T, b U) bool
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.And(0, 1)) // false
fmt.Println(condition.And(0, "")) // false
fmt.Println(condition.And(0, "0")) // false
fmt.Println(condition.And(1, "0")) // true
}
Or
Returns false iff neither a nor b is truthy.
Signature:
func Or[T, U any](a T, b U) bool
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Or(0, "")) // false
fmt.Println(condition.Or(0, 1)) // true
fmt.Println(condition.Or(0, "0")) // true
fmt.Println(condition.Or(1, "0")) // true
}
Xor
Returns true iff a or b but not both is truthy.
Signature:
func Xor[T, U any](a T, b U) bool
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Xor(0, 0)) // false
fmt.Println(condition.Xor(0, 1)) // true
fmt.Println(condition.Xor(1, 0)) // true
fmt.Println(condition.Xor(1, 1)) // false
}
Nor
Returns true iff neither a nor b is truthy.
Signature:
func Nor[T, U any](a T, b U) bool
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Nor(0, 0)) // true
fmt.Println(condition.Nor(0, 1)) // false
fmt.Println(condition.Nor(1, 0)) // false
fmt.Println(condition.Nor(1, 1)) // true
}
Nand
Returns false iff both a and b are truthy
Signature:
func Nand[T, U any](a T, b U) bool
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Nand(0, 0)) // true
fmt.Println(condition.Nand(0, 1)) // true
fmt.Println(condition.NNandor(1, 0)) // true
fmt.Println(condition.Nand(1, 1)) // false
}
TernaryOperator
Checks the value of param `isTrue`, if true return ifValue else return elseValue
Signature:
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
trueValue := "1"
falseValue := "0"
fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1"
}