Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ifFalse and ifTrue functions #127

Merged
merged 1 commit into from
Oct 4, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions kotlin-stdlib/api/android/kotlin-stdlib.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions kotlin-stdlib/api/jvm/kotlin-stdlib.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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 }
}
}