All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.kodeworks.kvarg.check.lib.package.scala Maven / Gradle / Ivy

There is a newer version: 0.7
Show newest version
package no.kodeworks.kvarg.check

package object lib {

  object IsUppercase {
    def apply(msg: String = "names must be UPPERCASE"): Check[String] =
      Check((_: String).forall(_.isUpper), msg)
  }

  object Size {
    def apply[T](
                  min: Int = 0,
                  max: Int = Int.MaxValue,
                  msg: String = defaultMsg): Check[T] =
      Check[T]((x: T) => {
        val l = x.toString.size
        min.max(Int.MinValue) <= l && l <= max.min(Int.MaxValue)
      }, msg.format(min, max))

    val defaultMsg = "Size must be between %s and %s"
  }

  object Pattern {
    def apply[T](
                  regex: String,
                  flags: Int = 0,
                  msg: String = defaultMsg): Check[T] = {
      val r = java.util.regex.Pattern.compile(regex, flags)
      Check[T]((x: T) => {
        val l = x.toString
        r.matcher(l).matches
      }, msg.format(regex, flags))
    }

    val defaultMsg = "Value must pass regex %s with flags %s"
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy