GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lancet/docs/en/api/packages/condition.md at main · duke-git/lancet

Expand file tree

/

condition.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
346 lines (251 loc) · 7.17 KB
/

condition.md

Copy path

File metadata and controls

346 lines (251 loc) · 7.17 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

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:

  • https://github.com/duke-git/lancet/blob/main/condition/condition.go

Usage:

import (
    "github.com/duke-git/lancet/v2/condition"
)

Index

  • Bool
  • And
  • Or
  • Xor
  • Nor
  • Xnor
  • Nand
  • Ternary
  • TernaryOperatordeprecated

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
    result1 := condition.Bool(false)
    result2 := condition.Bool(true)
    fmt.Println(result1) // false
    fmt.Println(result2) // true

    // integer
    result3 := condition.Bool(0) 
    result4 := condition.Bool(1)
    fmt.Println(result3) // false
    fmt.Println(result4) // true

    // string
    result5 := condition.Bool("")
    result6 := condition.Bool(" ")
    fmt.Println(result5) // false
    fmt.Println(result6) // true

    // slice
    nums := []int{}
    result7 := condition.Bool(nums)

    nums = append(nums, 1, 2)
    result8 := condition.Bool(nums)
    fmt.Println(result7) // false
    fmt.Println(result8) // true

    // struct
    result9 = condition.Bool(struct{}{})
    fmt.Println(result8) // false


    // Output:
    // false
    // true
    // false
    // true
    // false
    // true
    // false
    // true
    // false
}

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 if 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 if 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 if 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)) // false
}

Xnor

Returns true if both a and b or neither a nor b are truthy.

Signature:

func Xnor[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.Xnor(0, 0)) // true
    fmt.Println(condition.Xnor(0, 1)) // false
    fmt.Println(condition.Xnor(1, 0)) // false
    fmt.Println(condition.Xnor(1, 1)) // true
}

Nand

Returns false if 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.Nand(1, 0)) // true
    fmt.Println(condition.Nand(1, 1)) // false
}

Ternary

Checks the value of param `isTrue`, if true return ifValue else return elseValue

Signature:

func Ternary[T, U any](isTrue T, ifValue U, elseValue U) U

Example:运行

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
    conditionTrue := 2 > 1
    result1 := condition.Ternary(conditionTrue, 0, 1)

    conditionFalse := 2 > 3
    result2 := condition.Ternary(conditionFalse, 0, 1)
    
    fmt.Println(result1)
    fmt.Println(result2)

    // Output:
    // 0
    // 1
}

TernaryOperator

Checks the value of param `isTrue`, if true return ifValue else return elseValue

⚠️ This function is deprecated. use Ternary instead.

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() {
    conditionTrue := 2 > 1
    result1 := condition.TernaryOperator(conditionTrue, 0, 1)

    conditionFalse := 2 > 3
    result2 := condition.TernaryOperator(conditionFalse, 0, 1)
    
    fmt.Println(result1)
    fmt.Println(result2)

    // Output:
    // 0
    // 1
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.