Skip to content

Commit

Permalink
add blankToNull
Browse files Browse the repository at this point in the history
  • Loading branch information
robstoll committed Apr 27, 2019
1 parent cc5552d commit 2ecd4f1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Current extension functions:
with the ability to define a different separator for the last separation
=> handy if you want to form sentences like `a, b and c`

- [`CharSequence.blankToNull`](https://github.com/robstoll/kbox/tree/v0.12.1/kbox-common/src/main/kotlin/ch/tutteli/kbox/blanktoNull.kt#L7)
returns the same `CharSequence` if not blank, `null` otherwise

- [`forEachIn(Array/Iterable/Sequence<E>, Array/Iterable/Sequence<E>, ..., action: (E) -> Unit)`](https://github.com/robstoll/kbox/tree/v0.12.1/kbox-common/src/main/kotlin/ch/tutteli/kbox/forEachIn.kt#L6)
applies the given action to each entry in the given `Iterable`s.

Expand Down
7 changes: 7 additions & 0 deletions kbox-common/src/main/kotlin/ch/tutteli/kbox/blankToNull.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ch.tutteli.kbox

/**
* Returns this [String] if it is not blank, null otherwise.
*/
@Suppress("NOTHING_TO_INLINE")
inline fun <T: CharSequence> T.blankToNull(): T? = if(this.isNotBlank()) this else null
26 changes: 26 additions & 0 deletions kbox-jvm/src/test/kotlin/ch/tutteli/kbox/BlankToNullSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ch.tutteli.kbox

import ch.tutteli.atrium.api.cc.en_GB.toBe
import ch.tutteli.atrium.assert
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe

object BlankToNullSpec : Spek({

describe("fun ${String::blankToNull.name}") {
listOf<Triple<String, CharSequence, CharSequence?>>(
Triple("empty String", "", null),
Triple("empty StringBuilder", StringBuilder(), null),
Triple("String \" \"", " ", null),
Triple("StringBuilder \" \"", StringBuilder(" "), null),
Triple("String \" h\"", " h", " h"),
Triple("StringBuilder \" h\"", StringBuilder(" h"), StringBuilder(" h")),
Triple("String \"h \"", "h ", "h "),
Triple("StringBuilder \"h \"", StringBuilder("h "), StringBuilder("h "))
).forEach { (description, value, expected) ->
test("given $description, expect $expected") {
assert(value.blankToNull().toString()).toBe(expected.toString())
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object IsNotNullAndNotSpec : Spek({
Triple("null", null, false),
Triple("empty String", "", false),
Triple("empty StringBuilder", StringBuilder(), false),
Triple("String \" \"", StringBuilder("hello"), true),
Triple("String \" \"", " ", true),
Triple("StringBuilder \" \"", StringBuilder(" "), true),
Triple("String \"hello\"", "hello", true),
Triple("StringBuilder \"hello\"", StringBuilder("hello"), true)
Expand All @@ -28,7 +28,7 @@ object IsNotNullAndNotSpec : Spek({
Triple("null", null, false),
Triple("empty String", "", false),
Triple("empty StringBuilder", StringBuilder(), false),
Triple("String \" \"", StringBuilder(" "), false),
Triple("String \" \"", " ", false),
Triple("StringBuilder \" \"", StringBuilder(" "), false),
Triple("String \"hello\"", "hello", true),
Triple("StringBuilder \"hello\"", StringBuilder("hello"), true)
Expand Down

0 comments on commit 2ecd4f1

Please sign in to comment.