# 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](https://github.com/carlmjohnson/truthy), thanks carlmjohnson.
## Source: - [https://github.com/duke-git/lancet/blob/main/condition/condition.go](https://github.com/duke-git/lancet/blob/main/condition/condition.go) ## Usage: ```go import ( "github.com/duke-git/lancet/v2/condition" ) ``` ## Index - [Bool](#Bool) - [And](#And) - [Or](#Or) - [Xor](#Generate) - [Nor](#Nor) - [Nand](#Nand) - [TernaryOperator](#TernaryOperator) ## Documentation ### BoolReturns 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.
Returns true if both a and b are truthy.
Signature: ```go func And[T, U any](a T, b U) bool ``` Example: ```go 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 } ``` ### OrReturns false iff neither a nor b is truthy.
Signature: ```go func Or[T, U any](a T, b U) bool ``` Example: ```go 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 } ``` ### XorReturns true iff a or b but not both is truthy.
Signature: ```go func Xor[T, U any](a T, b U) bool ``` Example: ```go 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 } ``` ### NorReturns true iff neither a nor b is truthy.
Signature: ```go func Nor[T, U any](a T, b U) bool ``` Example: ```go 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 } ``` ### NandReturns false iff both a and b are truthy
Signature: ```go func Nand[T, U any](a T, b U) bool ``` Example: ```go 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.Nand(1, 0)) // true fmt.Println(condition.Nand(1, 1)) // false } ``` ### TernaryOperatorChecks the value of param `isTrue`, if true return ifValue else return elseValue
Signature: ```go func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U ``` Example: ```go 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" } ```