From c695837b16d4b737583a57240d87f4435e78762a Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 29 Aug 2022 11:23:02 +0800 Subject: [PATCH] doc: add document for package condition --- docs/condition.md | 265 ++++++++++++++++++++++++++++++++++++++++ docs/condition_zh-CN.md | 264 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 529 insertions(+) create mode 100644 docs/condition.md create mode 100644 docs/condition_zh-CN.md diff --git a/docs/condition.md b/docs/condition.md new file mode 100644 index 0000000..2cbb11c --- /dev/null +++ b/docs/condition.md @@ -0,0 +1,265 @@ +# 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) +- [OrDone](#OrDone) +- [Nor](#Nor) +- [Nand](#Nand) +- [TernaryOperator](#TernaryOperator) + +
+ +## 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: + +```go +func Bool[T any](value T) bool +``` +Example: + +```go +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: + +```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 +} +``` + + + +### Or +

Returns 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 +} +``` + + + +### Xor +

Returns 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 +} +``` + + + +### Nor +

Returns 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 +} +``` + + + +### Nand +

Returns 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.NNandor(1, 0)) // true + fmt.Println(condition.Nand(1, 1)) // false +} +``` + + + +### TernaryOperator +

Checks the value of param `isTrue`, if true return trueValue else return falseValue

+ +Signature: + +```go +func TernaryOperator[T any](isTrue bool, trueValue T, falseValue T) T +``` +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" +} +``` + + + + + + diff --git a/docs/condition_zh-CN.md b/docs/condition_zh-CN.md new file mode 100644 index 0000000..54fada4 --- /dev/null +++ b/docs/condition_zh-CN.md @@ -0,0 +1,264 @@ +# Condition +condition包含一些用于条件判断的函数。这个包的实现参考了carlmjohnson的truthy包的实现,更多有用的信息可以在[truthy](https://github.com/carlmjohnson/truthy)中找到,感谢carlmjohnson。 + +
+ +## 源码: + +- [https://github.com/duke-git/lancet/blob/main/condition/condition.go](https://github.com/duke-git/lancet/blob/main/condition/condition.go) + +
+ +## 用法: +```go +import ( + "github.com/duke-git/lancet/v2/condition" +) +``` + +
+ +## Index + +- [Bool](#Bool) +- [And](#And) +- [Or](#Or) +- [Xor](#Generate) +- [OrDone](#OrDone) +- [Nor](#Nor) +- [Nand](#Nand) +- [TernaryOperator](#TernaryOperator) + +
+ +## 目录 + +### Bool +

返回传入参数的bool值.
+如果出入类型参数含有Bool方法, 会调用该方法并返回
+如果传入类型参数有IsZero方法, 返回IsZero方法返回值的取反
+slices和map的length大于0时,返回true,否则返回false
+其他类型会判断是否是零值

+ +函数签名: + +```go +func Bool[T any](value T) bool +``` +例子: + +```go +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 +

逻辑且操作,当切仅当a和b都为true时返回true

+ +函数签名: + +```go +func And[T, U any](a T, b U) bool +``` +例子: + +```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 +} +``` + + + +### Or +

逻辑或操作,当切仅当a和b都为false时返回false

+ +函数签名: + +```go +func Or[T, U any](a T, b U) bool +``` +例子: + +```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 +} +``` + + + +### Xor +

逻辑异或操作,a和b相同返回false,a和b不相同返回true

+ +函数签名: + +```go +func Xor[T, U any](a T, b U) bool +``` +例子: + +```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 +} +``` + + + +### Nor +

异或的取反操作

+ +函数签名: + +```go +func Nor[T, U any](a T, b U) bool +``` +例子: + +```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 +} +``` + + + +### Nand +

如果a和b都为真,返回false,否则返回true

+ +函数签名: + +```go +func Nand[T, U any](a T, b U) bool +``` +例子: + +```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.NNandor(1, 0)) // true + fmt.Println(condition.Nand(1, 1)) // false +} +``` + + + +### TernaryOperator +

三元运算符

+ +函数签名: + +```go +func TernaryOperator[T any](isTrue bool, trueValue T, falseValue T) T +``` +例子: + +```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" +} +``` + + + + + +