Skip to content

Commit f0361d9

Browse files
committed
Added ifTake, runIf, applyIf, unlessTake
1 parent fc308bc commit f0361d9

File tree

4 files changed

+78
-2
lines changed
  • build-logic/build-logic-base/src/main/kotlin/com/ensody/buildlogic
  • reactivestate-core/src
    • commonMain/kotlin/com/ensody/reactivestate
    • commonTest/kotlin/com/ensody/reactivestate

4 files changed

+78
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* Added `childReactiveState` variant which takes an arbitrary event handler, so the parent ReactiveState doesn't have to implement the whole events interface.
2323
* Changed `MutableState.beforeUpdate`, `MutableState.afterUpdate` and `MutableState.withSetter` and `State.toMutable` to not be `@Composable`.
2424
* Removed direct dependency on JUnit 4, so you can choose more freely which JUnit version to use.
25+
* Added `ifTake` and `unlessTake` inversions of `takeIf` and `takeUnless`.
26+
* Added `runIf` and `applyIf`.
2527

2628
## 5.13.0
2729

build-logic/build-logic-base/src/main/kotlin/com/ensody/buildlogic/Utils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal fun Project.detectProjectVersion(): String =
8585
}?.removePrefix("v")?.removePrefix("-")?.takeIf { System.getenv("RUNNING_ON_CI") == "true" }
8686
?: run {
8787
val branchName = cli("git", "rev-parse", "--abbrev-ref", "HEAD")
88-
"999999.0.0-${sanitizeBranchName(branchName)}.1"
88+
"0.0.-${sanitizeBranchName(branchName)}.1"
8989
}
9090

9191
enum class OS {
@@ -120,5 +120,5 @@ private class VersionComparable(val parts: List<String>) : Comparable<VersionCom
120120
private fun sanitizeBranchName(name: String): String =
121121
sanitizeRegex.replace(name, "-")
122122

123-
private val versionRegex = Regex("""v-?(\d+)\.(\d+)\.(\d+)((-.+?\.)(\d+))*""")
123+
private val versionRegex = Regex("""v-?(\d+)\.(\d+)\.(\d+)(((?:-.+?)?\.)(\d+))*""")
124124
private val sanitizeRegex = Regex("""[^A-Za-z0-9\-]""")

reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/Utils.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,19 @@ public data class Wrapped<T>(public val value: T) : JvmSerializable, ReadOnlyPro
3030
override fun toString(): String =
3131
"Wrapped($value)"
3232
}
33+
34+
/** Returns the result of [block] if [value] is true, else null. Similar to [takeIf], but flipped arguments. */
35+
public inline fun <T> ifTake(value: Boolean, block: () -> T): T? =
36+
if (value) block() else null
37+
38+
/** Returns the result of [block] if [value] is false, else null. Similar to [takeUnless], but flipped arguments. */
39+
public inline fun <T> unlessTake(value: Boolean, block: () -> T): T? =
40+
if (!value) block() else null
41+
42+
/** Returns the result of [block] if [value] is true, else null. Similar to [run], but executes conditionally. */
43+
public inline fun <T, R> T.runIf(value: Boolean, block: T.() -> R): R? =
44+
if (value) block() else null
45+
46+
/** Executes [block] if [value] is true, else just returns `this`. Similar to [apply], but executes conditionally. */
47+
public inline fun <T> T.applyIf(value: Boolean, block: T.() -> Unit): T =
48+
if (value) apply(block) else this
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.ensody.reactivestate
2+
3+
import com.ensody.reactivestate.test.CoroutineTest
4+
import kotlinx.coroutines.delay
5+
import kotlin.test.Test
6+
import kotlin.test.assertEquals
7+
import kotlin.test.assertFalse
8+
import kotlin.test.assertNull
9+
import kotlin.test.assertSame
10+
import kotlin.test.assertTrue
11+
12+
internal class UtilsTest : CoroutineTest() {
13+
@Test
14+
fun testIfTake() = runTest {
15+
val result = ifTake(true) {
16+
delay(100)
17+
5
18+
}
19+
assertEquals(5, result)
20+
assertNull(ifTake(false) { 2 })
21+
}
22+
23+
@Test
24+
fun testUnlessTake() = runTest {
25+
val result = unlessTake(false) {
26+
delay(100)
27+
5
28+
}
29+
assertEquals(5, result)
30+
assertNull(unlessTake(true) { 2 })
31+
}
32+
33+
@Test
34+
fun testApplyIf() = runTest {
35+
var done = false
36+
val result = applyIf(true) {
37+
delay(100)
38+
done = true
39+
}
40+
assertSame(this, result)
41+
assertTrue(done)
42+
43+
done = false
44+
applyIf(false) { done = true }
45+
assertFalse(done)
46+
}
47+
48+
@Test
49+
fun testRunIf() = runTest {
50+
val result = runIf(true) {
51+
assertSame(this@runTest, this)
52+
delay(100)
53+
5
54+
}
55+
assertEquals(5, result)
56+
assertNull(runIf(false) { 2 })
57+
}
58+
}

0 commit comments

Comments
 (0)