Main Nightly

SweetBool

在 Swift 中使用 Bool 类型的语法糖。

为什么?

为了简化代码的编写和阅读,使其更接近英语。

运算符

isTrue

用于检查 Bool 值是否为 true 的语法糖。通过移除使用 == 的显式检查,简化条件语句。

if canDrinkBeer.isTrue {
    ...

isFalse

用于检查 Bool 值是否为 false 的语法糖。通过移除使用 == 的显式检查,简化条件语句。

if canDrinkBeer.isFalse {
    ...

negated

具有取反值的新实例。

whenTrue

当自身为 true 时,用于运行闭包的运算符。使用此运算符创建将触发操作的管道。

canDrinkBeer
    .whenTrue { openBeer() }
    .whenFalse { closeBeer() }

whenFalse

当自身为 false 时,用于运行闭包的运算符。使用此运算符创建将触发操作的管道。

canDrinkBeer
    .whenTrue { openBeer() }
    .whenFalse { closeBeer() }

biTransform(yes:no:)

将布尔值转换为某种 T 类型。在较长的管道中使用它,以避免使用普通运算符破坏流程

manager
    .boolProperty
    .biTransform(yes: "was true", no: "was false")
    .count // working with String now

and

&& 的语法糖。编写更复杂的 if 语句有时会很混乱。使用运算符可以更简洁地编写。

 if canDrinkBeer
        .and( isHealthy )
        .and( hasMoney )
        .or( isFriend ) {
        ...

or

|| 的语法糖。编写更复杂的 if 语句有时会很混乱。使用运算符可以更简洁地编写。

 if canDrinkBeer
        .and( isHealthy )
        .and( hasMoney )
        .or( isFriend ) {
        ...

toInt

转换为 Int 类型。当为 true 时返回正值,为 false 时返回 0。不要假定 true 的任何特定整数值。

init(fromInt:)

Int 创建 Bool 实例。当 value0 时返回 false,对于所有其他情况返回 true

谓词

always

当您需要一个始终为 true 或始终为 false 的谓词时,可以使用如下全局函数

func takesPredicate<A>(_ p: (A) -> Bool) {... }

// Before

takesPredicate( { _ in true } )

// now

takesPredicate( always(true) )
takesPredicate( true.always )

SweetPredicate

用于将谓词作为类型处理的库。

WiP

正在进行设计和文档编写。欢迎您查看并试用。


🐇🕳 Rabbit Hole

本项目是 🐇🕳 Rabbit Hole Packages Collection 的一部分