🔀 Merge “release/v81” into “master”
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / utils / Booleans.kt
1 package net.pterodactylus.sone.utils
2
3 /**
4  * Returns the value of [block] if `this` is true, returns `null` otherwise.
5  */
6 fun <R> Boolean.ifTrue(block: () -> R): R? = if (this) block() else null
7
8 /**
9  * Returns the value of [block] if `this` is false, returns `null` otherwise.
10  */
11 fun <R> Boolean.ifFalse(block: () -> R): R? = if (!this) block() else null
12
13 /**
14  * Returns `this` but runs the given block if `this`  is `true`.
15  *
16  * @param block The block to run if `this` is `true`
17  * @return `this`
18  */
19 fun Boolean.onTrue(block: () -> Unit): Boolean = also { if (this) block() }
20
21 /**
22  * Returns `this` but runs the given block if `this`  is `false`.
23  *
24  * @param block The block to run if `this` is `false`
25  * @return `this`
26  */
27 fun Boolean.onFalse(block: () -> Unit): Boolean = this.also { if (!this) block() }