diff --git a/CHANGELOG.md b/CHANGELOG.md index e49333b9..b67d8351 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Added +- `Boolean.ifFalse(block: () -> Unit): Boolean` +- `Boolean.ifTrue(block: () -> Unit): Boolean` - `T?.ifNotNull(block: () -> Unit): T?` - `T?.ifNull(block: () -> Unit): T?` - `Graph` data structure diff --git a/kotlin-stdlib/api/android/kotlin-stdlib.api b/kotlin-stdlib/api/android/kotlin-stdlib.api index c698a936..5f51bcc8 100644 --- a/kotlin-stdlib/api/android/kotlin-stdlib.api +++ b/kotlin-stdlib/api/android/kotlin-stdlib.api @@ -141,6 +141,11 @@ public final class com/javiersc/kotlin/stdlib/AnsiColorsKt { public static final fun ansiColor (Ljava/lang/String;Lcom/javiersc/kotlin/stdlib/AnsiColor;)Ljava/lang/String; } +public final class com/javiersc/kotlin/stdlib/BooleanKt { + public static final fun ifFalse (ZLkotlin/jvm/functions/Function0;)Z + public static final fun ifTrue (ZLkotlin/jvm/functions/Function0;)Z +} + public final class com/javiersc/kotlin/stdlib/CollectionsKt { public static final fun capitalize (Ljava/lang/String;)Ljava/lang/String; public static final fun decapitalize (Ljava/lang/String;)Ljava/lang/String; diff --git a/kotlin-stdlib/api/jvm/kotlin-stdlib.api b/kotlin-stdlib/api/jvm/kotlin-stdlib.api index c9b36fb2..8d22ecfa 100644 --- a/kotlin-stdlib/api/jvm/kotlin-stdlib.api +++ b/kotlin-stdlib/api/jvm/kotlin-stdlib.api @@ -141,6 +141,11 @@ public final class com/javiersc/kotlin/stdlib/AnsiColorsKt { public static final fun ansiColor (Ljava/lang/String;Lcom/javiersc/kotlin/stdlib/AnsiColor;)Ljava/lang/String; } +public final class com/javiersc/kotlin/stdlib/BooleanKt { + public static final fun ifFalse (ZLkotlin/jvm/functions/Function0;)Z + public static final fun ifTrue (ZLkotlin/jvm/functions/Function0;)Z +} + public final class com/javiersc/kotlin/stdlib/CollectionsKt { public static final fun capitalize (Ljava/lang/String;)Ljava/lang/String; public static final fun decapitalize (Ljava/lang/String;)Ljava/lang/String; diff --git a/kotlin-stdlib/common/main/kotlin/com/javiersc/kotlin/stdlib/Boolean.kt b/kotlin-stdlib/common/main/kotlin/com/javiersc/kotlin/stdlib/Boolean.kt new file mode 100644 index 00000000..94d8d45d --- /dev/null +++ b/kotlin-stdlib/common/main/kotlin/com/javiersc/kotlin/stdlib/Boolean.kt @@ -0,0 +1,11 @@ +package com.javiersc.kotlin.stdlib + +public inline fun Boolean.ifFalse(block: () -> Unit): Boolean { + if (!this) block() + return this +} + +public inline fun Boolean.ifTrue(block: () -> Unit): Boolean { + if (this) block() + return this +} diff --git a/kotlin-stdlib/common/test/kotlin/com/javiersc/kotlin/stdlib/BooleanTest.kt b/kotlin-stdlib/common/test/kotlin/com/javiersc/kotlin/stdlib/BooleanTest.kt new file mode 100644 index 00000000..6a12f8a3 --- /dev/null +++ b/kotlin-stdlib/common/test/kotlin/com/javiersc/kotlin/stdlib/BooleanTest.kt @@ -0,0 +1,35 @@ +package com.javiersc.kotlin.stdlib + +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class BooleanTest { + + private val fail: Boolean = false + private val success: Boolean = true + + @Test + fun ifFalse() { + var isFalse = false + var isTrue = false + + fail.ifFalse { isFalse = true } + success.ifFalse { isTrue = true } + + assertFalse { isTrue } + assertTrue { isFalse } + } + + @Test + fun ifTrue() { + var isFalse = false + var isTrue = false + + fail.ifTrue { isFalse = true } + success.ifTrue { isTrue = true } + + assertFalse { isFalse } + assertTrue { isTrue } + } +}