Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
-->

# KBox

KBox is a very small but useful utility library for Kotlin (JVM, Android and JS) providing functions which are missing
in the stdlib such as:

- [failIf](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/failIf.kt)
- [takeIf](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/takeIf.kt)
- [prefix takeIf/takeUnless](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/takeIf.kt)
- [letIf/runIf/alsoIf/applyIf](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/xyzIf.kt)
- [blankToNull](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/blankToNull.kt)
- [isNotNullAndNotEmpty/Blank](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/isNotNullAndNot.kt)
- [identity](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/identity.kt)
Expand All @@ -42,6 +45,6 @@ dependencies {

Visit [https://robstoll.github.io/kbox/kdoc](https://robstoll.github.io/kbox/kdoc/).


# License

KBox is licensed under [Apache 2.0](http://opensource.org/licenses/Apache2.0).
1 change: 1 addition & 0 deletions src/commonMain/kotlin/ch/tutteli/kbox/takeIf.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import kotlin.contracts.contract
inline fun <R> takeIf(predicate: Boolean, provider: () -> R): R? {
contract {
callsInPlace(provider, InvocationKind.AT_MOST_ONCE)
//TODO add `predicate holds in block` once we use Kotlin 2.2.20
}
return if (predicate) provider() else null
}
22 changes: 0 additions & 22 deletions src/commonMain/kotlin/ch/tutteli/kbox/xIf.kt

This file was deleted.

91 changes: 91 additions & 0 deletions src/commonMain/kotlin/ch/tutteli/kbox/xyzIf.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package ch.tutteli.kbox

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* Delegates to [let] in case the given [predicate] holds, returns `this` otherwise.
*
* This function shall complement [let] for cases such as invoking a builder method conditionally which without [letIf]
* would be written as follows `if (predicate) t.let { ... } else t`
*
* @since 3.2.0
*/
@OptIn(ExperimentalContracts::class)
inline fun <T : U, R : U, U> T.letIf(predicate: Boolean, block: (T) -> R): U {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
//TODO add `predicate holds in block` once we use Kotlin 2.2.20
}
return if (predicate) let(block) else this
}

/**
* Delegates to [run] in case the given [predicate] holds, returns `this` otherwise.
*
* This function shall complement [run] for cases such as invoking a builder method conditionally which without [runIf]
* would be written as follows `if (predicate) t.run { ... } else t`
*
* @since 3.2.0
*/
@OptIn(ExperimentalContracts::class)
inline fun <T : U, R : U, U> T.runIf(predicate: Boolean, block: T.() -> R): U {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
//TODO add `predicate holds in block` once we use Kotlin 2.2.20
}
return if (predicate) run(block) else this
}

/**
* Delegates to [also] in case the given [predicate] holds, returns `this` otherwise.
*
* This function shall complement [also] and make simple conditional based side effects more readable.
*
* @since 3.2.0
*/
@OptIn(ExperimentalContracts::class)
inline fun <T> T.alsoIf(predicate: Boolean, block: (T) -> Unit): T {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
//TODO add `predicate holds in block` once we use Kotlin 2.2.20
}
return if (predicate) also(block) else this
}

/**
* Delegates to [apply] in case the given [predicate] holds, returns `this` otherwise.
*
* This function shall complement [apply] and make simple conditional based side effects more readable.
*
* @since 3.2.0
*/
@OptIn(ExperimentalContracts::class)
inline fun <T> T.applyIf(predicate: Boolean, block: T.() -> Unit): T {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
//TODO add `predicate holds in block` once we use Kotlin 2.2.20
}
return if (predicate) apply(block) else this
}

/**
* Returns `null` if the [predicate] holds, calls the [provider] otherwise.
*
* This function shall complement [kotlin.takeUnless] for cases where the cost to call the provider
* is high or the code involved to define the provider is long and a postfix `takeUnless` is less readable
* than stating it at the beginning.
*
* @since 3.2.0
*/
//TODO move to xIf with 4.0.0
@OptIn(ExperimentalContracts::class)
inline fun <R> takeUnless(predicate: Boolean, provider: () -> R): R? {
contract {
callsInPlace(provider, InvocationKind.AT_MOST_ONCE)
//TODO add `predicate holds in block` once we use Kotlin 2.2.20
}
return if (predicate.not()) provider() else null
}

Loading