Skip to content

Commit

Permalink
Add emptyForBlank to StringHelpers. Closes lift#1127
Browse files Browse the repository at this point in the history
  • Loading branch information
jeppenejsum committed Oct 4, 2011
1 parent 05d43e4 commit a499905
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
18 changes: 16 additions & 2 deletions core/util/src/main/scala/net/liftweb/util/StringHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,25 @@ trait StringHelpers {
/** @return a SuperString with more available methods such as roboSplit or commafy */
implicit def listStringToSuper(in: List[String]): SuperListString = new SuperListString(in)

@deprecated("Use blankForNull instead")
def emptyForNull(s: String) = blankForNull(s)

/**
* Test for null and return either the given String if not null or the empty String.
* Test for null and return either the given String if not null or the blank String.
*/
def emptyForNull(s: String) = if (s != null) s else ""
def blankForNull(s: String) = if (s != null) s else ""

/**
* Turn a String into a Box[String], with Empty for the blank string.
*
* A string containing only spaces is considered blank.
*
* @return Full(s.trim) if s is not null or blank, Empty otherwise
*/
def emptyForBlank(s: String) = blankForNull(s).trim match {
case "" => Empty
case s => Full(s)
}
}

/**
Expand Down
21 changes: 18 additions & 3 deletions core/util/src/test/scala/net/liftweb/util/StringHelpersSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,27 @@ object StringHelpersSpec extends Specification("StringHelpers Specification") wi
(null: String).commafy must beNull
}
}
"The emptyForNull method" should {
"The blankForNull method" should {
"return the empty String for a given null String" in {
StringHelpers.emptyForNull(null) must_== ""
StringHelpers.blankForNull(null) must_== ""
}
"return the given String for a given not-null String" in {
StringHelpers.emptyForNull("x") must_== "x"
StringHelpers.blankForNull("x") must_== "x"
}
}
"The emptyForBlank method" should {
import net.liftweb.common._
"return Empty for a given null String" in {
StringHelpers.emptyForBlank(null) must_== Empty
}
"return Empty for a given a blank String" in {
StringHelpers.emptyForBlank("") must_== Empty
}
"return Empty for a String of spaces" in {
StringHelpers.emptyForBlank(" ") must_== Empty
}
"return the trim'ed String for a given not-null String" in {
StringHelpers.emptyForBlank(" x ") must_== Full("x")
}
}
}
Expand Down

0 comments on commit a499905

Please sign in to comment.