Skip to content

Commit

Permalink
Add validated and validatedNec to boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoMeijer committed Feb 28, 2022
1 parent ef910b6 commit deb923e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion shared/src/main/scala/mouse/boolean.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mouse

import cats.data.{EitherNel, NonEmptyList, Validated, ValidatedNel}
import cats.data.{EitherNel, NonEmptyList, Validated, ValidatedNec, ValidatedNel}
import cats.{Applicative, ApplicativeError, Monoid}
import mouse.BooleanSyntax.LiftToPartiallyApplied

Expand All @@ -23,6 +23,12 @@ final class BooleanOps(private val b: Boolean) extends AnyVal {

@inline def eitherNel[L, R](ifFalse: => L, ifTrue: => R): EitherNel[L, R] = either(NonEmptyList.one(ifFalse), ifTrue)

@inline def validated[L, R](ifFalse: => L, ifTrue: => R): Validated[L, R] =
fold(Validated.valid(ifTrue), Validated.invalid(ifFalse))

@inline def validatedNec[L, R](ifFalse: => L, ifTrue: => R): ValidatedNec[L, R] =
fold(Validated.validNec(ifTrue), Validated.invalidNec(ifFalse))

@inline def validatedNel[L, R](ifFalse: => L, ifTrue: => R): ValidatedNel[L, R] =
fold(Validated.validNel(ifTrue), Validated.invalidNel(ifFalse))

Expand Down
10 changes: 10 additions & 0 deletions shared/src/test/scala/mouse/BooleanSyntaxTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class BooleanSyntaxTest extends MouseSuite {
assertEquals(false.eitherNel("error", 1), Either.left(NonEmptyList.one("error")))
}

test("BooleanSyntax.validated") {
assertEquals(true.validated("error", 1), Validated.valid(1))
assertEquals(false.validated("error", 1), Validated.invalid("error"))
}

test("BooleanSyntax.validatedNec") {
assertEquals(true.validatedNec("error", 1), Validated.validNec(1))
assertEquals(false.validatedNec("error", 1), Validated.invalidNec("error"))
}

test("BooleanSyntax.validatedNel") {
assertEquals(true.validatedNel("error", 1), Validated.validNel(1))
assertEquals(false.validatedNel("error", 1), Validated.invalidNel("error"))
Expand Down

0 comments on commit deb923e

Please sign in to comment.