Skip to content

Commit

Permalink
Add Result.isSuccess/isFailure (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Synesso authored Jun 24, 2024
1 parent 2a7dcef commit 0e8be4b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Added
* Adds `Result<T>.isSuccess(predicate: (T) -> Boolean): Boolean` and `Result<T>.isFailure(predicate: (Throwable) -> Boolean): Boolean` (Jem Mawson)

## [0.5.6] - 2024-06-21

### Added
Expand Down
2 changes: 2 additions & 0 deletions lib/api/lib.api
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ public final class app/cash/quiver/extensions/ResultKt {
public static final fun failure (Ljava/lang/Throwable;)Ljava/lang/Object;
public static final fun flatTap (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun flatten (Ljava/lang/Object;)Ljava/lang/Object;
public static final fun isFailure (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public static final fun isSuccess (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public static final fun mapFailure (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun orThrow (Ljava/lang/Object;)Ljava/lang/Object;
public static final fun success (Ljava/lang/Object;)Ljava/lang/Object;
Expand Down
13 changes: 12 additions & 1 deletion lib/src/main/kotlin/app/cash/quiver/extensions/Result.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ fun <T> T.success(): Result<T> = Result.success(this)
*/
fun <A> Throwable.failure(): Result<A> = Result.failure(this)


/**
* Turns a nullable value into a [Result].
*/
Expand Down Expand Up @@ -80,3 +79,15 @@ inline fun <A, B> Result<A>.tap(f: (A) -> B): Result<A> = this.map { a ->
inline fun <A> Result<A>.flatTap(f: (A) -> Result<Any>): Result<A> = this.flatMap { a ->
f(a).map { a }
}

/**
* Returns false if Success or returns the result of the given predicate to the Failure value.
*/
inline fun <T> Result<T>.isFailure(predicate: (Throwable) -> Boolean): Boolean =
fold(onFailure = predicate, onSuccess = { false })

/**
* Returns false if Failure or returns the result of the given predicate to the Success value.
*/
inline fun <T> Result<T>.isSuccess(predicate: (T) -> Boolean): Boolean =
fold(onFailure = { false }, onSuccess = predicate)
12 changes: 12 additions & 0 deletions lib/src/test/kotlin/app/cash/quiver/extensions/ResultTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@ class ResultTest : StringSpec({
e.failure<Int>().flatTap { Result.failure(Exception("broken")) }.shouldBeFailure(e)
}

"Result.isFailure{}" {
Result.failure<String>(Exception("hello")).isFailure { it.message == "hello" } shouldBe true
Result.failure<String>(Exception("goodbye")).isFailure { it.message == "hello" } shouldBe false
"hello".success().isFailure { it.message == "hello" } shouldBe false
}

"Result.isSuccess{}" {
"hello".success().isSuccess { it == "hello" } shouldBe true
"goodbye".success().isSuccess { it == "hellow" } shouldBe false
Result.failure<String>(Exception("hello")).isSuccess { it == "hello" } shouldBe false
}

})

0 comments on commit 0e8be4b

Please sign in to comment.