Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add emptyForBlank to StringHelpers. Closes #1127

  • Loading branch information...
commit a4999059570b6da6ba40a4a2ab402cecd7958345 1 parent 05d43e4
@jeppenejsum jeppenejsum authored
View
18 core/util/src/main/scala/net/liftweb/util/StringHelpers.scala
@@ -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)
+ }
}
/**
View
21 core/util/src/test/scala/net/liftweb/util/StringHelpersSpec.scala
@@ -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")
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.