Skip to content

Commit

Permalink
Renamed some boolean methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Dec 30, 2023
1 parent 3833b20 commit 08d9204
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.sksamuel.tribune.core.map
* Transforms a String producing [Parser] into a Boolean producing Parser,
* by converting the String to a Boolean using the library function [toBoolean].
*/
fun <I, E> Parser<I, String, E>.toBoolean(): Parser<I, Boolean, E> =
fun <I, E> Parser<I, String, E>.boolean(): Parser<I, Boolean, E> =
flatMap {
val b = it.toBoolean()
b.right()
Expand All @@ -20,7 +20,7 @@ fun <I, E> Parser<I, String, E>.toBoolean(): Parser<I, Boolean, E> =
* Transforms a String producing [Parser] into a Boolean producing Parser,
* by converting the String to a Boolean using the library function [toBooleanStrict].
*/
fun <I, E> Parser<I, String, E>.toBooleanStrict(ifError: (String) -> E): Parser<I, Boolean, E> =
fun <I, E> Parser<I, String, E>.booleanStrict(ifError: (String) -> E): Parser<I, Boolean, E> =
flatMap { input ->
runCatching { input.toBooleanStrict() }.fold({ it.right() }, { ifError(input).leftNel() })
}
Expand All @@ -29,5 +29,5 @@ fun <I, E> Parser<I, String, E>.toBooleanStrict(ifError: (String) -> E): Parser<
* Transforms a nullable String producing [Parser] into a nullable Boolean producing Parser,
* by converting the String to a Boolean using the library function [toBooleanStrictOrNull].
*/
fun <I, E> Parser<I, String, E>.toBooleanStrictOrNull(): Parser<I, Boolean?, E> =
fun <I, E> Parser<I, String, E>.booleanStrictOrNull(): Parser<I, Boolean?, E> =
map { it.toBooleanStrictOrNull() }
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import io.kotest.matchers.shouldBe
class BooleanTest : FunSpec() {
init {
test("parser should support String -> Boolean") {
val p = Parser<String>().toBoolean()
val p = Parser<String>().boolean()
p.parse("foo").getOrNull() shouldBe false
p.parse("true").getOrNull() shouldBe true
p.parse("false").getOrNull() shouldBe false
}

test("parser should support String -> Boolean using strict mode") {
val p = Parser<String>().toBooleanStrict { "not a boolean" }
val p = Parser<String>().booleanStrict { "not a boolean" }
p.parse("foo").leftOrNull() shouldBe listOf("not a boolean")
p.parse("true").getOrNull() shouldBe true
p.parse("false").getOrNull() shouldBe false
}

test("parser should support String -> Boolean? using strict mode or null") {
val p = Parser<String>().toBooleanStrictOrNull()
val p = Parser<String>().booleanStrictOrNull()
p.parse("foo").getOrNull() shouldBe null
p.parse("true").getOrNull() shouldBe true
p.parse("false").getOrNull() shouldBe false
Expand Down

0 comments on commit 08d9204

Please sign in to comment.